-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-checksums.mjs
More file actions
62 lines (56 loc) · 2.06 KB
/
generate-checksums.mjs
File metadata and controls
62 lines (56 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
import { promises as fs } from "fs";
import path from "path";
import crypto from "crypto";
const ROOT_DIR = process.cwd();
const OUTPUT_PATH = path.join(ROOT_DIR, "checksums.txt");
const CURRENT_VERSION = "1.1.0";
const TARGETS = [
"manifest.json",
`schemas/v${CURRENT_VERSION}`,
`examples/v${CURRENT_VERSION}`
];
async function walk(targetPath) {
const absolute = path.join(ROOT_DIR, targetPath);
const stat = await fs.stat(absolute);
if (stat.isFile()) return [absolute];
const entries = await fs.readdir(absolute, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const fullPath = path.join(absolute, entry.name);
if (entry.isDirectory()) files.push(...(await walk(path.relative(ROOT_DIR, fullPath))));
else if (entry.isFile() && entry.name.endsWith(".json")) files.push(fullPath);
}
return files;
}
function isCoveredReleaseArtifact(relPath) {
return relPath === "manifest.json"
|| relPath.startsWith(`schemas/v${CURRENT_VERSION}/`)
|| relPath.startsWith(`examples/v${CURRENT_VERSION}/`);
}
async function hashFile(filePath) {
const buf = await fs.readFile(filePath);
return {
rel: path.relative(ROOT_DIR, filePath).replace(/\\/g, "/"),
hash: crypto.createHash("sha256").update(buf).digest("hex")
};
}
async function main() {
const collected = new Set();
for (const target of TARGETS) {
for (const file of await walk(target)) collected.add(file);
}
const rows = [];
for (const file of [...collected].sort()) rows.push(await hashFile(file));
if (rows.length === 0) throw new Error("no checksum targets collected");
for (const { rel } of rows) {
if (!isCoveredReleaseArtifact(rel)) throw new Error(`unexpected checksum target: ${rel}`);
}
await fs.writeFile(OUTPUT_PATH, rows.map(({ hash, rel }) => `${hash} ${rel}`).join("\n") + "\n");
console.log(`✅ Wrote ${rows.length} current-line machine-artifact checksums to ${OUTPUT_PATH}`);
}
main().catch((error) => {
console.error("❌ Failed to generate checksums.");
console.error(error);
process.exit(1);
});