also looking at this
Gate releases on shipped artifact findings
#316
11 files
+454
-25
| @@ -0,0 +1,42 @@ | ||
| 1 | import { join, resolve } from 'node:path' | |
| 2 | import { readArchiveManifest } from './scan-release-artifacts' | |
| 3 | ||
| 4 | type Command = (args: string[]) => { exitCode: number | null, stdout: string, stderr: string } | |
| 5 | ||
| 6 | const npmCommand: Command = args => { | |
| 7 | const result = Bun.spawnSync(['npm', ...args], { stdout: 'pipe', stderr: 'pipe' }) | |
| 8 | return { exitCode: result.exitCode, stdout: result.stdout.toString(), stderr: result.stderr.toString() } | |
| 9 | } | |
| 10 | ||
| 11 | export function publishNpmArchives(directory: string, command: Command = npmCommand, dryRun = false): void { | |
| 12 | const root = resolve(directory) | |
| 13 | const archives = readArchiveManifest(root) | |
| 14 | for (const archive of archives) { | |
| 15 | const path = join(root, archive.file) | |
| 16 | if (!dryRun) { | |
| 17 | const lookup = command(['view', `${archive.name}@${archive.version}`, 'version', '--json']) | |
| 18 | if (lookup.exitCode === 0) { | |
| 19 | if (JSON.parse(lookup.stdout) !== archive.version) | |
| 20 | throw new Error(`${archive.name}: npm returned an unexpected published version`) | |
| 21 | console.log(`${archive.name}@${archive.version} already published; skipping`) | |
| 22 | continue | |
| 23 | } | |
| 24 | if (!lookup.stderr.includes('E404')) | |
| 25 | throw new Error(`${archive.name}: npm version lookup failed: ${lookup.stderr}`) | |
| 26 | } | |
| 27 | // A failed or changed archive must never be published after the scan. | |
| 28 | readArchiveManifest(root) | |
| 29 | const args = ['publish', path, '--access', 'public', '--ignore-scripts', '--provenance'] | |
| 30 | if (dryRun) args.push('--dry-run') | |
| 31 | const publish = command(args) | |
| 32 | if (publish.exitCode !== 0) | |
| 33 | throw new Error(`${archive.name}: npm publish failed: ${publish.stderr}`) | |
| 34 | console.log(`${dryRun ? 'Dry-run checked' : 'Published'} ${archive.name}@${archive.version}`) | |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | if (import.meta.main) { | |
| 39 | const directory = Bun.argv[2] | |
| 40 | if (!directory) throw new Error('Usage: publish-npm-archives.ts <archive-dir> [--dry-run]') | |
| 41 | publishNpmArchives(directory, npmCommand, Bun.argv.includes('--dry-run')) | |
| 42 | } | |