Skip to content

Commit f4ab416

Browse files
authored
Merge pull request #3498 from hey-api/fix/dev-mode-check
fix: simplify dev mode check
2 parents 3ae2b77 + ea2538b commit f4ab416

6 files changed

Lines changed: 83 additions & 270 deletions

File tree

.changeset/slick-shirts-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
**internal**: fix: simplify dev mode check

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"tu": "turbo run build && vitest watch --update --project",
4545
"tb": "turbo run build --filter",
4646
"ty": "turbo run typecheck --filter",
47-
"dev:ts": "cd dev && tsx watch --clear-screen=false ../packages/openapi-ts/src/run.ts",
48-
"dev:py": "cd dev && tsx watch --clear-screen=false ../packages/openapi-python/src/run.ts"
47+
"dev:ts": "cd dev && OPENAPI_TS_DEV_MODE=1 tsx watch --clear-screen=false ../packages/openapi-ts/src/run.ts",
48+
"dev:py": "cd dev && OPENAPI_TS_DEV_MODE=1 tsx watch --clear-screen=false ../packages/openapi-python/src/run.ts"
4949
},
5050
"devDependencies": {
5151
"@arethetypeswrong/cli": "0.18.2",
Lines changed: 35 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from 'node:path';
1+
import { isDevMode } from '../client';
22

33
/**
44
* Replicates the outputHeaderToPrefix logic from generate/client.ts for testing.
@@ -48,129 +48,39 @@ describe('outputHeaderToPrefix logic', () => {
4848
});
4949

5050
describe('isDevMode logic', () => {
51-
const scenarios: ReadonlyArray<{
52-
description: string;
53-
expected: boolean;
54-
mockPath: string;
55-
}> = [
56-
{
57-
description: 'returns true in dev mode (src/generate)',
58-
expected: true,
59-
mockPath: ['', 'home', 'user', 'packages', 'openapi-python', 'src', 'generate'].join(
60-
path.sep,
61-
),
62-
},
63-
{
64-
description: 'returns false in prod mode (dist/generate)',
65-
expected: false,
66-
mockPath: ['', 'home', 'user', 'packages', 'openapi-python', 'dist', 'generate'].join(
67-
path.sep,
68-
),
69-
},
70-
{
71-
description: 'returns false when path contains /src/ but not in correct position',
72-
expected: false,
73-
mockPath: [
74-
'',
75-
'home',
76-
'user',
77-
'src',
78-
'project',
79-
'node_modules',
80-
'@hey-api',
81-
'openapi-python',
82-
'dist',
83-
'generate',
84-
].join(path.sep),
85-
},
86-
{
87-
description: 'returns false when src is in project path (pnpm case from issue)',
88-
expected: false,
89-
mockPath: [
90-
'',
91-
'home',
92-
'user',
93-
'src',
94-
'thcdb',
95-
'worktree',
96-
'schema-gen',
97-
'web',
98-
'node_modules',
99-
'.pnpm',
100-
'@hey-api+openapi-python@0.91.1',
101-
'node_modules',
102-
'@hey-api',
103-
'openapi-python',
104-
'dist',
105-
'generate',
106-
].join(path.sep),
107-
},
108-
{
109-
description:
110-
'returns false when src is in project path with plugins path (exact error from issue)',
111-
expected: false,
112-
mockPath: [
113-
'',
114-
'home',
115-
'user',
116-
'src',
117-
'thcdb',
118-
'worktree',
119-
'schema-gen',
120-
'web',
121-
'node_modules',
122-
'.pnpm',
123-
'@hey-api+openapi-python@0.91.1_magicast@0.5.1_typescript@5.9.3',
124-
'node_modules',
125-
'@hey-api',
126-
'openapi-python',
127-
'plugins',
128-
'@hey-api',
129-
'client-core',
130-
'bundle',
131-
].join(path.sep),
132-
},
133-
{
134-
description: 'returns true only when ending with src/generate',
135-
expected: true,
136-
mockPath: [
137-
'',
138-
'home',
139-
'user',
140-
'src',
141-
'backup',
142-
'packages',
143-
'openapi-python',
144-
'src',
145-
'generate',
146-
].join(path.sep),
147-
},
148-
{
149-
description: 'returns false when not ending with generate',
150-
expected: false,
151-
mockPath: ['', 'home', 'user', 'packages', 'openapi-python', 'src', 'plugins'].join(path.sep),
152-
},
153-
{
154-
description: 'returns false when src exists but dist is later',
155-
expected: false,
156-
mockPath: ['', 'home', 'user', 'src', 'project', 'openapi-python', 'dist', 'generate'].join(
157-
path.sep,
158-
),
159-
},
160-
];
161-
162-
it.each(scenarios)('$description', ({ expected, mockPath }) => {
163-
// Test the isDevMode logic
164-
const normalized = mockPath.split(path.sep);
165-
const srcIndex = normalized.lastIndexOf('src');
166-
const distIndex = normalized.lastIndexOf('dist');
167-
168-
const result =
169-
srcIndex !== -1 &&
170-
srcIndex > distIndex &&
171-
srcIndex === normalized.length - 2 &&
172-
normalized[srcIndex + 1] === 'generate';
173-
174-
expect(result).toBe(expected);
51+
const originalEnv = process.env;
52+
53+
beforeEach(() => {
54+
process.env = { ...originalEnv };
55+
delete process.env.OPENAPI_TS_DEV_MODE;
56+
});
57+
58+
afterAll(() => {
59+
process.env = originalEnv;
60+
});
61+
62+
it('returns false when env var is not set', () => {
63+
expect(isDevMode()).toBe(false);
64+
});
65+
66+
it('returns true when env var is set to "true"', () => {
67+
process.env.OPENAPI_TS_DEV_MODE = 'true';
68+
expect(isDevMode()).toBe(true);
69+
});
70+
71+
it('returns true when env var is set to "1"', () => {
72+
process.env.OPENAPI_TS_DEV_MODE = '1';
73+
expect(isDevMode()).toBe(true);
74+
});
75+
76+
it('returns false when env var is set to other values', () => {
77+
process.env.OPENAPI_TS_DEV_MODE = '0';
78+
expect(isDevMode()).toBe(false);
79+
80+
process.env.OPENAPI_TS_DEV_MODE = 'false';
81+
expect(isDevMode()).toBe(false);
82+
83+
process.env.OPENAPI_TS_DEV_MODE = 'anything';
84+
expect(isDevMode()).toBe(false);
17585
});
17686
});

packages/openapi-python/src/generate/client.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ const __filename = fileURLToPath(import.meta.url);
1414
const __dirname = path.dirname(__filename);
1515

1616
/**
17-
* Dev mode: 'src' appears after 'dist' (or dist doesn't exist), and 'generate' follows 'src'
17+
* Dev mode: determined by OPENAPI_TS_DEV_MODE environment variable
1818
*/
19-
function isDevMode(): boolean {
20-
const normalized = __dirname.split(path.sep);
21-
const srcIndex = normalized.lastIndexOf('src');
22-
const distIndex = normalized.lastIndexOf('dist');
23-
return (
24-
srcIndex !== -1 &&
25-
srcIndex > distIndex &&
26-
srcIndex === normalized.length - 2 &&
27-
normalized[srcIndex + 1] === 'generate'
28-
);
19+
export function isDevMode(): boolean {
20+
return process.env.OPENAPI_TS_DEV_MODE === 'true' || process.env.OPENAPI_TS_DEV_MODE === '1';
2921
}
3022

3123
/**
Lines changed: 35 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from 'node:path';
1+
import { isDevMode } from '../client';
22

33
/**
44
* Replicates the outputHeaderToPrefix logic from generate/client.ts for testing.
@@ -48,125 +48,39 @@ describe('outputHeaderToPrefix logic', () => {
4848
});
4949

5050
describe('isDevMode logic', () => {
51-
const scenarios: ReadonlyArray<{
52-
description: string;
53-
expected: boolean;
54-
mockPath: string;
55-
}> = [
56-
{
57-
description: 'returns true in dev mode (src/generate)',
58-
expected: true,
59-
mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'src', 'generate'].join(path.sep),
60-
},
61-
{
62-
description: 'returns false in prod mode (dist/generate)',
63-
expected: false,
64-
mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'dist', 'generate'].join(path.sep),
65-
},
66-
{
67-
description: 'returns false when path contains /src/ but not in correct position',
68-
expected: false,
69-
mockPath: [
70-
'',
71-
'home',
72-
'user',
73-
'src',
74-
'project',
75-
'node_modules',
76-
'@hey-api',
77-
'openapi-ts',
78-
'dist',
79-
'generate',
80-
].join(path.sep),
81-
},
82-
{
83-
description: 'returns false when src is in project path (pnpm case from issue)',
84-
expected: false,
85-
mockPath: [
86-
'',
87-
'home',
88-
'user',
89-
'src',
90-
'thcdb',
91-
'worktree',
92-
'schema-gen',
93-
'web',
94-
'node_modules',
95-
'.pnpm',
96-
'@hey-api+openapi-ts@0.91.1',
97-
'node_modules',
98-
'@hey-api',
99-
'openapi-ts',
100-
'dist',
101-
'generate',
102-
].join(path.sep),
103-
},
104-
{
105-
description:
106-
'returns false when src is in project path with plugins path (exact error from issue)',
107-
expected: false,
108-
mockPath: [
109-
'',
110-
'home',
111-
'user',
112-
'src',
113-
'thcdb',
114-
'worktree',
115-
'schema-gen',
116-
'web',
117-
'node_modules',
118-
'.pnpm',
119-
'@hey-api+openapi-ts@0.91.1_magicast@0.5.1_typescript@5.9.3',
120-
'node_modules',
121-
'@hey-api',
122-
'openapi-ts',
123-
'plugins',
124-
'@hey-api',
125-
'client-core',
126-
'bundle',
127-
].join(path.sep),
128-
},
129-
{
130-
description: 'returns true only when ending with src/generate',
131-
expected: true,
132-
mockPath: [
133-
'',
134-
'home',
135-
'user',
136-
'src',
137-
'backup',
138-
'packages',
139-
'openapi-ts',
140-
'src',
141-
'generate',
142-
].join(path.sep),
143-
},
144-
{
145-
description: 'returns false when not ending with generate',
146-
expected: false,
147-
mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'src', 'plugins'].join(path.sep),
148-
},
149-
{
150-
description: 'returns false when src exists but dist is later',
151-
expected: false,
152-
mockPath: ['', 'home', 'user', 'src', 'project', 'openapi-ts', 'dist', 'generate'].join(
153-
path.sep,
154-
),
155-
},
156-
];
157-
158-
it.each(scenarios)('$description', ({ expected, mockPath }) => {
159-
// Test the isDevMode logic
160-
const normalized = mockPath.split(path.sep);
161-
const srcIndex = normalized.lastIndexOf('src');
162-
const distIndex = normalized.lastIndexOf('dist');
163-
164-
const result =
165-
srcIndex !== -1 &&
166-
srcIndex > distIndex &&
167-
srcIndex === normalized.length - 2 &&
168-
normalized[srcIndex + 1] === 'generate';
169-
170-
expect(result).toBe(expected);
51+
const originalEnv = process.env;
52+
53+
beforeEach(() => {
54+
process.env = { ...originalEnv };
55+
delete process.env.OPENAPI_TS_DEV_MODE;
56+
});
57+
58+
afterAll(() => {
59+
process.env = originalEnv;
60+
});
61+
62+
it('returns false when env var is not set', () => {
63+
expect(isDevMode()).toBe(false);
64+
});
65+
66+
it('returns true when env var is set to "true"', () => {
67+
process.env.OPENAPI_TS_DEV_MODE = 'true';
68+
expect(isDevMode()).toBe(true);
69+
});
70+
71+
it('returns true when env var is set to "1"', () => {
72+
process.env.OPENAPI_TS_DEV_MODE = '1';
73+
expect(isDevMode()).toBe(true);
74+
});
75+
76+
it('returns false when env var is set to other values', () => {
77+
process.env.OPENAPI_TS_DEV_MODE = '0';
78+
expect(isDevMode()).toBe(false);
79+
80+
process.env.OPENAPI_TS_DEV_MODE = 'false';
81+
expect(isDevMode()).toBe(false);
82+
83+
process.env.OPENAPI_TS_DEV_MODE = 'anything';
84+
expect(isDevMode()).toBe(false);
17185
});
17286
});

packages/openapi-ts/src/generate/client.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ const __filename = fileURLToPath(import.meta.url);
1414
const __dirname = path.dirname(__filename);
1515

1616
/**
17-
* Dev mode: 'src' appears after 'dist' (or dist doesn't exist), and 'generate' follows 'src'
17+
* Dev mode: determined by OPENAPI_TS_DEV_MODE environment variable
1818
*/
19-
function isDevMode(): boolean {
20-
const normalized = __dirname.split(path.sep);
21-
const srcIndex = normalized.lastIndexOf('src');
22-
const distIndex = normalized.lastIndexOf('dist');
23-
return (
24-
srcIndex !== -1 &&
25-
srcIndex > distIndex &&
26-
srcIndex === normalized.length - 2 &&
27-
normalized[srcIndex + 1] === 'generate'
28-
);
19+
export function isDevMode(): boolean {
20+
return process.env.OPENAPI_TS_DEV_MODE === 'true' || process.env.OPENAPI_TS_DEV_MODE === '1';
2921
}
3022

3123
/**

0 commit comments

Comments
 (0)