Skip to content

Commit b02b484

Browse files
authored
Update generate-checksums.mjs
1 parent 4f28606 commit b02b484

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

scripts/generate-checksums.mjs

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,84 @@
1+
#!/usr/bin/env node
12
import fs from "fs";
23
import path from "path";
34
import crypto from "crypto";
45
import { fileURLToPath } from "url";
56

7+
function die(msg) {
8+
console.error(`ERROR: ${msg}`);
9+
process.exit(1);
10+
}
11+
12+
// --- Args: [rootDir] [outFile]
13+
const rootDirArg = process.argv[2] || "schemas/v1.0.0";
14+
const outFileArg = process.argv[3] || "checksums.txt";
15+
616
// Resolve repo root (scripts/..)
717
const __filename = fileURLToPath(import.meta.url);
818
const __dirname = path.dirname(__filename);
919
const repoRoot = path.resolve(__dirname, "..");
1020

11-
// Which paths to include in checksums
12-
const ROOTS = [
13-
"schemas/v1.0.0"
14-
];
15-
16-
// Paths to skip entirely
17-
const IGNORE_PREFIXES = [
18-
".git",
19-
"node_modules",
20-
".github",
21-
"checksums.txt"
22-
];
23-
24-
function shouldIgnore(relPath) {
25-
return IGNORE_PREFIXES.some((prefix) => {
26-
return (
27-
relPath === prefix ||
28-
relPath.startsWith(prefix + path.sep)
29-
);
30-
});
31-
}
21+
const rootAbs = path.resolve(repoRoot, rootDirArg);
22+
const outAbs = path.resolve(repoRoot, outFileArg);
3223

33-
function walk(relPath, acc) {
34-
const absPath = path.join(repoRoot, relPath);
35-
const stat = fs.statSync(absPath);
24+
const IGNORE_PREFIXES = [".git", "node_modules", ".github"];
3625

37-
if (stat.isDirectory()) {
38-
const entries = fs.readdirSync(absPath);
39-
for (const entry of entries) {
40-
const childRel = path.join(relPath, entry);
41-
if (shouldIgnore(childRel)) continue;
42-
walk(childRel, acc);
43-
}
44-
} else if (stat.isFile()) {
45-
if (!shouldIgnore(relPath)) {
46-
acc.push(relPath.replace(/\\/g, "/")); // normalize to POSIX-style
47-
}
48-
}
26+
function toPosix(p) {
27+
return p.replace(/\\/g, "/");
28+
}
29+
30+
function shouldIgnore(relPosixPath) {
31+
return IGNORE_PREFIXES.some(
32+
(prefix) => relPosixPath === prefix || relPosixPath.startsWith(prefix + "/")
33+
);
4934
}
5035

51-
function sha256File(relPath) {
52-
const absPath = path.join(repoRoot, relPath);
36+
function sha256File(absPath) {
5337
const buf = fs.readFileSync(absPath);
54-
const hash = crypto.createHash("sha256").update(buf).digest("hex");
55-
return hash;
38+
return crypto.createHash("sha256").update(buf).digest("hex");
39+
}
40+
41+
function walkDir(absDir, relBasePosix, acc) {
42+
const entries = fs.readdirSync(absDir, { withFileTypes: true })
43+
.map((e) => e.name)
44+
.sort();
45+
46+
for (const name of entries) {
47+
const absChild = path.join(absDir, name);
48+
const relChildPosix = toPosix(path.posix.join(relBasePosix, name));
49+
50+
if (shouldIgnore(relChildPosix)) continue;
51+
52+
const stat = fs.statSync(absChild);
53+
if (stat.isDirectory()) {
54+
walkDir(absChild, relChildPosix, acc);
55+
} else if (stat.isFile()) {
56+
// Canonical scope: JSON schemas only
57+
if (relChildPosix.endsWith(".json")) acc.push(relChildPosix);
58+
}
59+
}
5660
}
5761

5862
function main() {
63+
if (!fs.existsSync(rootAbs)) die(`Missing root directory: ${rootDirArg}`);
64+
const st = fs.statSync(rootAbs);
65+
if (!st.isDirectory()) die(`Root is not a directory: ${rootDirArg}`);
66+
67+
const relRootPosix = toPosix(rootDirArg);
5968
const files = [];
60-
for (const root of ROOTS) {
61-
const abs = path.join(repoRoot, root);
62-
if (!fs.existsSync(abs)) continue;
63-
walk(root, files);
64-
}
69+
walkDir(rootAbs, relRootPosix, files);
6570

6671
files.sort();
6772

68-
const lines = files.map((rel) => {
69-
const hash = sha256File(rel);
70-
return `${hash} ${rel}`;
73+
const lines = files.map((relPosix) => {
74+
const absPath = path.join(repoRoot, relPosix);
75+
const hash = sha256File(absPath);
76+
// Match common sha256sum style (binary marker *)
77+
return `${hash} *${relPosix}`;
7178
});
7279

73-
const outPath = path.join(repoRoot, "checksums.txt");
74-
fs.writeFileSync(outPath, lines.join("\n") + "\n");
75-
76-
console.log(`Wrote ${lines.length} checksums to checksums.txt`);
80+
fs.writeFileSync(outAbs, lines.join("\n") + "\n", "utf8");
81+
console.log(`Wrote ${lines.length} checksums to ${toPosix(path.relative(repoRoot, outAbs))}`);
7782
}
7883

7984
main();

0 commit comments

Comments
 (0)