Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-guests-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-request": patch
---

perf: add option to cli for disabling interactive api generation
7 changes: 4 additions & 3 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import type { GenerateServiceProps } from '../index';
import { generateService } from '../index';
import { logError } from '../log';
import { readConfig } from '../readConfig';
import { createMultiselectOptions } from './utils';
import { createMultiselectOptions, parseInteractiveMode } from './utils';

program
.option('-cfn, --configFileName <string>', 'config file name')
.option('-cfp, --configFilePath <string>', 'config file path');
.option('-cfp, --configFilePath <string>', 'config file path')
.option('-i, --interactive <boolean>', 'enable interactive mode', true);

program.parse();
const options = program.opts();
Expand All @@ -37,7 +38,7 @@ async function run() {
/** 是否交互式 */
let isInteractive = false;

if (configs.length > 1) {
if (configs.length > 1 && parseInteractiveMode(options.interactive)) {
// 有多个配置,则交互式选择
isInteractive = true;

Expand Down
5 changes: 3 additions & 2 deletions src/bin/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { generateService } from '../index';
import { logError } from '../log';
import { readConfig } from '../readConfig';
import type { IPriorityRule, IReactQueryMode } from '../type';
import { createMultiselectOptions } from './utils';
import { createMultiselectOptions, parseInteractiveMode } from './utils';

const params = program
.name('openapi')
Expand Down Expand Up @@ -107,6 +107,7 @@ const params = program
'--supportParseEnumDescByReg <string>',
'custom regex for parsing enum description'
)
.option('-i, --interactive <boolean>', 'enable interactive mode', true)
.parse(process.argv)
.opts();

Expand Down Expand Up @@ -189,7 +190,7 @@ async function run() {
/** 是否交互式 */
let isInteractive = false;

if (configs.length > 1) {
if (configs.length > 1 && parseInteractiveMode(params.interactive)) {
// 有多个配置,则交互式选择
isInteractive = true;

Expand Down
12 changes: 12 additions & 0 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ export function createMultiselectOptions(
('schemaPath' in config ? config.schemaPath : 'Apifox Config'),
}));
}

export function parseInteractiveMode(value: unknown): boolean {
if (value === undefined) {
return true;
}

if (typeof value === 'boolean') {
return value;
}

return JSON.parse(value as string) === true;
}
13 changes: 13 additions & 0 deletions test/bin-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';

import { parseInteractiveMode } from '../src/bin/utils';

describe('parseInteractiveMode', () => {
it('defaults interactive mode to enabled', () => {
expect(parseInteractiveMode(undefined)).toBe(true);
});

it('allows disabling interactive mode with false', () => {
expect(parseInteractiveMode('false')).toBe(false);
});
});
Loading