/** * Integrate vertical apps into portal App Router as real routes. * - apps/ebook/src → @ebook/* , routes under app/book * - apps/nomadweb/frontend/src → @community/* , routes under app/community * - apps/navmini/src → app/nav (client page) * No iframe / webview / reverse-proxy. */ 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 (ent.name === "node_modules" || ent.name === ".next" || ent.name === "dist") continue; const p = path.join(dir, ent.name); if (ent.isDirectory()) walk(p, out); else out.push(p); } return out; } function rewriteAlias(file, fromPrefix, toPrefix) { let t = fs.readFileSync(file, "utf8"); const before = t; // from '@/x' or "@/x" or from '@/x/y' const re = new RegExp(`([\"'])${fromPrefix}/`, "g"); t = t.replace(re, `$1${toPrefix}/`); if (t !== before) { fs.writeFileSync(file, t); return true; } return false; } function ensureDir(p) { fs.mkdirSync(p, { recursive: true }); } function relPosix(from, to) { return path.relative(from, to).split(path.sep).join("/"); } /** Copy app route files into portal, skipping root layout.tsx/globals.css (handled separately). */ function mirrorAppRoutes(srcAppDir, destAppDir, importAlias) { const files = walk(srcAppDir).filter((f) => { const base = path.basename(f); const rel = path.relative(srcAppDir, f); if (rel === "layout.tsx" || rel === "globals.css" || rel === "global.css") return false; if (rel === "favicon.ico") return false; return /\.(tsx|ts|jsx|js|css|mdx)$/.test(base); }); let n = 0; for (const src of files) { const rel = path.relative(srcAppDir, src); const dest = path.join(destAppDir, rel); ensureDir(path.dirname(dest)); // For page/layout/route/loading/error/template/default files: keep as real route modules. // Rewrite their @/ imports already done in source trees. let body = fs.readFileSync(src, "utf8"); // If this is a page that was under apps and already uses @ebook/@community, fine. // Also fix any remaining @/ that slipped through in this file only if still present. body = body.replace(/(["'])@\//g, `$1${importAlias}/`); fs.writeFileSync(dest, body); n++; } return n; } function stripRootChrome(layoutSrc, outPath, cssImport, extraNote) { let t = fs.readFileSync(layoutSrc, "utf8"); // Remove html/body wrappers — nested segment layout only. // Common patterns: return (...) // We'll write a handcrafted layout instead if stripping is too fragile. fs.writeFileSync(outPath, t); return t; } // --- 1) Rewrite aliases in source trees --- const ebookSrc = path.join(root, "apps/ebook/src"); const communitySrc = path.join(root, "apps/nomadweb/frontend/src"); let ebookRewrites = 0; for (const f of walk(ebookSrc).filter((x) => /\.(ts|tsx|js|jsx)$/.test(x))) { if (rewriteAlias(f, "@", "@ebook")) ebookRewrites++; } let communityRewrites = 0; for (const f of walk(communitySrc).filter((x) => /\.(ts|tsx|js|jsx)$/.test(x))) { if (rewriteAlias(f, "@", "@community")) communityRewrites++; } console.log(`alias rewrite ebook files=${ebookRewrites}, community files=${communityRewrites}`); // --- 2) Fix ebook content root to apps/ebook/content --- for (const f of walk(ebookSrc).filter((x) => /\.(ts|tsx)$/.test(x))) { let t = fs.readFileSync(f, "utf8"); const before = t; // path.join(process.cwd(), 'content' ...) → path.join(process.cwd(), 'apps/ebook/content' ...) t = t.replace( /path\.join\(\s*process\.cwd\(\)\s*,\s*['\"]content['\"]/g, "path.join(process.cwd(), 'apps/ebook/content'", ); t = t.replace( /join\(\s*process\.cwd\(\)\s*,\s*['\"]content['\"]/g, "join(process.cwd(), 'apps/ebook/content'", ); if (t !== before) { fs.writeFileSync(f, t); console.log("content path fix:", path.relative(root, f)); } } // --- 3) Mirror routes into portal app/ --- const bookDest = path.join(root, "app/book"); const communityDest = path.join(root, "app/community"); fs.rmSync(bookDest, { recursive: true, force: true }); fs.rmSync(communityDest, { recursive: true, force: true }); ensureDir(bookDest); ensureDir(communityDest); const bookCount = mirrorAppRoutes(path.join(ebookSrc, "app"), bookDest, "@ebook"); const communityCount = mirrorAppRoutes( path.join(communitySrc, "app"), communityDest, "@community", ); console.log(`mirrored book routes=${bookCount}, community routes=${communityCount}`); // --- 4) Write nested layouts (no html/body) --- const bookLayout = `import { AntdRegistry } from "@ant-design/nextjs-registry"; import { Providers } from "@ebook/components/Providers"; import { getSiteConfig } from "@ebook/lib/site-config.server"; import { getServerFeatures } from "@ebook/lib/features.server"; import { getLocale } from "@ebook/lib/i18n/server"; import "@ebook/app/globals.css"; export default async function BookLayout({ children, }: { children: React.ReactNode; }) { const locale = await getLocale(); const site = getSiteConfig(); const f = getServerFeatures(); return (
{f.analytics.enabled ? : null} {f.ads.enabled && f.ads.clientId ? : null} {children} {f.feedback.enabled ? ( ) : null}