Skip to content

Commit 2e3d7ab

Browse files
authored
force oazapfts to use the same TS version as the codegen (#1771)
1 parent ab3e14a commit 2e3d7ab

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/rtk-query-codegen-openapi/src/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path';
22
import fs from 'fs';
33
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
4-
import { generateApi } from './generate';
54
import { isValidUrl, prettify } from './utils';
65
export { ConfigFile } from './types';
76

@@ -12,7 +11,10 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
1211
? options.schemaFile
1312
: path.resolve(process.cwd(), schemaLocation);
1413

15-
const sourceCode = await generateApi(schemaAbsPath, options);
14+
const sourceCode = await enforceOazapftsTsVersion(() => {
15+
const { generateApi } = require('./generate');
16+
return generateApi(schemaAbsPath, options);
17+
});
1618
const outputFile = options.outputFile;
1719
if (outputFile) {
1820
fs.writeFileSync(path.resolve(process.cwd(), outputFile), await prettify(outputFile, sourceCode));
@@ -38,3 +40,23 @@ export function parseConfig(fullConfig: ConfigFile) {
3840
}
3941
return outFiles;
4042
}
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+
}

packages/rtk-query-codegen-openapi/test/cli.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ afterEach(() => {
3333

3434
describe('CLI options testing', () => {
3535
test('generation with `config.example.js`', async () => {
36+
jest.setTimeout(10000);
37+
3638
const out = await cli([`./config.example.js`], __dirname);
3739

3840
expect(out).toEqual({
@@ -47,6 +49,8 @@ Done
4749
});
4850

4951
test('paths are relative to configfile, not to cwd', async () => {
52+
jest.setTimeout(10000);
53+
5054
const out = await cli([`../test/config.example.js`], path.resolve(__dirname, '../src'));
5155

5256
expect(out).toEqual({
@@ -61,7 +65,7 @@ Done
6165
});
6266

6367
test('ts, js and json all work the same', async () => {
64-
jest.setTimeout(15000);
68+
jest.setTimeout(25000);
6569

6670
await cli([`./config.example.js`], __dirname);
6771
const fromJs = fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8');

0 commit comments

Comments
 (0)