nomadro/scripts/prefix-routes.mjs
eric 9928c5185b Mount nav/book/community as native App Router routes in one Next process.
Replace reverse-proxy/iframe approach with @ebook/@community/@navmini imports under /nav /book /community so localhost:3000 serves all three products directly.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 03:49:16 -05:00

112 lines
2.9 KiB
JavaScript

/**
* Prefix in-app absolute paths for mounted vertical products.
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
function walk(dir, out = []) {
if (!fs.existsSync(dir)) return out;
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
if (["node_modules", ".next", "dist"].includes(ent.name)) continue;
const p = path.join(dir, ent.name);
if (ent.isDirectory()) walk(p, out);
else if (/\.(ts|tsx|js|jsx)$/.test(ent.name)) out.push(p);
}
return out;
}
function prefixPaths(file, prefix, { also = [], skip = [] } = {}) {
let t = fs.readFileSync(file, "utf8");
const before = t;
const shouldSkip = (p) => {
if (p.startsWith("http") || p.startsWith("//") || p.startsWith("#")) return true;
if (p.startsWith(prefix)) return true;
for (const s of skip) if (p === s || p.startsWith(s)) return true;
return false;
};
// href="/x", href='/x', to="/x", action="/x"
t = t.replace(
/\b(href|to|action)=(["'])\/([^"']*)\2/g,
(m, attr, q, rest) => {
const p = "/" + rest;
if (shouldSkip(p)) return m;
return `${attr}=${q}${prefix}${p === "/" ? "" : p}${q}`;
},
);
// router.push('/x') / replace('/x') / redirect('/x') / permanentRedirect('/x')
t = t.replace(
/\b(push|replace|redirect|permanentRedirect)\(\s*(["'])\/([^"']*)\2/g,
(m, fn, q, rest) => {
const p = "/" + rest;
if (shouldSkip(p)) return m;
return `${fn}(${q}${prefix}${p === "/" ? "" : p}${q}`;
},
);
// template literals: `/foo` used in push(`/foo`) — common pattern
t = t.replace(
/\b(push|replace|redirect|permanentRedirect)\(\s*`\/([^`]*)`/g,
(m, fn, rest) => {
const p = "/" + rest;
if (shouldSkip(p.split(/[?#]/)[0])) return m;
if (rest.startsWith(prefix.slice(1))) return m;
return `${fn}(\`${prefix}/${rest}\``;
},
);
for (const extra of also) {
const [from, to] = extra;
t = t.split(from).join(to);
}
if (t !== before) {
fs.writeFileSync(file, t);
return true;
}
return false;
}
const ebookSkip = ["/book", "/nav", "/community", "/_next", "/icons", "/api", "/favicon"];
const communitySkip = [
"/book",
"/nav",
"/community",
"/_next",
"/icons",
"/api",
"/favicon",
"/manifest",
];
let n1 = 0;
for (const f of [
...walk(path.join(root, "apps/ebook/src")),
...walk(path.join(root, "app/book")),
]) {
if (
prefixPaths(f, "/book", {
skip: ebookSkip,
also: [
// already-prefixed double fix guard not needed if skip works
],
})
)
n1++;
}
let n2 = 0;
for (const f of [
...walk(path.join(root, "apps/nomadweb/frontend/src")),
...walk(path.join(root, "app/community")),
]) {
if (prefixPaths(f, "/community", { skip: communitySkip })) n2++;
}
console.log(`prefixed ebook files=${n1}, community files=${n2}`);