main
js 72 lines 1.99 KB
Raw
1 #!/usr/bin/env node
2
3 'use strict';
4
5 const commandLineArgs = require('command-line-args');
6 const {splitCommaParams} = require('../utils');
7
8 const paramDefinitions = [
9 {
10 name: 'dry',
11 type: Boolean,
12 description: 'Dry run command without actually publishing to NPM.',
13 defaultValue: false,
14 },
15 {
16 name: 'tag',
17 type: String,
18 description:
19 'NPM dist-tag to attach at publish time. OIDC trusted publishing ' +
20 'authorizes a single tag per publish, so only one value is accepted ' +
21 '— passing comma-separated tags or repeating --tag is rejected.',
22 },
23 {
24 name: 'onlyPackages',
25 type: String,
26 multiple: true,
27 description: 'Packages to include in publishing',
28 defaultValue: [],
29 },
30 {
31 name: 'skipPackages',
32 type: String,
33 multiple: true,
34 description: 'Packages to exclude from publishing',
35 defaultValue: [],
36 },
37 ];
38
39 module.exports = () => {
40 const params = commandLineArgs(paramDefinitions);
41 splitCommaParams(params.skipPackages);
42 splitCommaParams(params.onlyPackages);
43
44 // Single-tag invariant. `command-line-args` already collapses multiple
45 // --tag occurrences to the last value (since `multiple` is not set), but it
46 // happily accepts `--tag a,b` as the literal string "a,b". Reject that
47 // here so the failure is loud and obvious instead of being deferred to a
48 // later "Unsupported tag" message that doesn't explain the cause.
49 if (params.tag == null || params.tag === '') {
50 console.error('--tag is required and must be a single dist-tag.');
51 process.exit(1);
52 }
53 if (params.tag.includes(',') || params.tag.includes(' ')) {
54 console.error('Only a single --tag is allowed, got: "' + params.tag + '"');
55 process.exit(1);
56 }
57 switch (params.tag) {
58 case 'latest':
59 case 'canary':
60 case 'experimental':
61 case 'backport':
62 case 'alpha':
63 case 'beta':
64 case 'rc':
65 break;
66 default:
67 console.error('Unsupported tag: "' + params.tag + '"');
68 process.exit(1);
69 }
70
71 return params;
72 };