|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import { Command } from 'commander'; |
| 4 | +import { readFile } from 'node:fs/promises'; |
| 5 | +import { dirname, resolve } from 'node:path'; |
| 6 | +import { pathToFileURL } from 'node:url'; |
| 7 | +import { createPublicationPlan, renderPublicationPlan } from './publication-plan.js'; |
| 8 | +import { sha256File } from './release-lib.js'; |
| 9 | +import { readReleaseRecord, verifyReleaseArtifacts } from './release-record.js'; |
| 10 | + |
| 11 | +const NATIVE_TARGETS = ['macos-arm64', 'macos-x64', 'linux-arm64', 'linux-x64']; |
| 12 | +const DESKTOP_ARCHITECTURES = ['arm64', 'x64']; |
| 13 | + |
| 14 | +/** @param {{recordPath: string}} options */ |
| 15 | +export async function inspectReleaseCandidate(options) { |
| 16 | + const recordPath = resolve(options.recordPath); |
| 17 | + const releaseRoot = dirname(recordPath); |
| 18 | + const record = await readReleaseRecord(recordPath); |
| 19 | + if ( |
| 20 | + record.state !== 'prepared' || |
| 21 | + record.stages.at(-1)?.name !== 'distribution-finalized' || |
| 22 | + record.publication.state !== 'unpublished' |
| 23 | + ) { |
| 24 | + throw new Error( |
| 25 | + 'Candidate inspection requires a finalized, unpublished prepared release.', |
| 26 | + ); |
| 27 | + } |
| 28 | + await verifyReleaseArtifacts(record, releaseRoot); |
| 29 | + |
| 30 | + const nativeTargets = record.verifications.map((verification) => { |
| 31 | + const target = /** @type {{operatingSystem: string, architecture: string}} */ ( |
| 32 | + verification.target |
| 33 | + ); |
| 34 | + return `${target.operatingSystem}-${target.architecture}`; |
| 35 | + }); |
| 36 | + if (nativeTargets.join(',') !== NATIVE_TARGETS.join(',')) { |
| 37 | + throw new Error( |
| 38 | + 'Candidate does not contain the complete native verification matrix.', |
| 39 | + ); |
| 40 | + } |
| 41 | + const desktopArchitectures = record.artifacts |
| 42 | + .filter((artifact) => artifact.type === 'desktop-dmg') |
| 43 | + .map((artifact) => artifact.architecture); |
| 44 | + if (desktopArchitectures.join(',') !== DESKTOP_ARCHITECTURES.join(',')) { |
| 45 | + throw new Error( |
| 46 | + 'Candidate does not contain both architecture-specific Desktop DMGs.', |
| 47 | + ); |
| 48 | + } |
| 49 | + for (const type of [ |
| 50 | + 'homebrew-formula', |
| 51 | + 'homebrew-cask', |
| 52 | + 'desktop-update-metadata', |
| 53 | + 'release-metadata', |
| 54 | + 'npm-package', |
| 55 | + ]) { |
| 56 | + if (!record.artifacts.some((artifact) => artifact.type === type)) { |
| 57 | + throw new Error(`Candidate is missing required artifact type ${type}.`); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const planPath = resolve(releaseRoot, 'publication-plan.md'); |
| 62 | + const expectedPlan = renderPublicationPlan(createPublicationPlan(record)); |
| 63 | + if ((await readFile(planPath, 'utf8')) !== expectedPlan) { |
| 64 | + throw new Error('Candidate publication plan does not match its release record.'); |
| 65 | + } |
| 66 | + |
| 67 | + return { |
| 68 | + schemaVersion: 1, |
| 69 | + status: 'ready-for-publication-review', |
| 70 | + releaseId: record.releaseId, |
| 71 | + releaseVersion: record.releaseVersion, |
| 72 | + source: { ...record.source }, |
| 73 | + versions: { ...record.versions }, |
| 74 | + policy: { ...record.policy }, |
| 75 | + state: record.state, |
| 76 | + publicationState: record.publication.state, |
| 77 | + artifactCount: record.artifacts.length, |
| 78 | + nativeTargets, |
| 79 | + desktopArchitectures, |
| 80 | + publicationPlanSha256: await sha256File(planPath), |
| 81 | + publicMutationPerformed: false, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) { |
| 86 | + const program = new Command() |
| 87 | + .name('release:inspect') |
| 88 | + .description('Inspect a finalized release candidate without publishing it') |
| 89 | + .requiredOption('--record <path>', 'finalized release-record.json path') |
| 90 | + .option('--json', 'emit the inspection summary as JSON', false) |
| 91 | + .action(async (values) => { |
| 92 | + const result = await inspectReleaseCandidate({ recordPath: values.record }); |
| 93 | + if (values.json) { |
| 94 | + console.log(JSON.stringify(result, null, 2)); |
| 95 | + return; |
| 96 | + } |
| 97 | + console.log( |
| 98 | + `Verified ${result.releaseId} from ${result.source.commit}: ` + |
| 99 | + `${result.artifactCount} artifacts, ${result.nativeTargets.length} native targets, ` + |
| 100 | + `${result.desktopArchitectures.length} Desktop DMGs; no public mutation performed.`, |
| 101 | + ); |
| 102 | + }); |
| 103 | + await program.parseAsync(); |
| 104 | +} |
0 commit comments