-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 …
…publishable libraries
- Loading branch information
Showing
3 changed files
with
118 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,44 @@ | ||
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.red(\`Invalid version\`)); | ||
process.exit(1); | ||
} | ||
// Updating the version in "package.json" before publishing | ||
const json = JSON.parse(readFileSync(\`package.json\`).toString()); | ||
json.version = version; | ||
writeFileSync(\`package.json\`, JSON.stringify(json, null, 2)); | ||
// 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; | ||
} |