55 writeFileSync ,
66 rmSync ,
77 existsSync ,
8- cpSync
8+ cpSync ,
9+ exists
910} from 'fs'
1011import { TypeBox } from '@sinclair/typemap'
1112
@@ -46,6 +47,14 @@ interface OpenAPIGeneratorOptions {
4647 * @default process.cwd()
4748 */
4849 projectRoot ?: string
50+
51+ /**
52+ * Override output path
53+ *
54+ * Under any circumstance, that Elysia failed to find a correct schema,
55+ * Put your own schema in this path
56+ */
57+ overrideOutputPath ?( tempDir : string ) : string
4958}
5059
5160/**
@@ -66,7 +75,8 @@ export const fromTypes =
6675 {
6776 tsconfigPath = 'tsconfig.json' ,
6877 instanceName,
69- projectRoot = process . cwd ( )
78+ projectRoot = process . cwd ( ) ,
79+ overrideOutputPath
7080 } : OpenAPIGeneratorOptions = { }
7181 ) =>
7282 ( ) => {
@@ -107,16 +117,22 @@ export const fromTypes =
107117 exec ( `tsc` , tmpRoot )
108118
109119 try {
110- const declaration = readFileSync (
111- join (
120+ const fileName = targetFilePath
121+ . replace ( / .t s x $ / , '.ts' )
122+ . replace ( / .t s $ / , '.d.ts' )
123+
124+ let targetFile =
125+ overrideOutputPath ?.( tmpRoot ) ?? join ( tmpRoot , 'dist' , fileName )
126+
127+ // Sometime TypeScript doesn't include the first level directory, eg. src/file.ts -> dist/file.d.ts
128+ if ( ! existsSync ( targetFile ) )
129+ targetFile = join (
112130 tmpRoot ,
113131 'dist' ,
114- targetFilePath
115- . replace ( / .t s x $ / , '.ts' )
116- . replace ( / .t s $ / , '.d.ts' )
117- ) ,
118- 'utf8'
119- )
132+ fileName . slice ( fileName . indexOf ( '/' ) + 1 )
133+ )
134+
135+ const declaration = readFileSync ( targetFile , 'utf8' )
120136
121137 // Check just in case of race-condition
122138 if ( existsSync ( tmpRoot ) )
@@ -184,7 +200,7 @@ export const fromTypes =
184200
185201 return routes
186202 } catch ( error ) {
187- console . warn ( 'Failed to generate OpenAPI schema' )
203+ console . warn ( '[@elysiajs/openapi/gen] Failed to generate OpenAPI schema' )
188204 console . warn ( error )
189205
190206 return
0 commit comments