Repository navigation
Expand file tree
/
Copy pathrelease-publish.mjs
More file actions
59 lines (53 loc) · 2.37 KB
/
Copy pathrelease-publish.mjs
File metadata and controls
59 lines (53 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
// Uploads the tarballs the release build packed and verified, through npm trusted publishing.
// It runs in the only job holding `id-token: write`, so it uses nothing but Node and the npm CLI.
const [directory, version, distTag] = process.argv.slice(2);
if (!directory || !version || !distTag) {
throw new Error('Usage: node scripts/release-publish.mjs <tarball-dir> <version> <dist-tag>');
}
const [major, minor, patch] = npm(['--version']).trim().split('.').map(Number);
if (major < 11 || (major === 11 && (minor < 5 || (minor === 5 && patch < 1)))) {
throw new Error('npm trusted publishing needs npm 11.5.1 or newer.');
}
const tarballs = fs
.readdirSync(directory)
.filter((file) => file.endsWith('.tgz'))
.map((file) => {
const tarball = path.join(directory, file);
const manifest = JSON.parse(
execFileSync('tar', ['-xOf', tarball, 'package/package.json'], { encoding: 'utf8' }),
);
if (!/^(agent-device|@agent-device\/[a-z0-9-]+)$/.test(manifest.name)) {
throw new Error(`${file} packs ${manifest.name}, which this release does not own.`);
}
if (manifest.version !== version || manifest.private === true) {
throw new Error(`${file} packs ${manifest.name}@${manifest.version}, not ${version}.`);
}
return { tarball, name: manifest.name };
})
// Plugins first: the core going live is the signal that the whole release is available.
.sort((a, b) => Number(a.name === 'agent-device') - Number(b.name === 'agent-device'));
if (!tarballs.some((entry) => entry.name === 'agent-device')) {
throw new Error(`${directory} has no agent-device tarball.`);
}
const publishArgs = ['--tag', distTag, '--access', 'public'];
for (const { tarball } of tarballs) npm(['publish', tarball, ...publishArgs, '--dry-run']);
for (const { tarball, name } of tarballs) {
if (isPublished(name)) {
process.stdout.write(`${name}@${version} is already published; skipping.\n`);
continue;
}
npm(['publish', tarball, ...publishArgs, '--provenance'], 'inherit');
}
function isPublished(name) {
try {
return npm(['view', `${name}@${version}`, 'version', '--json']).trim() !== '';
} catch {
return false;
}
}
function npm(args, stdio = 'pipe') {
return execFileSync('npm', args, { encoding: 'utf8', stdio: ['ignore', stdio, 'inherit'] }) ?? '';
}