shoc-frontend-new/scripts/fix-router-paths.ts

23 lines
802 B
TypeScript

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
export const defaultRouterFile = path.resolve(scriptDir, "../src/router.ts");
/** Strip `/src/app` prefix from generouted Path types — views live under src/app/, not src/pages/. */
export function fixGeneroutedRouterPaths(routerFile = defaultRouterFile) {
const content = fs.readFileSync(routerFile, "utf8");
const fixed = content.replace(/`\/+src\/app/g, "`").replace(/"\/+src\/app/g, '"/');
if (fixed !== content) {
fs.writeFileSync(routerFile, fixed);
}
}
const isMain =
process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url));
if (isMain) {
fixGeneroutedRouterPaths();
}