Skip to content

Commit a2aab8e

Browse files
committed
feat: 增加静默模式
1 parent 448ccf6 commit a2aab8e

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export const fooClient = new OpenapiClientFoo(adapter1);
9494
export const barClient = new OpenapiClientBar(adapter2);
9595
```
9696

97-
# 环境变量
97+
# CLI选项
98+
99+
## 环境变量
98100

99101
不同运行环境下,可能需要使用不同的服务端,比如开发一套服务,生产一套服务。因此执行指令时可以传入`-env`参数
100102

@@ -118,6 +120,22 @@ export default defineConfig((env) => {
118120
});
119121
```
120122

123+
## 静默模式
124+
125+
如果不希望屏幕上有文字输出,则使用`--silent`参数
126+
127+
```bash
128+
npx openapi --silent
129+
```
130+
131+
## 指定文件
132+
133+
默认配置文件:`openapi.config.ts`,可以使用`--config`指定新的文件
134+
135+
```bash
136+
npx openapi --config my-custom.config.ts
137+
```
138+
121139
# 参数
122140

123141
### url

src/bin.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,30 @@ import { generateTemplate } from './lib/generate-template';
1414
import { filterTag } from './lib/filter-tag';
1515
import { filterUrl } from './lib/filter-url';
1616
import { readConfig } from './lib/read-config';
17+
import { SilentSpinner } from './silent-spinner';
1718

18-
const sleep = () => timers.setTimeout(300);
19+
const argv = minimist(process.argv.slice(2), {
20+
alias: { config: ['c'], env: ['e'] },
21+
});
22+
const silent = Boolean(argv['silent']);
23+
const env = argv['env'] || process.env['NODE_ENV'] || 'development';
24+
const configFile = argv['config'];
1925

26+
const sleep = () => timers.setTimeout(300);
2027
const toArray = (value: any) => (Array.isArray(value) ? value : [value]);
2128

22-
const spinner = new Listr<{
29+
const spinner = (silent ? new SilentSpinner([]) : new Listr([])) as Listr<{
2330
configs: OpenapiClientConfig[];
2431
docs: OpenAPIV3.Document[];
2532
projects: Record<string, { dts: string; js: string }>;
26-
}>([]);
33+
}>;
2734

2835
spinner.add({
2936
title: '读取配置文件openapi.config.ts',
3037
task: async (ctx, task) => {
31-
const userConfig = readConfig();
38+
const userConfig = readConfig(configFile);
3239

3340
if (typeof userConfig === 'function') {
34-
const args = minimist(process.argv.slice(2), { alias: { env: ['e'] } });
35-
const env = args['env'] || process.env['NODE_ENV'] || 'development';
3641
task.title += ` ${colors.gray(env)}`;
3742
ctx.configs = toArray(await userConfig(env));
3843
} else {
@@ -114,4 +119,4 @@ spinner.add({
114119
},
115120
});
116121

117-
spinner.run();
122+
await spinner.run();

src/lib/read-config.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ 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';
65

7-
const argv = minimist(process.argv.slice(2), {
8-
alias: { config: ['c'] },
9-
});
10-
11-
export const readConfig = () => {
6+
export const readConfig = (configFile: string = 'openapi.config.ts') => {
127
const { default: content } = require(pathToFileURL(
13-
path.resolve(argv['config'] || 'openapi.config.ts'),
8+
path.resolve(configFile),
149
).toString(), import.meta.url);
1510
return content as DefineConfigOptions;
1611
};

src/silent-spinner.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export class SilentSpinner {
2+
constructor(protected readonly tasks: Task[]) {}
3+
4+
add(task: Task) {
5+
this.tasks.push(task);
6+
}
7+
8+
async run(ctx: object = {}) {
9+
for (const task of this.tasks) {
10+
if (task.skip) {
11+
if (task.skip === true) continue;
12+
if (await task.skip(ctx)) continue;
13+
}
14+
await task.task(ctx, { title: task.title });
15+
}
16+
}
17+
}
18+
19+
interface Task {
20+
title: string;
21+
skip?: boolean | ((ctx: object) => boolean | Promise<boolean>);
22+
task: (ctx: object, task: { title: string }) => Promise<any>;
23+
}

test/bin.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('生成runtime并合并代码', { timeout: 9_000 }, async () => {
2222
);
2323
});
2424

25-
test('配置数组生成多个client', async () => {
25+
test('配置数组生成多个client', { timeout: 9_000 }, async () => {
2626
execSync('node dist/bin.mjs -c openapi-array.config.ts', {
2727
encoding: 'utf8',
2828
stdio: 'inherit',
@@ -32,3 +32,10 @@ test('配置数组生成多个client', async () => {
3232
expect(content).toContain('declare namespace OpenapiClientBar {');
3333
expect(content).not.toContain('declare namespace OpenapiClient {');
3434
});
35+
36+
test('静默模式', { timeout: 9_000 }, async () => {
37+
execSync('node dist/bin.mjs --silent', { encoding: 'utf8', stdio: 'inherit' });
38+
expect(readFileSync(path.resolve('dist', 'index.d.ts'), 'utf8')).toContain(
39+
'declare namespace OpenapiClient {',
40+
);
41+
});

tsup.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default defineConfig([
1111
format: ['esm'],
1212
platform: 'node',
1313
tsconfig: './tsconfig.json',
14-
target: 'es2020',
14+
target: 'node18',
1515
shims: false,
1616
dts: false,
1717
outExtension: () => ({ js: '.mjs' }),
@@ -24,7 +24,7 @@ export default defineConfig([
2424
format: ['cjs', 'esm'],
2525
platform: 'node',
2626
tsconfig: './tsconfig.json',
27-
target: 'es2020',
27+
target: 'node18',
2828
shims: false,
2929
dts: true,
3030
legacyOutput: true,
@@ -39,7 +39,7 @@ export default defineConfig([
3939
format: ['cjs', 'esm'],
4040
platform: 'node',
4141
tsconfig: './tsconfig.json',
42-
target: 'es2020',
42+
target: 'node18',
4343
shims: false,
4444
dts: true,
4545
legacyOutput: true,

0 commit comments

Comments
 (0)