/**
 * `wrnexus` typed-routes codegen. Scans the app's pages and writes
 * `app/routes.gen.ts` (a `Routes` map + `href()` builder). Run at `wrnexus dev`
 * startup; also exposed via `wrnexus generate routes`.
 */

import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { buildRouter, generateRoutesFile } from "@wrnexus/router";

/** Regenerate `app/routes.gen.ts`. Returns the number of page routes. */
export function regenerateRoutes(appDir: string): number {
  const router = buildRouter(appDir);
  const code = generateRoutesFile(router.pages);
  writeFileSync(join(appDir, "routes.gen.ts"), code, "utf8");
  return router.pages.length;
}
