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.

Merge pull request #3300 from hey-api/copilot/fix-isdevmode-false-positive

Fix isDevMode false-positive when install path contains /src/

authored by

Lubos and committed by
GitHub
c05437be ce5977e7

+144 -3
+5
.changeset/tired-rats-watch.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + **fix**: improve dev mode detection causing builds to fail
+127
packages/openapi-ts/src/generate/__tests__/client.test.ts
··· 1 + import path from 'node:path'; 2 + 3 + import { describe, expect, it } from 'vitest'; 4 + 5 + describe('isDevMode logic', () => { 6 + const scenarios: ReadonlyArray<{ 7 + description: string; 8 + expected: boolean; 9 + mockPath: string; 10 + }> = [ 11 + { 12 + description: 'returns true in dev mode (src/generate)', 13 + expected: true, 14 + mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'src', 'generate'].join(path.sep), 15 + }, 16 + { 17 + description: 'returns false in prod mode (dist/generate)', 18 + expected: false, 19 + mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'dist', 'generate'].join(path.sep), 20 + }, 21 + { 22 + description: 'returns false when path contains /src/ but not in correct position', 23 + expected: false, 24 + mockPath: [ 25 + '', 26 + 'home', 27 + 'user', 28 + 'src', 29 + 'project', 30 + 'node_modules', 31 + '@hey-api', 32 + 'openapi-ts', 33 + 'dist', 34 + 'generate', 35 + ].join(path.sep), 36 + }, 37 + { 38 + description: 'returns false when src is in project path (pnpm case from issue)', 39 + expected: false, 40 + mockPath: [ 41 + '', 42 + 'home', 43 + 'user', 44 + 'src', 45 + 'thcdb', 46 + 'worktree', 47 + 'schema-gen', 48 + 'web', 49 + 'node_modules', 50 + '.pnpm', 51 + '@hey-api+openapi-ts@0.91.1', 52 + 'node_modules', 53 + '@hey-api', 54 + 'openapi-ts', 55 + 'dist', 56 + 'generate', 57 + ].join(path.sep), 58 + }, 59 + { 60 + description: 61 + 'returns false when src is in project path with plugins path (exact error from issue)', 62 + expected: false, 63 + mockPath: [ 64 + '', 65 + 'home', 66 + 'user', 67 + 'src', 68 + 'thcdb', 69 + 'worktree', 70 + 'schema-gen', 71 + 'web', 72 + 'node_modules', 73 + '.pnpm', 74 + '@hey-api+openapi-ts@0.91.1_magicast@0.5.1_typescript@5.9.3', 75 + 'node_modules', 76 + '@hey-api', 77 + 'openapi-ts', 78 + 'plugins', 79 + '@hey-api', 80 + 'client-core', 81 + 'bundle', 82 + ].join(path.sep), 83 + }, 84 + { 85 + description: 'returns true only when ending with src/generate', 86 + expected: true, 87 + mockPath: [ 88 + '', 89 + 'home', 90 + 'user', 91 + 'src', 92 + 'backup', 93 + 'packages', 94 + 'openapi-ts', 95 + 'src', 96 + 'generate', 97 + ].join(path.sep), 98 + }, 99 + { 100 + description: 'returns false when not ending with generate', 101 + expected: false, 102 + mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'src', 'plugins'].join(path.sep), 103 + }, 104 + { 105 + description: 'returns false when src exists but dist is later', 106 + expected: false, 107 + mockPath: ['', 'home', 'user', 'src', 'project', 'openapi-ts', 'dist', 'generate'].join( 108 + path.sep, 109 + ), 110 + }, 111 + ]; 112 + 113 + it.each(scenarios)('$description', ({ expected, mockPath }) => { 114 + // Test the isDevMode logic 115 + const normalized = mockPath.split(path.sep); 116 + const srcIndex = normalized.lastIndexOf('src'); 117 + const distIndex = normalized.lastIndexOf('dist'); 118 + 119 + const result = 120 + srcIndex !== -1 && 121 + srcIndex > distIndex && 122 + srcIndex === normalized.length - 2 && 123 + normalized[srcIndex + 1] === 'generate'; 124 + 125 + expect(result).toBe(expected); 126 + }); 127 + });
+12 -3
packages/openapi-ts/src/generate/client.ts
··· 13 13 const __filename = fileURLToPath(import.meta.url); 14 14 const __dirname = path.dirname(__filename); 15 15 16 + /** 17 + * Dev mode: 'src' appears after 'dist' (or dist doesn't exist), and 'generate' follows 'src' 18 + */ 16 19 function isDevMode(): boolean { 17 - // In dev: __dirname = .../packages/openapi-ts/src/generate 18 - // In prod: __dirname = .../packages/openapi-ts/dist/generate 19 - return __dirname.includes(`${path.sep}src${path.sep}`); 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 + ); 20 29 } 21 30 22 31 /**