99} from "elysia" ;
1010import type { BaseMacro } from "elysia/dist/types" ;
1111import {
12+ addRelativeIfNotDot ,
1213 fixSlashes ,
1314 getPath ,
1415 sortByNestedParams ,
@@ -44,6 +45,7 @@ export interface IAutoloadOptions {
4445 types ?: ITypesOptions | true ;
4546}
4647
48+ const DIR_ROUTES_DEFAULT = "./routes" ;
4749const TYPES_OUTPUT_DEFAULT = "./routes-types.ts" ;
4850const TYPES_TYPENAME_DEFAULT = "Routes" ;
4951const TYPES_OBJECT_DEFAULT = {
@@ -55,8 +57,9 @@ export async function autoload(options: IAutoloadOptions = {}) {
5557 // autoload-plugin-sources
5658 const fileSources = { } ;
5759
58- const { pattern, dir , prefix, schema } = options ;
60+ const { pattern, prefix, schema } = options ;
5961
62+ const dir = options . dir ?? DIR_ROUTES_DEFAULT ;
6063 // some strange code to provide defaults
6164 const types : Omit < ITypesOptions , "output" > & { output : string [ ] } =
6265 options . types && options . types !== true
@@ -72,7 +75,7 @@ export async function autoload(options: IAutoloadOptions = {}) {
7275 }
7376 : TYPES_OBJECT_DEFAULT ;
7477
75- const directoryPath = getPath ( dir || "./routes" ) ;
78+ const directoryPath = getPath ( dir ) ;
7679
7780 if ( ! fs . existsSync ( directoryPath ) )
7881 throw new Error ( `Directory ${ directoryPath } doesn't exists` ) ;
@@ -117,26 +120,30 @@ export async function autoload(options: IAutoloadOptions = {}) {
117120 if ( types ) paths . push ( fullPath . replace ( directoryPath , "" ) ) ;
118121 }
119122
120- // esbuild-plugin-autoload remove-start
121123 if ( types ) {
122- const imports : string [ ] = paths . map (
123- ( x , index ) =>
124- `import type Route${ index } from "${ (
125- directoryPath + x . replace ( ".ts" , "" ) . replace ( ".tsx" , "" )
126- ) . replace ( / \\ / gu, "/" ) } ";`,
127- ) ;
128-
129124 for await ( const outputPath of types . output ) {
125+ const outputAbsolutePath = getPath ( outputPath ) ;
126+
127+ const imports : string [ ] = paths . map (
128+ ( x , index ) =>
129+ `import type Route${ index } from "${ addRelativeIfNotDot (
130+ path
131+ . relative (
132+ path . dirname ( outputAbsolutePath ) ,
133+ directoryPath + x . replace ( ".ts" , "" ) . replace ( ".tsx" , "" ) ,
134+ )
135+ . replace ( / \\ / gu, "/" ) ,
136+ ) } ";`,
137+ ) ;
138+
130139 await Bun . write (
131- getPath ( outputPath ) ,
140+ outputAbsolutePath ,
132141 [
133142 `import type { ElysiaWithBaseUrl } from "elysia-autoload";` ,
134143 imports . join ( "\n" ) ,
135144 "" ,
136145 ! types . useExport ? "declare global {" : "" ,
137- ` export type ${
138- ! types . typeName ? TYPES_TYPENAME_DEFAULT : types . typeName
139- } = ${ paths
146+ ` export type ${ types . typeName } = ${ paths
140147 . map (
141148 ( x , index ) =>
142149 `ElysiaWithBaseUrl<"${
@@ -151,7 +158,6 @@ export async function autoload(options: IAutoloadOptions = {}) {
151158 }
152159 }
153160
154- // esbuild-plugin-autoload remove-end
155161 return plugin ;
156162}
157163export * from "./types" ;
0 commit comments