Update generate-index.mjs

This commit is contained in:
Mark Randall Havens △ The Empathic Technologist ⟁ Doctor Who 42 2025-11-08 15:34:32 -06:00 committed by GitHub
parent fdf1dfd7e2
commit 615da89e8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,10 +26,9 @@ function parseTitle(raw, ext) {
return null; return null;
} }
async function walk(relBase = "") { async function collectFiles(relBase = "", flat = []) {
const abs = path.join(ROOT, relBase); const abs = path.join(ROOT, relBase);
const entries = await fs.readdir(abs, { withFileTypes: true }); const entries = await fs.readdir(abs, { withFileTypes: true });
const dir = { type: "dir", name: path.posix.basename(relBase) || "", path: relBase, children: [] };
for (const e of entries) { for (const e of entries) {
if (e.name.startsWith(".")) continue; if (e.name.startsWith(".")) continue;
@ -38,8 +37,7 @@ async function walk(relBase = "") {
if (e.isDirectory()) { if (e.isDirectory()) {
const top = rel.split("/")[0]; const top = rel.split("/")[0];
if (STATIC_TOPLEVEL.has(top)) continue; if (STATIC_TOPLEVEL.has(top)) continue;
const child = await walk(rel); await collectFiles(rel, flat);
dir.children.push(child);
continue; continue;
} }
@ -50,7 +48,7 @@ async function walk(relBase = "") {
const title = parseTitle(raw, ext) || e.name; const title = parseTitle(raw, ext) || e.name;
const mtime = dateFromName(e.name) ?? st.mtimeMs; const mtime = dateFromName(e.name) ?? st.mtimeMs;
dir.children.push({ flat.push({
type: "file", type: "file",
name: e.name, name: e.name,
title, title,
@ -60,16 +58,14 @@ async function walk(relBase = "") {
mtime mtime
}); });
} }
return dir; return flat;
} }
(async () => { (async () => {
try { try {
const tree = await walk(); const flat = await collectFiles();
const flat = [];
(function flatten(n) { for (const c of n.children) { c.type === "file" ? flat.push(c) : flatten(c); } })(tree);
const sections = [...new Set(flat.map(f => f.path.split("/")[0]))]; const sections = [...new Set(flat.map(f => f.path.split("/")[0]))];
await fs.writeFile(OUT, JSON.stringify({ tree: tree.children, flat, sections }, null, 2)); await fs.writeFile(OUT, JSON.stringify({ flat, sections }, null, 2));
console.log(`index.json built with ${flat.length} files across ${sections.length} sections.`); console.log(`index.json built with ${flat.length} files across ${sections.length} sections.`);
} catch (e) { } catch (e) {
console.error("Build failed:", e); console.error("Build failed:", e);