Files
8bitworkshop/scripts/urustmods.mjs
T

62 lines
2.4 KiB
JavaScript

// Regenerate src/common/urustmods.ts from src/worker/fs/urust-fs.zip.
//
// npm run gen-urustmods
//
// The zip is what the editor can open: the rs8 sources urust embeds, copied
// out so the HeaderView and the search can read them. The editor's `use
// rs8::<module>::<child>` links need the same module/child split, and a hand
// list drifts (gfx, sort and gfx::span were missing), so the table is
// generated from the zip and test/unit/testurust.ts fails if it is stale.
//
// `rs8/lib.rs` is the rustc host shim, not a module a program can name.
import { readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import JSZip from "jszip";
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
const zipfile = join(root, "src/worker/fs/urust-fs.zip");
const outfile = join(root, "src/common/urustmods.ts");
const zip = await JSZip.loadAsync(readFileSync(zipfile));
const modules = new Map();
for (const entry of Object.keys(zip.files)) {
if (zip.files[entry].dir) continue;
const top = /^rs8\/([^/]+)\.rs$/.exec(entry);
if (top) {
if (top[1] !== "lib") modules.set(top[1], modules.get(top[1]) || []);
continue;
}
const nested = /^rs8\/([^/]+)\/([^/]+)\.rs$/.exec(entry);
if (nested) {
const children = modules.get(nested[1]) || [];
children.push(nested[2]);
modules.set(nested[1], children);
}
}
if (modules.size === 0) throw new Error(`${zipfile}: no rs8 modules`);
const names = [...modules.keys()].sort();
const quote = (s) => JSON.stringify(s);
const rows = (list, sep) => list.map(quote).join(sep);
let text = `// Generated by scripts/urustmods.mjs from src/worker/fs/urust-fs.zip.
// Do not edit: run \`npm run gen-urustmods\` after the zip changes.
//
// The modules urust embeds (and the editor can open). A \`use rs8::<module>\`
// or \`use rs8::<module>::<child>\` link resolves here; test/unit/testurust.ts
// checks this table against the zip both ways, so an unlisted file fails.
export const URUST_RS8_MODULES: string[] = [
${names.map((n) => ` ${quote(n)},`).join("\n")}
];
export const URUST_RS8_CHILDREN: { [module: string]: string[] } = {
${names
.filter((n) => modules.get(n).length)
.map((n) => ` ${n}: [${rows(modules.get(n).sort(), ", ")}],`)
.join("\n")}
};
`;
writeFileSync(outfile, text);
console.log(`wrote ${outfile}: ${names.length} modules, ${names.reduce((n, m) => n + modules.get(m).length, 0)} children`);