feat(init): publishing
This commit is contained in:
parent
32636217ee
commit
c297e3f4ef
15 changed files with 9934 additions and 6837 deletions
75
scripts/publish.ts
Normal file
75
scripts/publish.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import * as yargs from 'yargs';
|
||||
import * as child_process from 'child_process';
|
||||
|
||||
const usage = `Publish a package
|
||||
|
||||
This is a wrapper around \`npm publish\`.
|
||||
|
||||
It reads a npm registry url, and authentication token from input arguments,
|
||||
configures npm (in the project context) for publishing, then publishes and
|
||||
restores the npm configuration.
|
||||
`
|
||||
|
||||
const argv = require('yargs')
|
||||
.usage('$0 [args]')
|
||||
.option('registry', {
|
||||
alias: 'r',
|
||||
describe: 'URL of npm registry',
|
||||
type: 'string'
|
||||
})
|
||||
.option('authToken', {
|
||||
alias: 't',
|
||||
describe: 'authentication token',
|
||||
type: 'string'
|
||||
})
|
||||
.demandOption(['registry'])
|
||||
.help()
|
||||
.argv;
|
||||
|
||||
argv.authToken = process.env.NPM_AUTH_TOKEN ?? argv.authToken;
|
||||
|
||||
if (!argv.registry) { throw new Error('no url provided.')}
|
||||
if (!argv.authToken) {
|
||||
throw new Error(
|
||||
'no authToken or \'NPM_AUTH_TOKEN\' environment variable provided.'
|
||||
)
|
||||
}
|
||||
|
||||
console.info(`npm: set registry ${argv.registry} --location project`);
|
||||
if(child_process.spawnSync(
|
||||
'npm',
|
||||
['set', 'registry', argv.registry],
|
||||
{ stdio: "inherit" }
|
||||
).status) { process.exit(1) }
|
||||
|
||||
const registry = argv.registry.replace('https://', '').replace('http://', '');
|
||||
|
||||
const authTokenConfigItemName = '${registry}/:_authToken';
|
||||
|
||||
console.info(`npm: set _authToken ...`);
|
||||
if (child_process.spawnSync(
|
||||
'npm',
|
||||
['set', authTokenConfigItemName, argv.authToken, '--location', 'project'],
|
||||
{ stdio: "inherit" }
|
||||
).status) { process.exit(1) }
|
||||
|
||||
console.info(`npm: publish`);
|
||||
if (child_process.spawnSync('npm', ['publish'], { stdio: "inherit" }).status) {
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
console.info(`npm: delete registry --location project`);
|
||||
if (child_process.spawnSync(
|
||||
'npm',
|
||||
['config', 'delete', 'registry', '--location', 'project'],
|
||||
{ stdio: "inherit" }
|
||||
).status) { process.exit(1) }
|
||||
|
||||
console.info(`npm: delete _authToken ... --location project`);
|
||||
if (child_process.spawnSync(
|
||||
'npm',
|
||||
['config', 'delete', authTokenConfigItemName, '--location', 'project'],
|
||||
{ stdio: "inherit" }
|
||||
).status) { process.exit(1) }
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue