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