fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: show environment value in development

Lubos 571bc8a3 21808fff

+52 -140
+5
.changeset/cute-hotels-write.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + **cli**: show environment value in development
+5
.changeset/proud-geckos-grab.md
··· 1 + --- 2 + "@hey-api/shared": patch 3 + --- 4 + 5 + **cli**: export isEnvironment function
+5
.changeset/smart-pugs-jog.md
··· 1 + --- 2 + "@hey-api/openapi-python": patch 3 + --- 4 + 5 + **cli**: show environment value in development
+2 -2
package.json
··· 44 44 "tu": "turbo run build && vitest watch --update --project", 45 45 "tb": "turbo run build --filter", 46 46 "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" 49 49 }, 50 50 "devDependencies": { 51 51 "@arethetypeswrong/cli": "0.18.2",
+2 -1
packages/openapi-python/src/cli/index.ts
··· 1 + import { isEnvironment } from '@hey-api/shared'; 1 2 import { Command, CommanderError } from 'commander'; 2 3 3 4 import pkg from '../../package.json'; ··· 9 10 const program = new Command() 10 11 .name(binName) 11 12 .description('Generate Python code from OpenAPI specifications') 12 - .version(pkg.version); 13 + .version(isEnvironment('development') ? '[DEVELOPMENT]' : pkg.version); 13 14 14 15 program 15 16 .option('-i, --input <path...>', 'OpenAPI specification (path, URL, or string)')
-86
packages/openapi-python/src/generate/__tests__/client.test.ts
··· 1 - import { isDevMode } from '../client'; 2 - 3 - /** 4 - * Replicates the outputHeaderToPrefix logic from generate/client.ts for testing. 5 - */ 6 - function outputHeaderToPrefix(header: unknown): string { 7 - if (header == null || typeof header === 'function') return ''; 8 - const lines = 9 - typeof header === 'string' 10 - ? header.split(/\r?\n/) 11 - : (header as string[]).flatMap((line) => line.split(/\r?\n/)); 12 - const content = lines.join('\n'); 13 - return content ? `${content}\n\n` : ''; 14 - } 15 - 16 - describe('outputHeaderToPrefix logic', () => { 17 - it('returns default comment for string header', () => { 18 - const result = outputHeaderToPrefix('# This file is auto-generated by @hey-api/openapi-python'); 19 - expect(result).toBe('# This file is auto-generated by @hey-api/openapi-python\n\n'); 20 - }); 21 - 22 - it('returns joined lines for array header', () => { 23 - const result = outputHeaderToPrefix([ 24 - '# This file is auto-generated by @hey-api/openapi-python', 25 - '# type: ignore', 26 - ]); 27 - expect(result).toBe( 28 - '# This file is auto-generated by @hey-api/openapi-python\n# type: ignore\n\n', 29 - ); 30 - }); 31 - 32 - it('returns empty string for null header', () => { 33 - expect(outputHeaderToPrefix(null)).toBe(''); 34 - }); 35 - 36 - it('returns empty string for undefined header', () => { 37 - expect(outputHeaderToPrefix(undefined)).toBe(''); 38 - }); 39 - 40 - it('returns empty string for function header', () => { 41 - expect(outputHeaderToPrefix(() => '# dynamic')).toBe(''); 42 - }); 43 - 44 - it('handles string with embedded newlines', () => { 45 - const result = outputHeaderToPrefix('# line1\n# line2'); 46 - expect(result).toBe('# line1\n# line2\n\n'); 47 - }); 48 - }); 49 - 50 - describe('isDevMode logic', () => { 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); 85 - }); 86 - });
+3 -15
packages/openapi-python/src/generate/client.ts
··· 4 4 5 5 import type { IProject } from '@hey-api/codegen-core'; 6 6 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'; 8 8 9 9 import type { Config } from '../config/types'; 10 10 import type { Client } from '../plugins/@hey-api/client-core/types'; ··· 12 12 13 13 const __filename = fileURLToPath(import.meta.url); 14 14 const __dirname = path.dirname(__filename); 15 - 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 15 23 16 /** 24 17 * Returns paths to client bundle files based on execution context ··· 29 22 } { 30 23 const clientName = pluginName.slice('@hey-api/client-'.length); 31 24 32 - if (isDevMode()) { 25 + if (isEnvironment('development')) { 33 26 // Dev: source bundle folders at src/plugins/@hey-api/{client}/bundle 34 27 const pluginsDir = path.resolve(__dirname, '..', 'plugins', '@hey-api'); 35 28 return { ··· 107 100 function replaceImports({ 108 101 filePath, 109 102 header, 110 - isDevMode, 111 103 renamed, 112 104 }: { 113 105 filePath: string; 114 106 header?: string; 115 - isDevMode?: boolean; 116 107 renamed: Map<string, string>; 117 108 }): void { 118 109 let content = fs.readFileSync(filePath, 'utf8'); 119 110 120 111 // Dev mode: rewrite source bundle imports to match output structure 121 - if (isDevMode) { 112 + if (isEnvironment('development')) { 122 113 // ...client_core.bundle.foo -> ..core.foo 123 114 content = content.replace( 124 115 /from\s+(\.{3,})\.?client_core\.bundle\./g, ··· 163 154 project: IProject; 164 155 }): Map<string, string> | undefined { 165 156 const renamed = new Map<string, string>(); 166 - const devMode = isDevMode(); 167 157 const headerPrefix = outputHeaderToPrefix(header, project); 168 158 169 159 // copy Hey API clients to output ··· 206 196 // for (const file of coreFiles) { 207 197 // replaceImports({ 208 198 // filePath: path.resolve(coreOutputPath, file), 209 - // isDevMode: devMode, 210 199 // renamed, 211 200 // }); 212 201 // } ··· 216 205 replaceImports({ 217 206 filePath: path.resolve(clientOutputPath, file), 218 207 header: headerPrefix, 219 - isDevMode: devMode, 220 208 renamed, 221 209 }); 222 210 }
+2 -1
packages/openapi-ts/src/cli/index.ts
··· 1 + import { isEnvironment } from '@hey-api/shared'; 1 2 import { Command, CommanderError } from 'commander'; 2 3 3 4 import pkg from '../../package.json'; ··· 9 10 const program = new Command() 10 11 .name(binName) 11 12 .description('Generate TypeScript code from OpenAPI specifications') 12 - .version(pkg.version); 13 + .version(isEnvironment('development') ? '[DEVELOPMENT]' : pkg.version); 13 14 14 15 program 15 16 .option('-i, --input <path...>', 'OpenAPI specification (path, URL, or string)')
+13 -18
packages/openapi-ts/src/generate/__tests__/client.test.ts packages/shared/src/__tests__/cli.test.ts
··· 1 - import { isDevMode } from '../client'; 1 + import { isEnvironment } from '../cli'; 2 2 3 3 /** 4 4 * Replicates the outputHeaderToPrefix logic from generate/client.ts for testing. ··· 47 47 }); 48 48 }); 49 49 50 - describe('isDevMode logic', () => { 50 + describe('isEnvironment', () => { 51 51 const originalEnv = process.env; 52 52 53 53 beforeEach(() => { 54 54 process.env = { ...originalEnv }; 55 - delete process.env.OPENAPI_TS_DEV_MODE; 55 + delete process.env.HEYAPI_CODEGEN_ENV; 56 56 }); 57 57 58 58 afterAll(() => { ··· 60 60 }); 61 61 62 62 it('returns false when env var is not set', () => { 63 - expect(isDevMode()).toBe(false); 63 + expect(isEnvironment('development')).toBe(false); 64 64 }); 65 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); 66 + it('returns true when env var is set to "development"', () => { 67 + process.env.HEYAPI_CODEGEN_ENV = 'development'; 68 + expect(isEnvironment('development')).toBe(true); 74 69 }); 75 70 76 71 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); 79 74 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); 82 77 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); 85 80 }); 86 81 });
+3 -15
packages/openapi-ts/src/generate/client.ts
··· 4 4 5 5 import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core'; 6 6 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'; 8 8 9 9 import type { Config } from '../config/types'; 10 10 import type { Client } from '../plugins/@hey-api/client-core/types'; ··· 12 12 13 13 const __filename = fileURLToPath(import.meta.url); 14 14 const __dirname = path.dirname(__filename); 15 - 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 15 23 16 /** 24 17 * Returns paths to client bundle files based on execution context ··· 29 22 } { 30 23 const clientName = pluginName.slice('@hey-api/client-'.length); 31 24 32 - if (isDevMode()) { 25 + if (isEnvironment('development')) { 33 26 // Dev: source bundle folders at src/plugins/@hey-api/{client}/bundle 34 27 const pluginsDir = path.resolve(__dirname, '..', 'plugins', '@hey-api'); 35 28 return { ··· 107 100 function replaceImports({ 108 101 filePath, 109 102 header, 110 - isDevMode, 111 103 meta, 112 104 renamed, 113 105 }: { 114 106 filePath: string; 115 107 header?: string; 116 - isDevMode?: boolean; 117 108 meta: ProjectRenderMeta; 118 109 renamed: Map<string, string>; 119 110 }): void { 120 111 let content = fs.readFileSync(filePath, 'utf8'); 121 112 122 113 // Dev mode: rewrite source bundle imports to match output structure 123 - if (isDevMode) { 114 + if (isEnvironment('development')) { 124 115 // ../../client-core/bundle/foo -> ../core/foo 125 116 content = content.replace(/from\s+['"]\.\.\/\.\.\/client-core\/bundle\//g, "from '../core/"); 126 117 // ../../client-core/bundle' (index import) ··· 166 157 project: IProject; 167 158 }): Map<string, string> | undefined { 168 159 const renamed = new Map<string, string>(); 169 - const devMode = isDevMode(); 170 160 const headerPrefix = outputHeaderToPrefix(header, project); 171 161 172 162 // copy Hey API clients to output ··· 209 199 replaceImports({ 210 200 filePath: path.resolve(coreOutputPath, file), 211 201 header: headerPrefix, 212 - isDevMode: devMode, 213 202 meta, 214 203 renamed, 215 204 }); ··· 220 209 replaceImports({ 221 210 filePath: path.resolve(clientOutputPath, file), 222 211 header: headerPrefix, 223 - isDevMode: devMode, 224 212 meta, 225 213 renamed, 226 214 });
+11 -1
packages/shared/src/cli.ts
··· 38 38 return { lines, maxLineLength }; 39 39 }; 40 40 41 + /** 42 + * Checks the current environment based on the HEYAPI_CODEGEN_ENV environment variable. 43 + */ 44 + export function isEnvironment(value: 'development'): boolean { 45 + return process.env.HEYAPI_CODEGEN_ENV === value; 46 + } 47 + 41 48 // TODO: show ascii logo only in `--help` and `--version` commands 42 49 export function printCliIntro(initialDir: string, showLogo: boolean = false): void { 43 50 const packageJson = loadPackageJson(initialDir); ··· 48 55 console.log(colors.cyan(line)); 49 56 } 50 57 } 51 - console.log(colors.gray(`${packageJson.name} v${packageJson.version}`)); 58 + const versionString = isEnvironment('development') 59 + ? '[DEVELOPMENT]' 60 + : `v${packageJson.version}`; 61 + console.log(colors.gray(`${packageJson.name} ${versionString}`)); 52 62 } 53 63 console.log(''); 54 64 }
+1 -1
packages/shared/src/index.ts
··· 1 - export { printCliIntro } from './cli'; 1 + export { isEnvironment, printCliIntro } from './cli'; 2 2 export { checkNodeVersion } from './config/engine'; 3 3 export { getInput } from './config/input/input'; 4 4 export { compileInputPath, logInputPaths } from './config/input/path';