| @@ -0,0 +1,144 @@ |
| 1 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { dirname, join, resolve } from 'node:path' |
| 4 | + |
| 5 | +type Manifest = { |
| 6 | + name: string |
| 7 | + private?: boolean |
| 8 | + scripts?: Record<string, string> |
| 9 | + peerDependencies?: Record<string, string> |
| 10 | + main?: string |
| 11 | + module?: string |
| 12 | + types?: string |
| 13 | + svelte?: string |
| 14 | + bin?: string | Record<string, string> |
| 15 | + exports?: unknown |
| 16 | +} |
| 17 | + |
| 18 | +const root = resolve(import.meta.dir, '..') |
| 19 | + |
| 20 | +function run(args: string[], cwd: string): string { |
| 21 | + const result = Bun.spawnSync(args, { cwd, stdout: 'pipe', stderr: 'pipe' }) |
| 22 | + if (result.exitCode !== 0) |
| 23 | + throw new Error(`${args.join(' ')} failed in ${cwd}\n${result.stdout}\n${result.stderr}`) |
| 24 | + return result.stdout.toString() |
| 25 | +} |
| 26 | + |
| 27 | +export function manifestTargets(pkg: Manifest): string[] { |
| 28 | + const targets = new Set<string>() |
| 29 | + const add = (value: unknown): void => { |
| 30 | + if (typeof value === 'string') { |
| 31 | + if (value.includes('*')) |
| 32 | + throw new Error(`${pkg.name}: wildcard targets require explicit archive validation: ${value}`) |
| 33 | + const target = value.replace(/^\.\ |
| 34 | + if (target.startsWith('/') || target.split('/').includes('..') || target.includes('\\')) |
| 35 | + throw new Error(`${pkg.name}: invalid package target ${value}`) |
| 36 | + targets.add(target) |
| 37 | + } |
| 38 | + else if (value && typeof value === 'object') { |
| 39 | + Object.values(value).forEach(add) |
| 40 | + } |
| 41 | + } |
| 42 | + for (const value of [pkg.main, pkg.module, pkg.types, pkg.svelte, pkg.bin, pkg.exports]) |
| 43 | + add(value) |
| 44 | + return [...targets] |
| 45 | +} |
| 46 | + |
| 47 | +export function verifyArchiveTargets(pkg: Manifest, entries: string[]): void { |
| 48 | + const files = new Set(entries.filter(entry => !entry.endsWith('/'))) |
| 49 | + for (const target of manifestTargets(pkg)) { |
| 50 | + if (!files.has(`package/${target}`)) |
| 51 | + throw new Error(`${pkg.name}: packed artifact is missing ${target}`) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function publicPackages(): { dir: string, pkg: Manifest }[] { |
| 56 | + const packages: { dir: string, pkg: Manifest }[] = [] |
| 57 | + |
| 58 | + |
| 59 | + for (const path of new Bun.Glob('packages/*/package.json').scanSync({ cwd: root })) { |
| 60 | + const pkg: Manifest = JSON.parse(readFileSync(join(root, path), 'utf8')) |
| 61 | + if (!pkg.private) |
| 62 | + packages.push({ dir: dirname(join(root, path)), pkg }) |
| 63 | + } |
| 64 | + |
| 65 | + return packages.sort((a, b) => a.pkg.name === 'craft-native' ? -1 : b.pkg.name === 'craft-native' ? 1 : a.pkg.name.localeCompare(b.pkg.name)) |
| 66 | +} |
| 67 | + |
| 68 | +function importSpecifiers(pkg: Manifest): string[] { |
| 69 | + if (!pkg.exports) |
| 70 | + return pkg.main || pkg.module ? [pkg.name] : [] |
| 71 | + if (typeof pkg.exports === 'object' && pkg.exports !== null) { |
| 72 | + const keys = Object.keys(pkg.exports) |
| 73 | + if (keys.some(key => key.startsWith('.'))) |
| 74 | + return keys.filter(key => key !== './package.json').map(key => key === '.' ? pkg.name : `${pkg.name}${key.slice(1)}`) |
| 75 | + } |
| 76 | + return [pkg.name] |
| 77 | +} |
| 78 | + |
| 79 | +export async function verifyNpmPackages(buildOnly = false): Promise<void> { |
| 80 | + const packages = publicPackages() |
| 81 | + if (packages.length === 0) |
| 82 | + throw new Error('No public npm workspaces found') |
| 83 | + const temp = mkdtempSync(join(tmpdir(), 'craft-npm-verify-')) |
| 84 | + try { |
| 85 | + for (const { dir, pkg } of packages) { |
| 86 | + if (pkg.scripts?.build) { |
| 87 | + console.log(`Building ${pkg.name}`) |
| 88 | + |
| 89 | + rmSync(join(dir, 'dist'), { recursive: true, force: true }) |
| 90 | + run([process.execPath, 'run', 'build'], dir) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (buildOnly) |
| 95 | + return |
| 96 | + |
| 97 | + const specifiers: string[] = [] |
| 98 | + for (const [index, { dir, pkg }] of packages.entries()) { |
| 99 | + const archive = join(temp, `${index}.tgz`) |
| 100 | + run([process.execPath, 'pm', 'pack', '--filename', archive], dir) |
| 101 | + const entries = run(['tar', '-tzf', archive], temp).trim().split('\n') |
| 102 | + const packed: Manifest = JSON.parse(run(['tar', '-xOzf', archive, 'package/package.json'], temp)) |
| 103 | + verifyArchiveTargets(packed, entries) |
| 104 | + const installed = join(temp, 'node_modules', pkg.name) |
| 105 | + mkdirSync(installed, { recursive: true }) |
| 106 | + run(['tar', '-xzf', archive, '--strip-components=1', '-C', installed], temp) |
| 107 | + specifiers.push(...importSpecifiers(packed)) |
| 108 | + console.log(`Verified ${pkg.name}: ${manifestTargets(packed).length} packed targets`) |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + for (const { dir, pkg } of packages) { |
| 114 | + for (const peer of Object.keys(pkg.peerDependencies ?? {})) { |
| 115 | + const destination = join(temp, 'node_modules', peer) |
| 116 | + if (existsSync(destination)) |
| 117 | + continue |
| 118 | + let source = join(dir, 'node_modules', peer) |
| 119 | + if (!existsSync(source)) source = join(root, 'node_modules', peer) |
| 120 | + if (!existsSync(source)) throw new Error(`${pkg.name}: missing installed peer ${peer}`) |
| 121 | + mkdirSync(dirname(destination), { recursive: true }) |
| 122 | + symlinkSync(source, destination, 'dir') |
| 123 | + } |
| 124 | + } |
| 125 | + const runtimeImports = specifiers.map(specifier => `await import(${JSON.stringify(specifier)})`).join('\n') |
| 126 | + writeFileSync(join(temp, 'runtime.mjs'), runtimeImports) |
| 127 | + run([process.execPath, 'runtime.mjs'], temp) |
| 128 | + const typeImports = specifiers.map((specifier, index) => `import * as entry${index} from ${JSON.stringify(specifier)}; void entry${index}`).join('\n') |
| 129 | + writeFileSync(join(temp, 'consumer.mts'), typeImports) |
| 130 | + run([ |
| 131 | + process.execPath, join(root, 'packages/typescript/scripts/tsc.ts'), 'consumer.mts', |
| 132 | + '--noEmit', '--strict', '--module', 'esnext', '--moduleResolution', 'bundler', |
| 133 | + '--target', 'esnext', '--skipLibCheck', '--ignoreConfig', |
| 134 | + '--typeRoots', join(root, 'node_modules/@types'), |
| 135 | + ], temp) |
| 136 | + console.log(`Verified ${packages.length} npm archives and ${specifiers.length} installed import/type entry points.`) |
| 137 | + } |
| 138 | + finally { |
| 139 | + rmSync(temp, { recursive: true, force: true }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +if (import.meta.main) |
| 144 | + await verifyNpmPackages(Bun.argv.includes('--build-only')) |