refactor(ui): adopt vue-router file-based routing under src/pages

This commit is contained in:
sam committed 2026-11-27 16:29:38 +11:00
1 parent 1e105ff4c8
commit 936c5267f0
62 files changed
+2378 -963

No files matched your search

+101 -35
View File
@@ -1,63 +1,129 @@
// Writes pages/pages.json from the router's own route table.
// 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 { writeFileSync } from "node:fs";
import { readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { routes } from "../src/router/routes.ts";
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 };
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}`;
};
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(":");
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({
function readDefinePageMeta(file: string): Meta | undefined {
const sfc = readFileSync(file, "utf8");
const scriptMatch = sfc.match(/<script\b[^>]*\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",
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: route.meta.label,
description: route.meta.description ?? "",
label: meta.label,
description: 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<string>();
return pages.filter((page) => {
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<string>();
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 pages = unique(collect(routes as Record_[]));
const here = dirname(fileURLToPath(import.meta.url));
const target = join(here, "..", "pages", "pages.json");
const target = join(uiRoot, "pages", "pages.json");
writeFileSync(target, `${JSON.stringify(pages, null, 2)}\n`);
console.log(`pages: ${pages.length} written to ${target}`);