import fs from "fs"; import path from "path"; export type RouteNode = { name: string; path: string; children?: RouteNode[]; }; export function getRouteTree(): RouteNode[] { const appDir = path.join(process.cwd(), "app"); function walk(currentDir: string, baseRoute = ""): RouteNode[] { const entries = fs.readdirSync(currentDir, { withFileTypes: true }); return entries .filter((e) => { if (!e.isDirectory()) return false; // EXCLUDE folders that should not be routes if (e.name.startsWith("_")) return false; if (e.name.startsWith("(")) return false; if (e.name.startsWith("[")) return false; // hide [...path] // Only include if folder HAS a page.tsx OR contains nested route pages const folderPath = path.join(currentDir, e.name); const hasPage = fs.existsSync(path.join(folderPath, "page.tsx")); const hasNestedPages = fs .readdirSync(folderPath) .some((child) => { const full = path.join(folderPath, child); return ( fs.lstatSync(full).isDirectory() && fs.existsSync(path.join(full, "page.tsx")) ); }); return hasPage || hasNestedPages; }) .map((folder) => { const folderPath = path.join(currentDir, folder.name); const routePath = `${baseRoute}/${folder.name}`; const children = walk(folderPath, routePath); return { name: folder.name, path: routePath, children, }; }); } return walk(appDir, ""); }