Skip to content

Commit

Permalink
fix: 数组配置只生成了最后一个客户端
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Oct 17, 2024
1 parent 6cf2196 commit 9751a58
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions openapi-array.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from './src/define-config';

export default defineConfig([
{ url: './openapi/openapi.json', projectName: 'foo' },
{ url: './openapi/openapi.json', projectName: 'bar' },
]);
1 change: 1 addition & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ spinner.add({

await Promise.all(
ctx.configs.map(async (config, i) => {
ctx.projects = {
...ctx.projects,
...(await generateTemplate(ctx.docs[i]!, config)),
};
const project = await generateTemplate(ctx.docs[i]!, config);
ctx.projects = { ...ctx.projects, ...project };
}),
);

Expand Down
7 changes: 6 additions & 1 deletion src/lib/read-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { require } from 'tsx/cjs/api';
import type { DefineConfigOptions } from '../define-config';
import minimist from 'minimist';

const argv = minimist(process.argv.slice(2), {
alias: { config: ['c'] },
});

export const readConfig = () => {
const { default: content } = require(pathToFileURL(
path.resolve('openapi.config.ts'),
path.resolve(argv['config'] || 'openapi.config.ts'),
).toString(), import.meta.url);
return content as DefineConfigOptions;
};
19 changes: 15 additions & 4 deletions test/bin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from 'child_process';
import { existsSync, readFileSync } from 'fs';
import { rm } from 'fs/promises';
import path from 'path';
import { execSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { rm } from 'node:fs/promises';
import path from 'node:path';
import { beforeAll, expect, test } from 'vitest';

beforeAll(async () => {
Expand All @@ -21,3 +21,14 @@ test('生成runtime并合并代码', { timeout: 9_000 }, async () => {
'declare namespace OpenapiClient {',
);
});

test('配置数组生成多个client', async () => {
execSync('node dist/bin.mjs -c openapi-array.config.ts', {
encoding: 'utf8',
stdio: 'inherit',
});
const content = readFileSync(path.resolve('dist', 'index.d.ts'), 'utf8');
expect(content).toContain('declare namespace OpenapiClientFoo {');
expect(content).toContain('declare namespace OpenapiClientBar {');
expect(content).not.toContain('declare namespace OpenapiClient {');
});

0 comments on commit 9751a58

Please sign in to comment.