/ concept-collection / fastandaccurate-results
Sign in
concept-collection / fastandaccurate-results
fastandaccurate-results / scripts / build-index.mjs
28 lines · 1.1 KBBlameHistoryRaw
1/**
2 * Regenerate index.json from the files under results/. The site fetches
3 * index.json and then each listed file; a result not in the index is
4 * invisible. Run with --check to verify without writing (CI does this).
5 */
6import { readFile, writeFile } from "node:fs/promises";
7import { glob } from "node:fs/promises";
8import { join, relative } from "node:path";
10const root = new URL("..", import.meta.url).pathname;
11const files = [];
12for await (const entry of glob("results/**/*.json", { cwd: root })) {
13 files.push(relative(root, join(root, entry)).replaceAll("\\", "/"));
15files.sort();
16const index = `${JSON.stringify({ files }, null, 2)}\n`;
18if (process.argv.includes("--check")) {
19 const existing = await readFile(join(root, "index.json"), "utf8").catch(() => "");
20 if (existing !== index) {
21 console.error("index.json is out of date; run: node scripts/build-index.mjs");
22 process.exit(1);
23 }
24 console.log(`index.json up to date (${files.length} results)`);
25} else {
26 await writeFile(join(root, "index.json"), index);
27 console.log(`index.json written (${files.length} results)`);
moveopenescclose