also looking at this
fix(release): publish one complete platform manifest
#293
4 files
+145
-2
| @@ -0,0 +1,61 @@ | ||
| 1 | import { createHash } from 'node:crypto' | |
| 2 | import { lstatSync, readFileSync, readdirSync, writeFileSync } from 'node:fs' | |
| 3 | import { join } from 'node:path' | |
| 4 | ||
| 5 | const requiredAssets = ['craft-darwin-arm64.zip', 'craft-darwin-x64.zip', 'craft-linux-x64.zip', 'craft-windows-x64.zip'] | |
| 6 | ||
| 7 | type ReleaseIdentity = { repository: string, tag: string, commit: string } | |
| 8 | type ReleaseAsset = { name: string, size: number, sha256: string } | |
| 9 | type ReleaseManifest = ReleaseIdentity & { schemaVersion: 1, generatedAt: string, assets: ReleaseAsset[] } | |
| 10 | ||
| 11 | export function createReleaseManifest(identity: ReleaseIdentity, directory: string): ReleaseManifest { | |
| 12 | if (!/^[\w.-]+\/[\w.-]+$/.test(identity.repository) || !/^v\d+\.\d+\.\d+(?:[-+][\w.+-]+)?$/.test(identity.tag) || !/^[a-f\d]{40}$/i.test(identity.commit)) | |
| 13 | throw new Error('Release manifest requires repository, version tag, and full commit identity') | |
| 14 | const names = readdirSync(directory).filter(name => /^craft-.*\.zip$/.test(name)).sort() | |
| 15 | const missing = requiredAssets.filter(name => !names.includes(name)) | |
| 16 | if (missing.length) | |
| 17 | throw new Error(`Missing release archives: ${missing.join(', ')}`) | |
| 18 | const assets = names.map((name) => { | |
| 19 | const path = join(directory, name) | |
| 20 | if (!lstatSync(path).isFile()) | |
| 21 | throw new Error(`Release archive must be a regular file: ${name}`) | |
| 22 | const bytes = readFileSync(path) | |
| 23 | if (bytes.length === 0) | |
| 24 | throw new Error(`Release archive is empty: ${name}`) | |
| 25 | return { name, size: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex') } | |
| 26 | }) | |
| 27 | return { schemaVersion: 1, ...identity, generatedAt: new Date().toISOString(), assets } | |
| 28 | } | |
| 29 | ||
| 30 | export function verifyReleaseManifest(actual: unknown, expected: ReleaseManifest): void { | |
| 31 | if (!actual || typeof actual !== 'object') | |
| 32 | throw new Error('Release manifest must be an object') | |
| 33 | const manifest = actual as Partial<ReleaseManifest> | |
| 34 | for (const key of ['schemaVersion', 'repository', 'tag', 'commit'] as const) { | |
| 35 | if (manifest[key] !== expected[key]) | |
| 36 | throw new Error(`Release manifest ${key} mismatch`) | |
| 37 | } | |
| 38 | if (!Array.isArray(manifest.assets) || manifest.assets.length !== expected.assets.length) | |
| 39 | throw new Error('Release manifest asset set mismatch') | |
| 40 | const seen = new Set<string>() | |
| 41 | for (const asset of manifest.assets) { | |
| 42 | const wanted = expected.assets.find(entry => entry.name === asset?.name) | |
| 43 | if (!wanted || seen.has(asset.name)) | |
| 44 | throw new Error(`Unexpected or duplicate release asset: ${asset?.name}`) | |
| 45 | seen.add(asset.name) | |
| 46 | if (asset.size !== wanted.size || asset.sha256 !== wanted.sha256) | |
| 47 | throw new Error(`Release manifest size or SHA-256 mismatch: ${asset.name}`) | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | if (import.meta.main) { | |
| 52 | const [mode, directory, path] = Bun.argv.slice(2) | |
| 53 | if (!directory || !path || !['create', 'verify'].includes(mode)) | |
| 54 | throw new Error('Usage: bun scripts/release-manifest.ts <create|verify> <archive-directory> <manifest-path>') | |
| 55 | const manifest = createReleaseManifest({ repository: process.env.GITHUB_REPOSITORY ?? '', tag: process.env.GITHUB_REF_NAME ?? '', commit: process.env.GITHUB_SHA ?? '' }, directory) | |
| 56 | if (mode === 'create') | |
| 57 | writeFileSync(path, `${JSON.stringify(manifest, null, 2)}\n`) | |
| 58 | else | |
| 59 | verifyReleaseManifest(JSON.parse(readFileSync(path, 'utf8')), manifest) | |
| 60 | console.log(`${mode === 'create' ? 'Created' : 'Verified'} manifest for ${manifest.assets.length} final release archives`) | |
| 61 | } | |