ReviewOS

also looking at this

craft-native/craft

Gate releases on shipped artifact findings

#316
Merged glennmichael123 wants to merge codex/gate-shipped-artifacts into main
11 files +454 -25
scripts/publish-npm-archives.tsadded+42-0
Changes to scripts/publish-npm-archives.ts
@@ -0,0 +1,42 @@
1import { join, resolve } from 'node:path'
2import { readArchiveManifest } from './scan-release-artifacts'
3
4type Command = (args: string[]) => { exitCode: number | null, stdout: string, stderr: string }
5
6const 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
11export 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
38if (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}