66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
|
|
import { TECH_LOG_ROUTE_REGISTRY } from "../src/features/tech-log/contracts/tech-log-route-contract.ts";
|
|
|
|
const ROOT = path.resolve("src/features/tech-log/presentation");
|
|
const EXTENSIONS = new Set([".ts", ".tsx"]);
|
|
const TECH_LOG_ROUTE_ROOTS = new Set(
|
|
Object.values(TECH_LOG_ROUTE_REGISTRY)
|
|
.map(({ path: routePath }) => /^\/([^/:*]+)/.exec(routePath)?.[1])
|
|
.filter((segment): segment is string => Boolean(segment)),
|
|
);
|
|
|
|
type RouteLiteralViolation = Readonly<{
|
|
file: string;
|
|
line: number;
|
|
value: string;
|
|
}>;
|
|
|
|
const violations: RouteLiteralViolation[] = [];
|
|
|
|
function visitFile(filePath: string): void {
|
|
const source = fs.readFileSync(filePath, "utf8");
|
|
const withoutComments = source
|
|
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
.replace(/\/\/.*$/gm, "");
|
|
// Only strings beginning with a real TechLog route family are forbidden.
|
|
// Media paths (/media/...), Markdown command examples (/problem, /context),
|
|
// generated metadata paths, etc. are slash-prefixed strings too, but they
|
|
// are not navigation URLs and must not be classified as route duplication.
|
|
const pattern = /(["'`])(\/([A-Za-z0-9-]+))(?=[/?#"'`])/g;
|
|
for (const match of withoutComments.matchAll(pattern)) {
|
|
const routeRoot = match[3];
|
|
if (!routeRoot || !TECH_LOG_ROUTE_ROOTS.has(routeRoot)) continue;
|
|
|
|
const before = withoutComments.slice(0, match.index ?? 0);
|
|
const line = before.split("\n").length;
|
|
violations.push({
|
|
file: path.relative(process.cwd(), filePath),
|
|
line,
|
|
value: match[2] ?? match[0],
|
|
});
|
|
}
|
|
}
|
|
|
|
function walk(directory: string): void {
|
|
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
const filePath = path.join(directory, entry.name);
|
|
if (entry.isDirectory()) walk(filePath);
|
|
else if (EXTENSIONS.has(path.extname(entry.name))) visitFile(filePath);
|
|
}
|
|
}
|
|
|
|
walk(ROOT);
|
|
|
|
if (violations.length > 0) {
|
|
console.error("TechLog route source-of-truth check failed.");
|
|
console.error("Build internal TechLog URLs through publicRoutes/studioRoutes instead of presentation literals.");
|
|
for (const violation of violations) {
|
|
console.error(`- ${violation.file}:${violation.line} ${JSON.stringify(violation.value)}`);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log("TechLog route source-of-truth: PASS");
|