Skip to content

Commit 238de88

Browse files
committed
fix: infix .gen to client and core
1 parent 70c3836 commit 238de88

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getClientPlugin } from '../plugins/@hey-api/client-core/utils';
99
import type { DefinePlugin } from '../plugins/types';
1010
import type { ImportExportItemObject } from '../tsc/utils';
1111
import type { Config } from '../types/config';
12+
import { _test } from './file';
1213
import { ensureDirSync, relativeModulePath } from './utils';
1314

1415
// Use require.resolve to find the package root, then construct the path
@@ -89,7 +90,7 @@ const copyRecursivePnP = (src: string, dest: string) => {
8990
}
9091
};
9192

92-
const replaceRelativeImports = (filePath: string) => {
93+
const appendRelativeImportsSuffix = (filePath: string, suffix = '.js') => {
9394
let content = fs.readFileSync(filePath, 'utf8');
9495

9596
// Replace relative imports to append .js extension for ESM compatibility
@@ -105,13 +106,38 @@ const replaceRelativeImports = (filePath: string) => {
105106
if (fileName.includes('.')) {
106107
return match;
107108
}
108-
return `from '${importPath}.js'`;
109+
return `from '${importPath}${suffix}'`;
109110
},
110111
);
111112

112113
fs.writeFileSync(filePath, content, 'utf8');
113114
};
114115

116+
const replaceRelativeImports = (filePath: string) =>
117+
appendRelativeImportsSuffix(filePath);
118+
119+
const infixDotGenToFiles = (outputPath: string) => {
120+
const coreFiles = fs.readdirSync(outputPath);
121+
for (const file of coreFiles) {
122+
const filePath = path.resolve(outputPath, file);
123+
if (file !== 'index.ts') {
124+
const { extension, name } = _test.splitNameAndExtension(filePath);
125+
const newFilePath = path.resolve(
126+
outputPath,
127+
[name, 'gen', extension].filter(Boolean).join('.'),
128+
);
129+
fs.renameSync(filePath, newFilePath);
130+
131+
appendRelativeImportsSuffix(
132+
path.resolve(outputPath, newFilePath),
133+
'.gen',
134+
);
135+
} else {
136+
appendRelativeImportsSuffix(path.resolve(outputPath, filePath), '.gen');
137+
}
138+
}
139+
};
140+
115141
/**
116142
* Creates a `client` folder containing the same modules as the client package.
117143
*/
@@ -135,6 +161,9 @@ export const generateClientBundle = ({
135161
ensureDirSync(coreOutputPath);
136162
const coreDistPath = path.resolve(packageRoot, 'dist', 'clients', 'core');
137163
copyRecursivePnP(coreDistPath, coreOutputPath);
164+
165+
infixDotGenToFiles(coreOutputPath);
166+
138167
if (shouldAppendJs) {
139168
const coreFiles = fs.readdirSync(coreOutputPath);
140169
for (const file of coreFiles) {
@@ -152,6 +181,9 @@ export const generateClientBundle = ({
152181
clientDistFolderName,
153182
);
154183
copyRecursivePnP(clientDistPath, clientOutputPath);
184+
185+
infixDotGenToFiles(clientOutputPath);
186+
155187
if (shouldAppendJs) {
156188
const clientFiles = fs.readdirSync(clientOutputPath);
157189
for (const file of clientFiles) {

0 commit comments

Comments
 (0)