Skip to content

Commit 0c1c506

Browse files
chore: clean code
1 parent b845463 commit 0c1c506

File tree

5 files changed

+55
-83
lines changed

5 files changed

+55
-83
lines changed

README-en_US.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ export default defineConfig({
5151
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
5252
serversPath: './src/apis',
5353
});
54-
55-
//
56-
57-
// import type { GenerateServiceProps } from 'openapi-ts-request';
58-
59-
// export default {
60-
// schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
61-
// } as GenerateServiceProps;
6254
```
6355

6456
support passing in array config for generate
@@ -76,21 +68,6 @@ export default defineConfig([
7668
serversPath: './src/apis/auth',
7769
},
7870
]);
79-
80-
// or
81-
82-
// import type { GenerateServiceProps } from 'openapi-ts-request';
83-
84-
// export default [
85-
// {
86-
// schemaPath: 'http://app.swagger.io/v2/swagger.json',
87-
// serversPath: './src/apis/app',
88-
// },
89-
// {
90-
// schemaPath: 'http://auth.swagger.io/v2/swagger.json',
91-
// serversPath: './src/apis/auth',
92-
// },
93-
// ] as GenerateServiceProps[];
9471
```
9572

9673
add the command in `script` of `package.json`: `"openapi": "openapi-ts",`

README.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ export default defineConfig({
5151
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
5252
serversPath: './src/apis',
5353
});
54-
55-
//
56-
57-
// import type { GenerateServiceProps } from 'openapi-ts-request';
58-
59-
// export default {
60-
// schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
61-
// } as GenerateServiceProps;
6254
```
6355

6456
支持传入数组配置进行生成
@@ -76,21 +68,6 @@ export default defineConfig([
7668
serversPath: './src/apis/auth',
7769
},
7870
]);
79-
80-
//
81-
82-
// import type { GenerateServiceProps } from 'openapi-ts-request';
83-
84-
// export default [
85-
// {
86-
// schemaPath: 'http://app.swagger.io/v2/swagger.json',
87-
// serversPath: './src/apis/app',
88-
// },
89-
// {
90-
// schemaPath: 'http://auth.swagger.io/v2/swagger.json',
91-
// serversPath: './src/apis/auth',
92-
// },
93-
// ] as GenerateServiceProps[];
9471
```
9572

9673
`package.json``script` 中添加命令: `"openapi": "openapi-ts",`

src/bin/cli.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
22
import { cancel, intro, isCancel, multiselect, outro } from '@clack/prompts';
3-
import chalk from 'chalk';
43
import { program } from 'commander';
54

65
import type { GenerateServiceProps } from '../index';
76
import { generateService } from '../index';
7+
import { logError } from '../log';
88
import { readConfig } from '../readConfig';
99

1010
program
@@ -15,6 +15,11 @@ program
1515
program.parse();
1616
const options = program.opts();
1717

18+
/**
19+
* 1. 执行 cli 命令读取配置文件,已经使用 openapi.ts 替代了 cli.ts,后期会废弃 cli.ts
20+
* 2. 如果配置文件中有 uniqueKey,则根据 uniqueKey 生成 service
21+
* 3. 如果配置文件中没有 uniqueKey,且有多个 service,则交互式选择要生成的 service
22+
*/
1823
async function run() {
1924
const config = await readConfig<
2025
GenerateServiceProps | GenerateServiceProps[]
@@ -65,29 +70,29 @@ async function run() {
6570
}
6671

6772
const results = await Promise.allSettled(tasks);
68-
const errors: PromiseRejectedResult[] = results.filter(
69-
(result) => result.status === 'rejected'
70-
);
7173
let errorMsg = '';
7274

73-
for (let i = 0; i < errors.length; i++) {
74-
const error = errors[i];
75-
const cnf = configs[i];
76-
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${error.reason}\n`;
75+
for (let i = 0; i < results.length; i++) {
76+
const result = results[i];
77+
if (result.status === 'rejected') {
78+
const cnf = configs[i];
79+
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${result.reason}\n`;
80+
}
7781
}
7882

7983
if (errorMsg) {
8084
throw new Error(errorMsg);
8185
}
8286

83-
if (isInteractive && !errors.length) {
87+
if (isInteractive && !errorMsg) {
8488
outro('🎉 All done!');
8589
}
8690
} else {
8791
throw new Error('config is not found');
8892
}
8993
} catch (error) {
90-
console.log(chalk.red(error));
94+
logError(error);
95+
process.exit(1);
9196
}
9297
}
9398

src/bin/openapi.ts

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import { cancel, intro, isCancel, multiselect, outro } from '@clack/prompts';
23
import { type OptionValues, program } from 'commander';
34
import { pickBy } from 'lodash';
45
import { join } from 'path';
@@ -171,49 +172,61 @@ async function run() {
171172
if (cnf) {
172173
const tasks = [];
173174
let configs: GenerateServiceProps[] = Array.isArray(cnf) ? cnf : [cnf];
175+
/** 是否交互式 */
176+
let isInteractive = false;
174177

175178
if (params.uniqueKey) {
176179
configs = configs.filter(
177180
(config) => config.uniqueKey === params.uniqueKey
178181
);
182+
} else if (configs.length > 1) {
183+
// 如果没有指定 uniqueKey,并且有多个配置,则交互式选择
184+
isInteractive = true;
185+
186+
console.log(''); // 添加一个空行
187+
intro('🎉 欢迎使用 openapi-ts-request 生成器');
188+
const selected = await multiselect({
189+
message: '请选择要生成的 service',
190+
options: configs.map((config) => ({
191+
value: config,
192+
label: config.describe || config.schemaPath,
193+
})),
194+
});
195+
196+
if (isCancel(selected)) {
197+
cancel('👋 Has cancelled');
198+
process.exit(0);
199+
}
200+
201+
configs = selected;
179202
}
180203

181204
for (const config of configs) {
182205
tasks.push(generateService(config));
183206
}
184207

185208
const results = await Promise.allSettled(tasks);
186-
const errors: PromiseRejectedResult[] = results.filter(
187-
(result) => result.status === 'rejected'
188-
);
189209
let errorMsg = '';
190210

191-
for (let i = 0; i < errors.length; i++) {
192-
const error = errors[i];
193-
const cnf = configs[i];
194-
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${error.reason}\n`;
211+
for (let i = 0; i < results.length; i++) {
212+
const result = results[i];
213+
if (result.status === 'rejected') {
214+
const cnf = configs[i];
215+
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${result.reason}\n`;
216+
}
195217
}
196218

197219
if (errorMsg) {
198-
logError(errorMsg);
199-
process.exit(1);
200-
}
201-
} else {
202-
if (!params.input || !params.output) {
203-
logError(
204-
'Please provide either input/output options or a configuration file path and name.'
205-
);
206-
process.exit(1);
220+
throw new Error(errorMsg);
207221
}
208222

209-
const options = baseGenerate(params);
210-
await generateService(
211-
pickBy(
212-
options,
213-
(value) => value !== null && value !== undefined && value !== ''
214-
) as GenerateServiceProps
223+
if (isInteractive && !errorMsg) {
224+
outro('🎉 All done!');
225+
}
226+
} else {
227+
throw new Error(
228+
'Please provide either input/output options or a configuration file path and name.'
215229
);
216-
process.exit(0);
217230
}
218231
} catch (error) {
219232
logError(error);

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ export type GenerateServiceProps = {
3434
* Swagger2/OpenAPI3 地址
3535
*/
3636
schemaPath: string;
37-
/**
38-
* 描述信息,在用 cli 可交互运行方式时会用到
39-
*/
40-
describe?: string;
4137
/**
4238
* 生成的文件夹的路径
4339
*/
@@ -53,6 +49,10 @@ export type GenerateServiceProps = {
5349
* 是否全量替换, 默认: true, 如果为false, 则进行增量替换
5450
*/
5551
full?: boolean;
52+
/**
53+
* 描述信息,在用 cli 可交互运行方式时会用到
54+
*/
55+
describe?: string;
5656
/**
5757
* 开启日志
5858
*/

0 commit comments

Comments
 (0)