main
js 55 lines 1.69 KB
Raw
1 #!/usr/bin/env node
2
3 'use strict';
4
5 const {spawnSync} = require('child_process');
6 const {readJsonSync} = require('fs-extra');
7 const {join} = require('path');
8 const theme = require('../theme');
9
10 const run = async ({cwd, dry, tag}, packageName) => {
11 const packagePath = join(cwd, 'build/node_modules', packageName);
12 const {version} = readJsonSync(join(packagePath, 'package.json'));
13
14 // Check if this package version has already been published.
15 // If so we might be resuming from a previous run.
16 const {status: npmViewStatus} = spawnSync('npm', [
17 'view',
18 `${packageName}@${version}`,
19 ]);
20 const packageExists = npmViewStatus === 0;
21 if (packageExists) {
22 console.log(
23 theme`{package ${packageName}} {version ${version}} has already been published.`
24 );
25 return;
26 }
27
28 console.log(
29 `::group::${theme`{spinnerSuccess ✓} Publishing {package ${packageName}}${dry ? ' (--dry-run)' : ''}`}`
30 );
31 // OIDC trusted publishing authorizes exactly one publish per call, and
32 // can't add/remove dist-tags after the fact — so we only ever publish under
33 // a single tag, and that tag is the one chosen at workflow dispatch time.
34 const args = ['publish', `--tag`, tag];
35 if (dry) {
36 args.push('--dry-run');
37 }
38 console.log(theme.command(` cd ${packagePath}`));
39 console.log(theme.command(` npm ${args.join(' ')}`));
40
41 const {error, status: publishStatus} = spawnSync('npm', args, {
42 cwd: packagePath,
43 stdio: ['ignore', 'inherit', 'inherit'],
44 });
45 console.log('::endgroup::');
46 if (error) {
47 throw error;
48 } else if (publishStatus !== 0) {
49 throw new Error(
50 `Failed to publish ${packageName}@${version} with tag ${tag}`
51 );
52 }
53 };
54
55 module.exports = run;