-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add publish target and generate minimal publish script for … (
#9806) * feat(js): add publish target and generate minimal publish script for publishable libraries * fix(js): adjust minimal publish script to display better error for version validation * Update packages/js/src/utils/minimal-publish-script.ts Co-authored-by: Caleb Ukle <[email protected]> Co-authored-by: Caleb Ukle <[email protected]>
- Loading branch information
1 parent
02cf4be
commit 2735aa0
Showing
3 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
|
||
const publishScriptContent = ` | ||
/** | ||
* This is a minimal script to publish your package to "npm". | ||
* This is meant to be used as-is or customize as you see fit. | ||
* | ||
* This script is executed on "dist/path/to/library" as "cwd" by default. | ||
* | ||
* You might need to authenticate with NPM before running this script. | ||
*/ | ||
import { execSync } from 'child_process'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import * as chalk from 'chalk'; | ||
// Executing publish script: node path/to/publish.mjs {name} --version {version} --tag {tag} | ||
// Default "tag" to "next" so we won't publish the "latest" tag by accident. | ||
const [, , name, version, tag = 'next'] = process.argv; | ||
// A simple SemVer validation to validate the version | ||
const validVersion = /^\\d+\\.\\d+\\.\\d(-\\w+\\.\\d+)?/; | ||
if (!version || !validVersion.test(version)) { | ||
console.error(chalk.bold.red(\`No version provided or version did not match Semantic Versioning, expected: #.#.#-tag.# or #.#.#, got \${version}\`)); | ||
process.exit(1); | ||
} | ||
// Updating the version in "package.json" before publishing | ||
try { | ||
const json = JSON.parse(readFileSync(\`package.json\`).toString()); | ||
json.version = version; | ||
writeFileSync(\`package.json\`, JSON.stringify(json, null, 2)); | ||
} catch (e) { | ||
console.error(chalk.bold.red(\`Error reading package.json file from library build output.\`)) | ||
} | ||
// Execute "npm publish" to publish | ||
execSync(\`npm publish --access public\`); | ||
`; | ||
|
||
export function addMinimalPublishScript(tree: Tree) { | ||
const publishScriptPath = 'tools/scripts/publish.mjs'; | ||
|
||
if (!tree.exists(publishScriptPath)) { | ||
tree.write(publishScriptPath, publishScriptContent); | ||
} | ||
|
||
return publishScriptPath; | ||
} |