// Writes pages/pages.json from the router's own route table. // // The dashboard's pages are the routes it serves, so deriving the catalogue // from them is what stops the assistant naming a page the dashboard lost or // missing one it gained. `task pages:check` fails when the committed file is // stale, which is what makes a new route carry a catalogue entry. import { writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { routes } from "../src/router/routes.ts"; type Meta = { label?: string; description?: string; nav?: boolean }; type Record_ = { path: string; meta?: Meta; children?: Record_[]; }; export type Page = { path: string; label: string; description: string }; const join_ = (parent: string, path: string): string => { if (path.startsWith("/")) return path; if (path === "") return parent; return `${parent.replace(/\/$/, "")}/${path}`; }; // dynamic is true for a route the operator reaches with an id in it; the // assistant cannot name one, so it stays out of the catalogue. const dynamic = (path: string): boolean => path.includes(":"); const collect = (records: Record_[], parent = "", out: Page[] = []): Page[] => { for (const route of records) { const path = join_(parent, route.path); if (route.meta?.label && route.meta.nav !== false && path && !dynamic(path)) { out.push({ path, label: route.meta.label, description: route.meta.description ?? "", }); } if (route.children) collect(route.children, path, out); } return out; }; // The org shell and its index child both answer at /org, so the first label // for a path wins rather than the catalogue carrying the page twice. const unique = (pages: Page[]): Page[] => { const seen = new Set(); return pages.filter((page) => { if (seen.has(page.path)) return false; seen.add(page.path); return true; }); }; const pages = unique(collect(routes as Record_[])); const here = dirname(fileURLToPath(import.meta.url)); const target = join(here, "..", "pages", "pages.json"); writeFileSync(target, `${JSON.stringify(pages, null, 2)}\n`); console.log(`pages: ${pages.length} written to ${target}`);