// Writes pages/pages.json from the file-based pages under src/pages/ and the // redirect table in src/router/redirects.ts. // // 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 { readFileSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import ts from "typescript"; import { createRoutesContext, resolveOptions } from "vue-router/unplugin"; import { redirects } from "../src/router/redirects.ts"; type Meta = { label?: string; description?: string; nav?: boolean }; export type Page = { path: string; label: string; description: string }; const here = dirname(fileURLToPath(import.meta.url)); const uiRoot = join(here, ".."); // 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(":"); function readDefinePageMeta(file: string): Meta | undefined { const sfc = readFileSync(file, "utf8"); const scriptMatch = sfc.match(/]*\bsetup\b[^>]*>([\s\S]*?)<\/script>/); if (!scriptMatch) return undefined; const sf = ts.createSourceFile(file, scriptMatch[1], ts.ScriptTarget.Latest, true); let meta: Meta | undefined; const propName = (name: ts.PropertyName): string | undefined => { if (ts.isIdentifier(name) || ts.isStringLiteral(name)) return name.text; return undefined; }; const visit = (node: ts.Node): void => { if ( ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "definePage" && node.arguments.length > 0 && ts.isObjectLiteralExpression(node.arguments[0]) ) { for (const prop of node.arguments[0].properties) { if ( ts.isPropertyAssignment(prop) && propName(prop.name) === "meta" && ts.isObjectLiteralExpression(prop.initializer) ) { const parsed: Meta = {}; for (const field of prop.initializer.properties) { if (!ts.isPropertyAssignment(field)) continue; const key = propName(field.name); if (key === "label" && ts.isStringLiteral(field.initializer)) { parsed.label = field.initializer.text; } else if (key === "description" && ts.isStringLiteral(field.initializer)) { parsed.description = field.initializer.text; } else if (key === "nav") { if (field.initializer.kind === ts.SyntaxKind.FalseKeyword) parsed.nav = false; if (field.initializer.kind === ts.SyntaxKind.TrueKeyword) parsed.nav = true; } } meta = parsed; } } } ts.forEachChild(node, visit); }; visit(sf); return meta; } const collected: Page[] = []; const opts = resolveOptions({ root: uiRoot, routesFolder: "src/pages", exclude: ["**/components/**"], dts: false, extendRoute(route) { const file = route.components.get("default"); if (!file) return; const path = route.fullPath; if (!path || dynamic(path)) return; const meta = readDefinePageMeta(file); if (meta?.label && meta.nav !== false) { collected.push({ path, label: meta.label, description: meta.description ?? "", }); } }, }); const ctx = createRoutesContext(opts); await ctx.scanPages(false); ctx.generateRoutes(); for (const route of redirects) { const meta = route.meta as Meta | undefined; if (meta?.label && meta.nav !== false && route.path && !dynamic(route.path)) { collected.push({ path: route.path, label: meta.label, description: meta.description ?? "", }); } } // Shell routes and their index children share a path (for example /org and // /controls), so the first label for a path wins rather than listing it twice. const seen = new Set(); const pages = collected .filter((page) => { if (seen.has(page.path)) return false; seen.add(page.path); return true; }) .sort((a, b) => a.path.localeCompare(b.path)); const target = join(uiRoot, "pages", "pages.json"); writeFileSync(target, `${JSON.stringify(pages, null, 2)}\n`); console.log(`pages: ${pages.length} written to ${target}`);