| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | const crypto = require("crypto"); |
| 4 | const fs = require("fs/promises"); |
| 5 | const path = require("path"); |
| 6 | |
| 7 | const { |
| 8 | allReleaseAssetNames, |
| 9 | BUNDLE_ASSET_NAMES, |
| 10 | BUNDLE_CHECKSUM_MANIFEST, |
| 11 | CHECKSUM_MANIFEST, |
| 12 | CHECKSUM_SIGNATURE, |
| 13 | checksummedReleaseAssetNames, |
| 14 | } = require("../../npm/codewhale/scripts/artifacts"); |
| 15 | |
| 16 | const compiledHosts = require("../../npm/codewhale/scripts/compiled-hosts"); |
| 17 | |
| 18 | async function readHostCatalog(directory) { |
| 19 | const file = path.join(directory, compiledHosts.HOST_CATALOG); |
| 20 | try { |
| 21 | const metadata = await fs.lstat(file); |
| 22 | if (!metadata.isFile() || metadata.isSymbolicLink()) throw new Error("compiled host catalog is not a regular file"); |
| 23 | return compiledHosts.parseCatalog(await fs.readFile(file, "utf8")); |
| 24 | } catch (error) { |
| 25 | if (error.code === "ENOENT") return undefined; |
| 26 | throw error; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | const WINDOWS_LAUNCHER = "codewhale.bat"; |
| 31 | |
| 32 | function usage() { |
| 33 | return [ |
| 34 | "Usage:", |
| 35 | " node scripts/release/assemble-release-assets.js INPUT_DIR OUTPUT_DIR", |
| 36 | " node scripts/release/assemble-release-assets.js --verify ASSET_DIR", |
| 37 | ].join("\n"); |
| 38 | } |
| 39 | |
| 40 | async function sha256(filePath) { |
| 41 | const hash = crypto.createHash("sha256"); |
| 42 | hash.update(await fs.readFile(filePath)); |
| 43 | return hash.digest("hex"); |
| 44 | } |
| 45 | |
| 46 | function parseChecksumManifest(content, label) { |
| 47 | const checksums = new Map(); |
| 48 | for (const line of content.split(/\r?\n/)) { |
| 49 | const trimmed = line.trim(); |
| 50 | if (!trimmed) { |
| 51 | continue; |
| 52 | } |
| 53 | const match = trimmed.match(/^([a-fA-F0-9]{64})\s+\*?(.+)$/); |
| 54 | if (!match) { |
| 55 | throw new Error(`${label} contains an invalid checksum row: ${trimmed}`); |
| 56 | } |
| 57 | const name = match[2]; |
| 58 | if (checksums.has(name)) { |
| 59 | throw new Error(`${label} contains duplicate checksum rows for ${name}`); |
| 60 | } |
| 61 | checksums.set(name, match[1].toLowerCase()); |
| 62 | } |
| 63 | return checksums; |
| 64 | } |
| 65 | |
| 66 | function assertExactNames(actualNames, expectedNames, label) { |
| 67 | const actual = new Set(actualNames); |
| 68 | const expected = new Set(expectedNames); |
| 69 | const missing = expectedNames.filter((name) => !actual.has(name)); |
| 70 | const unexpected = actualNames.filter((name) => !expected.has(name)); |
| 71 | if (missing.length > 0 || unexpected.length > 0 || actual.size !== actualNames.length) { |
| 72 | throw new Error( |
| 73 | `${label} does not match the authoritative inventory` + |
| 74 | `${missing.length > 0 ? `; missing: ${missing.join(", ")}` : ""}` + |
| 75 | `${unexpected.length > 0 ? `; unexpected: ${unexpected.join(", ")}` : ""}` + |
| 76 | `${actual.size !== actualNames.length ? "; duplicate basenames are present" : ""}`, |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | async function assertManifest(directory, manifestName, expectedNames) { |
| 82 | const manifestPath = path.join(directory, manifestName); |
| 83 | const checksums = parseChecksumManifest( |
| 84 | await fs.readFile(manifestPath, "utf8"), |
| 85 | manifestName, |
| 86 | ); |
| 87 | assertExactNames([...checksums.keys()], expectedNames, manifestName); |
| 88 | for (const name of expectedNames) { |
| 89 | const actual = await sha256(path.join(directory, name)); |
| 90 | if (checksums.get(name) !== actual) { |
| 91 | throw new Error(`${manifestName} checksum mismatch for ${name}`); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | async function verifyAssetDirectory(directory) { |
| 97 | const entries = await fs.readdir(directory, { withFileTypes: true }); |
| 98 | const nonFiles = entries.filter((entry) => !entry.isFile()); |
| 99 | if (nonFiles.length > 0) { |
| 100 | throw new Error( |
| 101 | `Release asset directory must be flat; found: ${nonFiles.map((entry) => entry.name).join(", ")}`, |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | const catalog = await readHostCatalog(directory); |
| 106 | const expected = allReleaseAssetNames(catalog); |
| 107 | const signature = entries.find((entry) => entry.name === CHECKSUM_SIGNATURE); |
| 108 | if (signature && (await fs.stat(path.join(directory, CHECKSUM_SIGNATURE))).size > 1024) { |
| 109 | throw new Error(`${CHECKSUM_SIGNATURE} is larger than a detached signature`); |
| 110 | } |
| 111 | assertExactNames( |
| 112 | entries.map((entry) => entry.name).filter((name) => name !== CHECKSUM_SIGNATURE), |
| 113 | expected, |
| 114 | "Release asset directory", |
| 115 | ); |
| 116 | await assertManifest(directory, CHECKSUM_MANIFEST, checksummedReleaseAssetNames(catalog)); |
| 117 | await assertManifest(directory, BUNDLE_CHECKSUM_MANIFEST, BUNDLE_ASSET_NAMES); |
| 118 | if (catalog) compiledHosts.verifyDirectory(directory, catalog); |
| 119 | console.log(`Verified ${expected.length} release assets in ${directory}`); |
| 120 | } |
| 121 | |
| 122 | function windowsLauncherContents() { |
| 123 | return [ |
| 124 | "@echo off", |
| 125 | "where wt >nul 2>nul", |
| 126 | "set NO_ANIMATIONS=1", |
| 127 | 'if "%ERRORLEVEL%"=="0" (', |
| 128 | ' wt --title Codewhale cmd /k "%~dp0codewhale-windows-x64.exe"', |
| 129 | ") else (", |
| 130 | ' "%~dp0codewhale-windows-x64.exe"', |
| 131 | ")", |
| 132 | "", |
| 133 | ].join("\r\n"); |
| 134 | } |
| 135 | |
| 136 | function intermediateArtifactPath(inputDirectory, name, catalog) { |
| 137 | if (compiledHosts.assets(catalog).includes(name)) return path.join(inputDirectory, "codewhale-compiled-hosts", name); |
| 138 | if (name === BUNDLE_CHECKSUM_MANIFEST || BUNDLE_ASSET_NAMES.includes(name)) { |
| 139 | return path.join(inputDirectory, "codewhale-bundles", name); |
| 140 | } |
| 141 | return path.join(inputDirectory, name, name); |
| 142 | } |
| 143 | |
| 144 | async function assemble(inputDirectory, outputDirectory) { |
| 145 | const catalog = await readHostCatalog(path.join(inputDirectory, "codewhale-compiled-hosts")); |
| 146 | const expected = allReleaseAssetNames(catalog); |
| 147 | const generated = new Set([WINDOWS_LAUNCHER, CHECKSUM_MANIFEST]); |
| 148 | const copiedNames = expected.filter((name) => !generated.has(name)); |
| 149 | const sources = new Map(); |
| 150 | for (const name of copiedNames) { |
| 151 | const source = intermediateArtifactPath(inputDirectory, name, catalog); |
| 152 | let sourceStat; |
| 153 | try { |
| 154 | sourceStat = await fs.lstat(source); |
| 155 | } catch (error) { |
| 156 | if (error && error.code === "ENOENT") { |
| 157 | throw new Error(`Downloaded release artifacts are missing ${name} at ${source}`); |
| 158 | } |
| 159 | throw error; |
| 160 | } |
| 161 | if (!sourceStat.isFile()) { |
| 162 | throw new Error(`Downloaded release artifact must be a regular file: ${source}`); |
| 163 | } |
| 164 | sources.set(name, source); |
| 165 | } |
| 166 | |
| 167 | await fs.mkdir(outputDirectory, { recursive: true }); |
| 168 | const existing = await fs.readdir(outputDirectory); |
| 169 | if (existing.length > 0) { |
| 170 | throw new Error(`Output directory must be empty: ${outputDirectory}`); |
| 171 | } |
| 172 | |
| 173 | for (const name of copiedNames) { |
| 174 | await fs.copyFile(sources.get(name), path.join(outputDirectory, name)); |
| 175 | } |
| 176 | await fs.writeFile( |
| 177 | path.join(outputDirectory, WINDOWS_LAUNCHER), |
| 178 | windowsLauncherContents(), |
| 179 | "utf8", |
| 180 | ); |
| 181 | |
| 182 | const checksumRows = []; |
| 183 | for (const name of [...checksummedReleaseAssetNames(catalog)].sort()) { |
| 184 | checksumRows.push(`${await sha256(path.join(outputDirectory, name))} ${name}`); |
| 185 | } |
| 186 | await fs.writeFile( |
| 187 | path.join(outputDirectory, CHECKSUM_MANIFEST), |
| 188 | `${checksumRows.join("\n")}\n`, |
| 189 | "utf8", |
| 190 | ); |
| 191 | |
| 192 | await verifyAssetDirectory(outputDirectory); |
| 193 | } |
| 194 | |
| 195 | async function main() { |
| 196 | if (process.argv[2] === "--verify") { |
| 197 | if (process.argv.length !== 4) { |
| 198 | throw new Error(usage()); |
| 199 | } |
| 200 | await verifyAssetDirectory(path.resolve(process.argv[3])); |
| 201 | return; |
| 202 | } |
| 203 | if (process.argv.length !== 4) { |
| 204 | throw new Error(usage()); |
| 205 | } |
| 206 | await assemble(path.resolve(process.argv[2]), path.resolve(process.argv[3])); |
| 207 | } |
| 208 | |
| 209 | if (require.main === module) { |
| 210 | main().catch((error) => { |
| 211 | console.error(`Release asset assembly failed: ${error.message}`); |
| 212 | process.exit(1); |
| 213 | }); |
| 214 | } |
| 215 | |
| 216 | module.exports = { |
| 217 | assemble, |
| 218 | parseChecksumManifest, |
| 219 | verifyAssetDirectory, |
| 220 | windowsLauncherContents, |
| 221 | }; |
| 222 |