Skip to content

Commit

Permalink
feat: 增加静默模式
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Oct 20, 2024
1 parent 448ccf6 commit a2aab8e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 19 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export const fooClient = new OpenapiClientFoo(adapter1);
export const barClient = new OpenapiClientBar(adapter2);
```

# 环境变量
# CLI选项

## 环境变量

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

Expand All @@ -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
Expand Down
19 changes: 12 additions & 7 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { dts: string; js: string }>;
}>([]);
}>;

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 {
Expand Down Expand Up @@ -114,4 +119,4 @@ spinner.add({
},
});

spinner.run();
await spinner.run();
9 changes: 2 additions & 7 deletions src/lib/read-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
23 changes: 23 additions & 0 deletions src/silent-spinner.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>);
task: (ctx: object, task: { title: string }) => Promise<any>;
}
9 changes: 8 additions & 1 deletion test/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 {',
);
});
6 changes: 3 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }),
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit a2aab8e

Please sign in to comment.