11import fs from 'fs' ;
2- import path from 'path' ;
3- import { fileURLToPath } from 'url' ;
2+ import path , { resolve } from 'path' ;
43import { getRoutes } from './utils' ;
54type TreeNode = {
65 type : 'static' | 'param' ;
@@ -10,16 +9,10 @@ type TreeNode = {
109
1110type Tree = Record < string , TreeNode > ;
1211
13- const __filename = fileURLToPath ( import . meta. url ) ; // get the resolved path to the file
14- const __dirname = path . dirname ( __filename ) ; // get the name of the directory
15-
16- const targertDir = 'src/RouteManifest' ;
17-
1812// Ensure the .route directory exists
19- function ensureRouteDirectory ( ) {
20- const dir = path . join ( __dirname , targertDir ) ;
21- if ( ! fs . existsSync ( dir ) ) {
22- fs . mkdirSync ( dir ) ;
13+ function ensureRouteDirectory ( targertDir : string ) {
14+ if ( ! fs . existsSync ( targertDir ) ) {
15+ fs . mkdirSync ( targertDir ) ;
2316 }
2417}
2518
@@ -123,7 +116,6 @@ function buildRoutesFromTree(tree: Tree, parentPath = '', depth = 2) {
123116async function generateRoutesFunction ( customRoutes : string [ ] ) {
124117 const fileRoutes = await getRoutes ( ) ;
125118 const routes = [ ...fileRoutes , ...customRoutes ] ;
126- console . log ( 'routes' , routes ) ;
127119 const tree = buildRouteTree ( routes ) ; // Build the tree from routes
128120
129121 const outputJS = [ ] ;
@@ -162,9 +154,9 @@ async function generateRoutesFunction(customRoutes: string[]) {
162154}
163155
164156// Function to write the JS file
165- async function generateJSFile ( routes : string [ ] ) {
166- const jsFilePath = path . join ( __dirname , targertDir , 'index.js' ) ;
167- const dtsFilePath = path . join ( __dirname , targertDir , 'index.d.ts' ) ;
157+ async function generateJSFile ( outDir : string , routes : string [ ] ) {
158+ const jsFilePath = path . join ( outDir , 'index.js' ) ;
159+ const dtsFilePath = path . join ( outDir , 'index.d.ts' ) ;
168160
169161 const [ routesFunctionString , typeDeclarationString ] = await generateRoutesFunction ( routes ) ;
170162
@@ -174,14 +166,12 @@ async function generateJSFile(routes: string[]) {
174166 if ( ! typeDeclarationString )
175167 throw new Error ( 'Could not create type declaration for Routes function' ) ;
176168 fs . writeFileSync ( dtsFilePath , typeDeclarationString , 'utf-8' ) ;
177-
178- console . log ( `Generated ${ jsFilePath } ` ) ;
179169}
180170
181171// Main function to generate the route manifest
182- async function generateRouteManifest ( routes : string [ ] = [ ] ) {
183- ensureRouteDirectory ( ) ;
184- await generateJSFile ( routes ) ;
172+ async function generateRouteManifest ( outDir : string , routes : string [ ] = [ ] ) {
173+ ensureRouteDirectory ( outDir ) ;
174+ await generateJSFile ( outDir , routes ) ;
185175}
186176
187177export { generateRouteManifest , generateRoutesFunction } ;
0 commit comments