1
1
import path from 'path' ;
2
2
import fs from 'fs' ;
3
3
import type { CommonOptions , ConfigFile , GenerationOptions , OutputFileOptions } from './types' ;
4
- import { generateApi } from './generate' ;
5
4
import { isValidUrl , prettify } from './utils' ;
6
5
export { ConfigFile } from './types' ;
7
6
@@ -12,7 +11,10 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
12
11
? options . schemaFile
13
12
: path . resolve ( process . cwd ( ) , schemaLocation ) ;
14
13
15
- const sourceCode = await generateApi ( schemaAbsPath , options ) ;
14
+ const sourceCode = await enforceOazapftsTsVersion ( ( ) => {
15
+ const { generateApi } = require ( './generate' ) ;
16
+ return generateApi ( schemaAbsPath , options ) ;
17
+ } ) ;
16
18
const outputFile = options . outputFile ;
17
19
if ( outputFile ) {
18
20
fs . writeFileSync ( path . resolve ( process . cwd ( ) , outputFile ) , await prettify ( outputFile , sourceCode ) ) ;
@@ -38,3 +40,23 @@ export function parseConfig(fullConfig: ConfigFile) {
38
40
}
39
41
return outFiles ;
40
42
}
43
+
44
+ /**
45
+ * Enforces `oazapfts` to use the same TypeScript version as this module itself uses.
46
+ * That should prevent enums from running out of sync if both libraries use different TS versions.
47
+ */
48
+ function enforceOazapftsTsVersion < T > ( cb : ( ) => T ) : T {
49
+ const ozTsPath = require . resolve ( 'typescript' , { paths : [ require . resolve ( 'oazapfts' ) ] } ) ;
50
+ const tsPath = require . resolve ( 'typescript' ) ;
51
+ const originalEntry = require . cache [ ozTsPath ] ;
52
+ try {
53
+ require . cache [ ozTsPath ] = require . cache [ tsPath ] ;
54
+ return cb ( ) ;
55
+ } finally {
56
+ if ( originalEntry ) {
57
+ require . cache [ ozTsPath ] = originalEntry ;
58
+ } else {
59
+ delete require . cache [ ozTsPath ] ;
60
+ }
61
+ }
62
+ }
0 commit comments