|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +const PAGES_DIR = path.join(process.cwd(), 'src/pages'); |
| 5 | + |
| 6 | +const fileNameToPath = (fileName: string) => { |
| 7 | + if (fileName === 'Main.tsx') return '/'; |
| 8 | + |
| 9 | + const specialCases: Record<string, string> = { |
| 10 | + 'NotFound.tsx': '/404', |
| 11 | + 'BadGateway.tsx': '/502', |
| 12 | + 'NoticeDetail.tsx': '/notice/detail', |
| 13 | + }; |
| 14 | + |
| 15 | + if (fileName in specialCases) { |
| 16 | + return specialCases[fileName]; |
| 17 | + } |
| 18 | + |
| 19 | + return fileName |
| 20 | + .replace(/\.(tsx|jsx)$/, '') |
| 21 | + .replace(/([A-Z])/g, (_match, letter, offset) => { |
| 22 | + return offset === 0 ? letter.toLowerCase() : letter.toLowerCase(); |
| 23 | + }) |
| 24 | + .toLowerCase() |
| 25 | + .replace(/\[(.+)\]/, ':$1'); |
| 26 | +}; |
| 27 | + |
| 28 | +const generateRoutes = () => { |
| 29 | + const routes: { path: string; component: string }[] = []; |
| 30 | + |
| 31 | + const scanDir = (dir: string) => { |
| 32 | + fs.readdirSync(dir).forEach((file) => { |
| 33 | + const filePath = path.join(dir, file); |
| 34 | + |
| 35 | + if (fs.statSync(filePath).isDirectory()) { |
| 36 | + scanDir(filePath); |
| 37 | + |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (!file.match(/\.(tsx|jsx)$/)) return; |
| 42 | + |
| 43 | + const relativePath = path.relative(PAGES_DIR, filePath); |
| 44 | + const routePath = fileNameToPath(relativePath); |
| 45 | + |
| 46 | + routes.push({ |
| 47 | + path: routePath.startsWith('/') ? routePath : `/${routePath}`, |
| 48 | + component: relativePath.replace(/\.tsx$/, ''), |
| 49 | + }); |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + scanDir(PAGES_DIR); |
| 54 | + |
| 55 | + routes.push({ |
| 56 | + path: '/*', |
| 57 | + component: 'NotFound', |
| 58 | + }); |
| 59 | + |
| 60 | + const GENERATED_DIR = path.join(process.cwd(), 'src/__generated__'); |
| 61 | + |
| 62 | + if (fs.existsSync(GENERATED_DIR)) { |
| 63 | + fs.rmSync(GENERATED_DIR, { recursive: true }); |
| 64 | + } |
| 65 | + fs.mkdirSync(GENERATED_DIR, { recursive: true }); |
| 66 | + |
| 67 | + const typesContent = `/* generated file by scripts/generateRoutes.ts -- do not edit */ |
| 68 | +/* authored by tooooo1 */ |
| 69 | +
|
| 70 | +/* tslint:disable */ |
| 71 | +/* eslint-disable */ |
| 72 | +
|
| 73 | +type BaseRoutePath = ${routes.map((route) => `'${route.path}'`).join(' | ')}; |
| 74 | +export type RoutePath = BaseRoutePath | \`\${BaseRoutePath}?\${string}\`; |
| 75 | +export type RoutePathWithQuery<T extends string> = \`\${BaseRoutePath}?\${T}\`; |
| 76 | +`; |
| 77 | + |
| 78 | + const routesContent = `/* generated file by scripts/generateRoutes.ts -- do not edit */ |
| 79 | +/* authored by tooooo1 */ |
| 80 | +
|
| 81 | +/* tslint:disable */ |
| 82 | +/* eslint-disable */ |
| 83 | +import { createBrowserRouter, type NavigateOptions } from 'react-router'; |
| 84 | +import { RoutePath } from './routes.types'; |
| 85 | +
|
| 86 | +import Layout from '../components/Etc/Layout'; |
| 87 | +
|
| 88 | +export const router = createBrowserRouter([ |
| 89 | + { |
| 90 | + element: <Layout />, |
| 91 | + children: [ |
| 92 | + ${routes |
| 93 | + .map( |
| 94 | + (route) => `{ |
| 95 | + path: '${route.path}' as RoutePath, |
| 96 | + lazy: async () => { |
| 97 | + const { default: Component } = await import('../pages/${route.component}'); |
| 98 | +
|
| 99 | + return { Component }; |
| 100 | + } |
| 101 | + }`, |
| 102 | + ) |
| 103 | + .join(',\n')} |
| 104 | + ] |
| 105 | + } |
| 106 | +]); |
| 107 | +
|
| 108 | +declare module 'react-router' { |
| 109 | + export function useNavigate(): (path: RoutePath, options?: NavigateOptions) => void; |
| 110 | +} |
| 111 | +`; |
| 112 | + |
| 113 | + fs.writeFileSync(path.join(GENERATED_DIR, 'routes.types.ts'), typesContent); |
| 114 | + fs.writeFileSync(path.join(GENERATED_DIR, 'routes.generated.tsx'), routesContent.trim()); |
| 115 | + |
| 116 | + console.log('\n🚀 Routes generated successfully!\n'); |
| 117 | + console.log('📍 Available routes:'); |
| 118 | + routes.forEach((route) => { |
| 119 | + console.log(` ${route.path.padEnd(20)} 🔗`); |
| 120 | + }); |
| 121 | + console.log('\n✨ Route generation completed!\n'); |
| 122 | +}; |
| 123 | + |
| 124 | +generateRoutes(); |
0 commit comments