diff --git a/openapi-array.config.ts b/openapi-array.config.ts new file mode 100644 index 0000000..7d06e3a --- /dev/null +++ b/openapi-array.config.ts @@ -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' }, +]); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9b9155f..643f1f4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1951,6 +1951,7 @@ packages: '@swc/register@0.1.10': resolution: {integrity: sha512-6STwH/q4dc3pitXLVkV7sP0Hiy+zBsU2wOF1aXpXR95pnH3RYHKIsDC+gvesfyB7jxNT9OOZgcqOp9RPxVTx9A==} + deprecated: Use @swc-node/register instead hasBin: true peerDependencies: '@swc/core': ^1.0.46 diff --git a/src/bin.ts b/src/bin.ts index e05df03..6c6ade6 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -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 }; }), ); diff --git a/src/lib/read-config.ts b/src/lib/read-config.ts index addf2e6..d7213ab 100644 --- a/src/lib/read-config.ts +++ b/src/lib/read-config.ts @@ -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; }; diff --git a/test/bin.test.ts b/test/bin.test.ts index 6083443..d334ec9 100644 --- a/test/bin.test.ts +++ b/test/bin.test.ts @@ -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 () => { @@ -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 {'); +});