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