thefoldwithin-earth/tools/generate-index.mjs

105 lines
2.8 KiB
JavaScript
Raw Normal View History

2025-11-08 11:23:21 -06:00
#!/usr/bin/env node
2025-11-08 11:29:10 -06:00
import { promises as fs } from "fs";
2025-11-08 09:05:04 -06:00
import path from "path";
2025-11-08 14:34:15 -06:00
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, "../public");
2025-11-08 11:23:21 -06:00
const OUT = path.join(ROOT, "index.json");
2025-11-08 11:29:10 -06:00
const STATIC_TOPLEVEL = new Set(["about", "contact", "legal"]);
2025-11-08 11:23:21 -06:00
const MAX_BYTES = 64 * 1024;
2025-11-08 10:37:54 -06:00
2025-11-08 11:29:10 -06:00
function dateFromName(name) {
const m = name.match(/^(\d{4}-\d{2}-\d{2})/);
return m ? new Date(m[0]).getTime() : null;
2025-11-08 10:37:54 -06:00
}
2025-11-08 14:34:15 -06:00
2025-11-08 11:29:10 -06:00
async function readHead(abs) {
const fh = await fs.open(abs, "r");
const buf = Buffer.alloc(MAX_BYTES);
const { bytesRead } = await fh.read(buf, 0, MAX_BYTES, 0);
2025-11-08 10:37:54 -06:00
await fh.close();
2025-11-08 11:29:10 -06:00
return buf.slice(0, bytesRead).toString("utf8");
2025-11-08 10:37:54 -06:00
}
2025-11-08 14:34:15 -06:00
2025-11-08 11:29:10 -06:00
function parseTitle(raw, ext) {
2025-11-08 14:34:15 -06:00
if (ext === ".md") return raw.match(/^\s*#\s+(.+?)\s*$/m)?.[1]?.trim();
if (ext === ".html")
return raw.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1]?.trim();
2025-11-08 10:37:54 -06:00
return null;
}
2025-11-08 11:29:10 -06:00
async function walk(relBase = "") {
2025-11-08 11:30:41 -06:00
const abs = path.join(ROOT, relBase);
const entries = await fs.readdir(abs, { withFileTypes: true });
2025-11-08 14:34:15 -06:00
const dir = {
type: "dir",
name: path.posix.basename(relBase) || "",
path: relBase,
children: [],
};
2025-11-08 11:30:41 -06:00
for (const e of entries) {
2025-11-08 14:34:15 -06:00
if (e.name.startsWith(".")) continue; // skip hidden
2025-11-08 11:30:41 -06:00
const rel = path.posix.join(relBase, e.name);
const absPath = path.join(ROOT, rel);
2025-11-08 14:34:15 -06:00
2025-11-08 11:30:41 -06:00
if (e.isDirectory()) {
const top = rel.split("/")[0];
if (STATIC_TOPLEVEL.has(top)) continue;
2025-11-08 14:34:15 -06:00
dir.children.push(await walk(rel));
2025-11-08 11:30:41 -06:00
continue;
}
2025-11-08 14:34:15 -06:00
2025-11-08 11:30:41 -06:00
const ext = path.posix.extname(e.name).toLowerCase();
if (![".md", ".html"].includes(ext)) continue;
2025-11-08 14:34:15 -06:00
2025-11-08 11:30:41 -06:00
const st = await fs.stat(absPath);
2025-11-08 14:34:15 -06:00
let title = e.name;
try {
const raw = await readHead(absPath);
title = parseTitle(raw, ext) || e.name;
} catch {
/* ignore parse errors */
}
2025-11-08 11:30:41 -06:00
const mtime = dateFromName(e.name) ?? st.mtimeMs;
dir.children.push({
type: "file",
name: e.name,
title,
path: rel,
ext,
pinned: rel.startsWith("pinned/"),
2025-11-08 14:34:15 -06:00
mtime,
2025-11-08 11:30:41 -06:00
});
}
2025-11-08 14:34:15 -06:00
// sort newest first by mtime
dir.children.sort((a, b) => (b.mtime ?? 0) - (a.mtime ?? 0));
2025-11-08 11:30:41 -06:00
return dir;
}
(async () => {
try {
const tree = await walk();
const flat = [];
2025-11-08 14:34:15 -06:00
(function flatten(n) {
for (const c of n.children) {
if (c.type === "file") flat.push(c);
else flatten(c);
}
})(tree);
const sections = [...new Set(flat.map((f) => f.path.split("/")[0]))];
await fs.writeFile(
OUT,
JSON.stringify({ tree: tree.children, flat, sections }, null, 2)
);
console.log(
`✅ index.json built with ${flat.length} files across ${sections.length} sections.`
);
2025-11-08 11:30:41 -06:00
} catch (e) {
2025-11-08 14:34:15 -06:00
console.error("❌ Build failed:", e);
2025-11-08 11:30:41 -06:00
process.exit(1);
}
})();