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/npm-packages.tsmodified+27-6
Changes to scripts/npm-packages.ts
@@ -1,9 +1,11 @@
1import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
1import { createHash } from 'node:crypto'
2import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
23import { tmpdir } from 'node:os'
3import { dirname, join, resolve } from 'node:path'
4import { dirname, join, resolve, sep } from 'node:path'
45
56type Manifest = {
67 name: string
8 version: string
79 private?: boolean
810 scripts?: Record<string, string>
911 peerDependencies?: Record<string, string>
@@ -76,10 +78,18 @@ function importSpecifiers(pkg: Manifest): string[] {
7678 return [pkg.name]
7779}
7880
79export async function verifyNpmPackages(buildOnly = false): Promise<void> {
81export async function verifyNpmPackages(buildOnly = false, archiveDir?: string): Promise<void> {
8082 const packages = publicPackages()
8183 if (packages.length === 0)
8284 throw new Error('No public npm workspaces found')
85 if (buildOnly && archiveDir) throw new Error('Cannot export archives in build-only mode')
86 const output = archiveDir ? resolve(archiveDir) : undefined
87 if (output) {
88 if (output === root || output.startsWith(`${root}${sep}`))
89 throw new Error('Archive output must be outside the repository')
90 mkdirSync(output, { recursive: true })
91 if (readdirSync(output).length) throw new Error('Archive output must be empty')
92 }
8393 const temp = mkdtempSync(join(tmpdir(), 'craft-npm-verify-'))
8494 try {
8595 for (const { dir, pkg } of packages) {
@@ -95,12 +105,17 @@ export async function verifyNpmPackages(buildOnly = false): Promise<void> {
95105 return
96106
97107 const specifiers: string[] = []
108 const archives: { name: string, version: string, file: string, sha256: string }[] = []
98109 for (const [index, { dir, pkg }] of packages.entries()) {
99 const archive = join(temp, `${index}.tgz`)
110 const file = `${index}.tgz`
111 const archive = join(output ?? temp, file)
100112 run([process.execPath, 'pm', 'pack', '--filename', archive], dir)
101113 const entries = run(['tar', '-tzf', archive], temp).trim().split('\n')
102114 const packed: Manifest = JSON.parse(run(['tar', '-xOzf', archive, 'package/package.json'], temp))
103115 verifyArchiveTargets(packed, entries)
116 if (packed.name !== pkg.name || packed.version !== pkg.version)
117 throw new Error(`${pkg.name}: packed identity differs from the source manifest`)
118 archives.push({ name: packed.name, version: packed.version, file, sha256: createHash('sha256').update(readFileSync(archive)).digest('hex') })
104119 const installed = join(temp, 'node_modules', pkg.name)
105120 mkdirSync(installed, { recursive: true })
106121 run(['tar', '-xzf', archive, '--strip-components=1', '-C', installed], temp)
@@ -133,6 +148,8 @@ export async function verifyNpmPackages(buildOnly = false): Promise<void> {
133148 '--target', 'esnext', '--skipLibCheck', '--ignoreConfig',
134149 '--typeRoots', join(root, 'node_modules/@types'),
135150 ], temp)
151 if (output)
152 writeFileSync(join(output, 'manifest.json'), `${JSON.stringify({ packages: archives }, null, 2)}\n`)
136153 console.log(`Verified ${packages.length} npm archives and ${specifiers.length} installed import/type entry points.`)
137154 }
138155 finally {
@@ -140,5 +157,9 @@ export async function verifyNpmPackages(buildOnly = false): Promise<void> {
140157 }
141158}
142159
143if (import.meta.main)
144 await verifyNpmPackages(Bun.argv.includes('--build-only'))
160if (import.meta.main) {
161 const index = Bun.argv.indexOf('--archive-dir')
162 if (index >= 0 && (!Bun.argv[index + 1] || Bun.argv[index + 1]!.startsWith('--')))
163 throw new Error('--archive-dir requires an output path')
164 await verifyNpmPackages(Bun.argv.includes('--build-only'), index >= 0 ? Bun.argv[index + 1] : undefined)
165}