Skip to content

Commit 0f79f2b

Browse files
authored
Merge pull request #3546 from hey-api/fix/cli-dev-version
fix: show environment value in development
2 parents 21808ff + 571bc8a commit 0f79f2b

12 files changed

Lines changed: 52 additions & 140 deletions

File tree

.changeset/cute-hotels-write.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+
**cli**: show environment value in development

.changeset/proud-geckos-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/shared": patch
3+
---
4+
5+
**cli**: export isEnvironment function

.changeset/smart-pugs-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-python": patch
3+
---
4+
5+
**cli**: show environment value in development

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 && 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"
47+
"dev:ts": "cd dev && HEYAPI_CODEGEN_ENV=development tsx watch --clear-screen=false ../packages/openapi-ts/src/run.ts",
48+
"dev:py": "cd dev && HEYAPI_CODEGEN_ENV=development tsx watch --clear-screen=false ../packages/openapi-python/src/run.ts"
4949
},
5050
"devDependencies": {
5151
"@arethetypeswrong/cli": "0.18.2",

packages/openapi-python/src/cli/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isEnvironment } from '@hey-api/shared';
12
import { Command, CommanderError } from 'commander';
23

34
import pkg from '../../package.json';
@@ -9,7 +10,7 @@ const binName = Object.keys(pkg.bin)[0]!;
910
const program = new Command()
1011
.name(binName)
1112
.description('Generate Python code from OpenAPI specifications')
12-
.version(pkg.version);
13+
.version(isEnvironment('development') ? '[DEVELOPMENT]' : pkg.version);
1314

1415
program
1516
.option('-i, --input <path...>', 'OpenAPI specification (path, URL, or string)')

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

Lines changed: 0 additions & 86 deletions
This file was deleted.

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';
44

55
import type { IProject } from '@hey-api/codegen-core';
66
import type { DefinePlugin, OutputHeader } from '@hey-api/shared';
7-
import { ensureDirSync, outputHeaderToPrefix } from '@hey-api/shared';
7+
import { ensureDirSync, isEnvironment, outputHeaderToPrefix } from '@hey-api/shared';
88

99
import type { Config } from '../config/types';
1010
import type { Client } from '../plugins/@hey-api/client-core/types';
@@ -13,13 +13,6 @@ import { getClientPlugin } from '../plugins/@hey-api/client-core/utils';
1313
const __filename = fileURLToPath(import.meta.url);
1414
const __dirname = path.dirname(__filename);
1515

16-
/**
17-
* Dev mode: determined by OPENAPI_TS_DEV_MODE environment variable
18-
*/
19-
export function isDevMode(): boolean {
20-
return process.env.OPENAPI_TS_DEV_MODE === 'true' || process.env.OPENAPI_TS_DEV_MODE === '1';
21-
}
22-
2316
/**
2417
* Returns paths to client bundle files based on execution context
2518
*/
@@ -29,7 +22,7 @@ function getClientBundlePaths(pluginName: string): {
2922
} {
3023
const clientName = pluginName.slice('@hey-api/client-'.length);
3124

32-
if (isDevMode()) {
25+
if (isEnvironment('development')) {
3326
// Dev: source bundle folders at src/plugins/@hey-api/{client}/bundle
3427
const pluginsDir = path.resolve(__dirname, '..', 'plugins', '@hey-api');
3528
return {
@@ -107,18 +100,16 @@ function renameFile({
107100
function replaceImports({
108101
filePath,
109102
header,
110-
isDevMode,
111103
renamed,
112104
}: {
113105
filePath: string;
114106
header?: string;
115-
isDevMode?: boolean;
116107
renamed: Map<string, string>;
117108
}): void {
118109
let content = fs.readFileSync(filePath, 'utf8');
119110

120111
// Dev mode: rewrite source bundle imports to match output structure
121-
if (isDevMode) {
112+
if (isEnvironment('development')) {
122113
// ...client_core.bundle.foo -> ..core.foo
123114
content = content.replace(
124115
/from\s+(\.{3,})\.?client_core\.bundle\./g,
@@ -163,7 +154,6 @@ export function generateClientBundle({
163154
project: IProject;
164155
}): Map<string, string> | undefined {
165156
const renamed = new Map<string, string>();
166-
const devMode = isDevMode();
167157
const headerPrefix = outputHeaderToPrefix(header, project);
168158

169159
// copy Hey API clients to output
@@ -206,7 +196,6 @@ export function generateClientBundle({
206196
// for (const file of coreFiles) {
207197
// replaceImports({
208198
// filePath: path.resolve(coreOutputPath, file),
209-
// isDevMode: devMode,
210199
// renamed,
211200
// });
212201
// }
@@ -216,7 +205,6 @@ export function generateClientBundle({
216205
replaceImports({
217206
filePath: path.resolve(clientOutputPath, file),
218207
header: headerPrefix,
219-
isDevMode: devMode,
220208
renamed,
221209
});
222210
}

packages/openapi-ts/src/cli/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isEnvironment } from '@hey-api/shared';
12
import { Command, CommanderError } from 'commander';
23

34
import pkg from '../../package.json';
@@ -9,7 +10,7 @@ const binName = Object.keys(pkg.bin)[0]!;
910
const program = new Command()
1011
.name(binName)
1112
.description('Generate TypeScript code from OpenAPI specifications')
12-
.version(pkg.version);
13+
.version(isEnvironment('development') ? '[DEVELOPMENT]' : pkg.version);
1314

1415
program
1516
.option('-i, --input <path...>', 'OpenAPI specification (path, URL, or string)')

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';
44

55
import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core';
66
import type { DefinePlugin, OutputHeader } from '@hey-api/shared';
7-
import { ensureDirSync, outputHeaderToPrefix } from '@hey-api/shared';
7+
import { ensureDirSync, isEnvironment, outputHeaderToPrefix } from '@hey-api/shared';
88

99
import type { Config } from '../config/types';
1010
import type { Client } from '../plugins/@hey-api/client-core/types';
@@ -13,13 +13,6 @@ import { getClientPlugin } from '../plugins/@hey-api/client-core/utils';
1313
const __filename = fileURLToPath(import.meta.url);
1414
const __dirname = path.dirname(__filename);
1515

16-
/**
17-
* Dev mode: determined by OPENAPI_TS_DEV_MODE environment variable
18-
*/
19-
export function isDevMode(): boolean {
20-
return process.env.OPENAPI_TS_DEV_MODE === 'true' || process.env.OPENAPI_TS_DEV_MODE === '1';
21-
}
22-
2316
/**
2417
* Returns paths to client bundle files based on execution context
2518
*/
@@ -29,7 +22,7 @@ function getClientBundlePaths(pluginName: string): {
2922
} {
3023
const clientName = pluginName.slice('@hey-api/client-'.length);
3124

32-
if (isDevMode()) {
25+
if (isEnvironment('development')) {
3326
// Dev: source bundle folders at src/plugins/@hey-api/{client}/bundle
3427
const pluginsDir = path.resolve(__dirname, '..', 'plugins', '@hey-api');
3528
return {
@@ -107,20 +100,18 @@ function renameFile({
107100
function replaceImports({
108101
filePath,
109102
header,
110-
isDevMode,
111103
meta,
112104
renamed,
113105
}: {
114106
filePath: string;
115107
header?: string;
116-
isDevMode?: boolean;
117108
meta: ProjectRenderMeta;
118109
renamed: Map<string, string>;
119110
}): void {
120111
let content = fs.readFileSync(filePath, 'utf8');
121112

122113
// Dev mode: rewrite source bundle imports to match output structure
123-
if (isDevMode) {
114+
if (isEnvironment('development')) {
124115
// ../../client-core/bundle/foo -> ../core/foo
125116
content = content.replace(/from\s+['"]\.\.\/\.\.\/client-core\/bundle\//g, "from '../core/");
126117
// ../../client-core/bundle' (index import)
@@ -166,7 +157,6 @@ export function generateClientBundle({
166157
project: IProject;
167158
}): Map<string, string> | undefined {
168159
const renamed = new Map<string, string>();
169-
const devMode = isDevMode();
170160
const headerPrefix = outputHeaderToPrefix(header, project);
171161

172162
// copy Hey API clients to output
@@ -209,7 +199,6 @@ export function generateClientBundle({
209199
replaceImports({
210200
filePath: path.resolve(coreOutputPath, file),
211201
header: headerPrefix,
212-
isDevMode: devMode,
213202
meta,
214203
renamed,
215204
});
@@ -220,7 +209,6 @@ export function generateClientBundle({
220209
replaceImports({
221210
filePath: path.resolve(clientOutputPath, file),
222211
header: headerPrefix,
223-
isDevMode: devMode,
224212
meta,
225213
renamed,
226214
});

packages/openapi-ts/src/generate/__tests__/client.test.ts renamed to packages/shared/src/__tests__/cli.test.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isDevMode } from '../client';
1+
import { isEnvironment } from '../cli';
22

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

50-
describe('isDevMode logic', () => {
50+
describe('isEnvironment', () => {
5151
const originalEnv = process.env;
5252

5353
beforeEach(() => {
5454
process.env = { ...originalEnv };
55-
delete process.env.OPENAPI_TS_DEV_MODE;
55+
delete process.env.HEYAPI_CODEGEN_ENV;
5656
});
5757

5858
afterAll(() => {
5959
process.env = originalEnv;
6060
});
6161

6262
it('returns false when env var is not set', () => {
63-
expect(isDevMode()).toBe(false);
63+
expect(isEnvironment('development')).toBe(false);
6464
});
6565

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);
66+
it('returns true when env var is set to "development"', () => {
67+
process.env.HEYAPI_CODEGEN_ENV = 'development';
68+
expect(isEnvironment('development')).toBe(true);
7469
});
7570

7671
it('returns false when env var is set to other values', () => {
77-
process.env.OPENAPI_TS_DEV_MODE = '0';
78-
expect(isDevMode()).toBe(false);
72+
process.env.HEYAPI_CODEGEN_ENV = '0';
73+
expect(isEnvironment('development')).toBe(false);
7974

80-
process.env.OPENAPI_TS_DEV_MODE = 'false';
81-
expect(isDevMode()).toBe(false);
75+
process.env.HEYAPI_CODEGEN_ENV = 'false';
76+
expect(isEnvironment('development')).toBe(false);
8277

83-
process.env.OPENAPI_TS_DEV_MODE = 'anything';
84-
expect(isDevMode()).toBe(false);
78+
process.env.HEYAPI_CODEGEN_ENV = 'anything';
79+
expect(isEnvironment('development')).toBe(false);
8580
});
8681
});

0 commit comments

Comments
 (0)