From a2aab8e38ffa8bac1d756960b1db39d23c3d0a5d Mon Sep 17 00:00:00 2001 From: geekact Date: Sun, 20 Oct 2024 12:23:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=9D=99=E9=BB=98?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 +++++++++++++++++++- src/bin.ts | 19 ++++++++++++------- src/lib/read-config.ts | 9 ++------- src/silent-spinner.ts | 23 +++++++++++++++++++++++ test/bin.test.ts | 9 ++++++++- tsup.config.ts | 6 +++--- 6 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 src/silent-spinner.ts diff --git a/README.md b/README.md index c27c727..a6d412d 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,9 @@ export const fooClient = new OpenapiClientFoo(adapter1); export const barClient = new OpenapiClientBar(adapter2); ``` -# 环境变量 +# CLI选项 + +## 环境变量 不同运行环境下,可能需要使用不同的服务端,比如开发一套服务,生产一套服务。因此执行指令时可以传入`-env`参数 @@ -118,6 +120,22 @@ export default defineConfig((env) => { }); ``` +## 静默模式 + +如果不希望屏幕上有文字输出,则使用`--silent`参数 + +```bash +npx openapi --silent +``` + +## 指定文件 + +默认配置文件:`openapi.config.ts`,可以使用`--config`指定新的文件 + +```bash +npx openapi --config my-custom.config.ts +``` + # 参数 ### url diff --git a/src/bin.ts b/src/bin.ts index 6c6ade6..c85e95f 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -14,25 +14,30 @@ import { generateTemplate } from './lib/generate-template'; import { filterTag } from './lib/filter-tag'; import { filterUrl } from './lib/filter-url'; import { readConfig } from './lib/read-config'; +import { SilentSpinner } from './silent-spinner'; -const sleep = () => timers.setTimeout(300); +const argv = minimist(process.argv.slice(2), { + alias: { config: ['c'], env: ['e'] }, +}); +const silent = Boolean(argv['silent']); +const env = argv['env'] || process.env['NODE_ENV'] || 'development'; +const configFile = argv['config']; +const sleep = () => timers.setTimeout(300); const toArray = (value: any) => (Array.isArray(value) ? value : [value]); -const spinner = new Listr<{ +const spinner = (silent ? new SilentSpinner([]) : new Listr([])) as Listr<{ configs: OpenapiClientConfig[]; docs: OpenAPIV3.Document[]; projects: Record; -}>([]); +}>; spinner.add({ title: '读取配置文件openapi.config.ts', task: async (ctx, task) => { - const userConfig = readConfig(); + const userConfig = readConfig(configFile); if (typeof userConfig === 'function') { - const args = minimist(process.argv.slice(2), { alias: { env: ['e'] } }); - const env = args['env'] || process.env['NODE_ENV'] || 'development'; task.title += ` ${colors.gray(env)}`; ctx.configs = toArray(await userConfig(env)); } else { @@ -114,4 +119,4 @@ spinner.add({ }, }); -spinner.run(); +await spinner.run(); diff --git a/src/lib/read-config.ts b/src/lib/read-config.ts index d7213ab..b36de52 100644 --- a/src/lib/read-config.ts +++ b/src/lib/read-config.ts @@ -2,15 +2,10 @@ 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 = () => { +export const readConfig = (configFile: string = 'openapi.config.ts') => { const { default: content } = require(pathToFileURL( - path.resolve(argv['config'] || 'openapi.config.ts'), + path.resolve(configFile), ).toString(), import.meta.url); return content as DefineConfigOptions; }; diff --git a/src/silent-spinner.ts b/src/silent-spinner.ts new file mode 100644 index 0000000..50b2672 --- /dev/null +++ b/src/silent-spinner.ts @@ -0,0 +1,23 @@ +export class SilentSpinner { + constructor(protected readonly tasks: Task[]) {} + + add(task: Task) { + this.tasks.push(task); + } + + async run(ctx: object = {}) { + for (const task of this.tasks) { + if (task.skip) { + if (task.skip === true) continue; + if (await task.skip(ctx)) continue; + } + await task.task(ctx, { title: task.title }); + } + } +} + +interface Task { + title: string; + skip?: boolean | ((ctx: object) => boolean | Promise); + task: (ctx: object, task: { title: string }) => Promise; +} diff --git a/test/bin.test.ts b/test/bin.test.ts index d334ec9..9a0816a 100644 --- a/test/bin.test.ts +++ b/test/bin.test.ts @@ -22,7 +22,7 @@ test('生成runtime并合并代码', { timeout: 9_000 }, async () => { ); }); -test('配置数组生成多个client', async () => { +test('配置数组生成多个client', { timeout: 9_000 }, async () => { execSync('node dist/bin.mjs -c openapi-array.config.ts', { encoding: 'utf8', stdio: 'inherit', @@ -32,3 +32,10 @@ test('配置数组生成多个client', async () => { expect(content).toContain('declare namespace OpenapiClientBar {'); expect(content).not.toContain('declare namespace OpenapiClient {'); }); + +test('静默模式', { timeout: 9_000 }, async () => { + execSync('node dist/bin.mjs --silent', { encoding: 'utf8', stdio: 'inherit' }); + expect(readFileSync(path.resolve('dist', 'index.d.ts'), 'utf8')).toContain( + 'declare namespace OpenapiClient {', + ); +}); diff --git a/tsup.config.ts b/tsup.config.ts index a701aeb..9a057e9 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -11,7 +11,7 @@ export default defineConfig([ format: ['esm'], platform: 'node', tsconfig: './tsconfig.json', - target: 'es2020', + target: 'node18', shims: false, dts: false, outExtension: () => ({ js: '.mjs' }), @@ -24,7 +24,7 @@ export default defineConfig([ format: ['cjs', 'esm'], platform: 'node', tsconfig: './tsconfig.json', - target: 'es2020', + target: 'node18', shims: false, dts: true, legacyOutput: true, @@ -39,7 +39,7 @@ export default defineConfig([ format: ['cjs', 'esm'], platform: 'node', tsconfig: './tsconfig.json', - target: 'es2020', + target: 'node18', shims: false, dts: true, legacyOutput: true,