1#!/usr/bin/env bun
2// build-routes.ts - Run this at build time
3
4import { Glob } from "bun";
5import { writeFileSync } from "fs";
6
7const routeImports: string[] = [];
8const routeMap: string[] = [];
9
10// Use Bun.Glob to find all route files
11const glob = new Glob("*.ts");
12
13for await (const file of glob.scan("./src/routes")) {
14 // Skip index.ts
15 if (file === "index.ts") continue;
16
17 const routeName = file.replace(".ts", "");
18 const routePath = `/${routeName}`;
19
20 // Generate import statement
21 routeImports.push(`import * as ${routeName}Route from "./${routeName}";`);
22
23 // Generate route map entry
24 routeMap.push(` "${routePath}": ${routeName}Route`);
25}
26
27// Generate the complete index.ts content
28const indexContent = `// Auto-generated route index
29${routeImports.join("\n")}
30
31export const routes: Record<
32 string,
33 Record<string, Bun.RouterTypes.RouteHandler<string>>
34> = {
35${routeMap.join(",\n")}
36};
37
38export default routes;
39`;
40
41// Write the generated index.ts file
42writeFileSync("./src/routes/index.ts", indexContent);