Skip to content

Commit 9751a58

Browse files
committed
fix: 数组配置只生成了最后一个客户端
1 parent 6cf2196 commit 9751a58

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

openapi-array.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from './src/define-config';
2+
3+
export default defineConfig([
4+
{ url: './openapi/openapi.json', projectName: 'foo' },
5+
{ url: './openapi/openapi.json', projectName: 'bar' },
6+
]);

pnpm-lock.yaml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bin.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ spinner.add({
9090

9191
await Promise.all(
9292
ctx.configs.map(async (config, i) => {
93-
ctx.projects = {
94-
...ctx.projects,
95-
...(await generateTemplate(ctx.docs[i]!, config)),
96-
};
93+
const project = await generateTemplate(ctx.docs[i]!, config);
94+
ctx.projects = { ...ctx.projects, ...project };
9795
}),
9896
);
9997

src/lib/read-config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import path from 'node:path';
22
import { pathToFileURL } from 'node:url';
33
import { require } from 'tsx/cjs/api';
44
import type { DefineConfigOptions } from '../define-config';
5+
import minimist from 'minimist';
6+
7+
const argv = minimist(process.argv.slice(2), {
8+
alias: { config: ['c'] },
9+
});
510

611
export const readConfig = () => {
712
const { default: content } = require(pathToFileURL(
8-
path.resolve('openapi.config.ts'),
13+
path.resolve(argv['config'] || 'openapi.config.ts'),
914
).toString(), import.meta.url);
1015
return content as DefineConfigOptions;
1116
};

test/bin.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { execSync } from 'child_process';
2-
import { existsSync, readFileSync } from 'fs';
3-
import { rm } from 'fs/promises';
4-
import path from 'path';
1+
import { execSync } from 'node:child_process';
2+
import { existsSync, readFileSync } from 'node:fs';
3+
import { rm } from 'node:fs/promises';
4+
import path from 'node:path';
55
import { beforeAll, expect, test } from 'vitest';
66

77
beforeAll(async () => {
@@ -21,3 +21,14 @@ test('生成runtime并合并代码', { timeout: 9_000 }, async () => {
2121
'declare namespace OpenapiClient {',
2222
);
2323
});
24+
25+
test('配置数组生成多个client', async () => {
26+
execSync('node dist/bin.mjs -c openapi-array.config.ts', {
27+
encoding: 'utf8',
28+
stdio: 'inherit',
29+
});
30+
const content = readFileSync(path.resolve('dist', 'index.d.ts'), 'utf8');
31+
expect(content).toContain('declare namespace OpenapiClientFoo {');
32+
expect(content).toContain('declare namespace OpenapiClientBar {');
33+
expect(content).not.toContain('declare namespace OpenapiClient {');
34+
});

0 commit comments

Comments
 (0)