Skip to content

Commit b6925ca

Browse files
committed
feat: 丰富命令行操作
1 parent 8222d04 commit b6925ca

File tree

12 files changed

+72
-19
lines changed

12 files changed

+72
-19
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ npm i -D openapi-axios
3333
const { defineConfig } = require('openapi-axios');
3434

3535
module.exports = defineConfig({
36-
modules: {
37-
// 将会生成 src/apis/petStore3.ts 文件
38-
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
36+
modules: {
37+
// 将会生成 src/apis/petStore3.ts 文件
38+
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
3939
},
40-
],
4140
});
4241
```
4342

bin/index.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
22

3-
const { run } = require('../dist/index.cjs');
3+
const { createCLI } = require('../dist/index.cjs');
44

5-
run();
5+
createCLI();

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"license": "MIT",
6565
"dependencies": {
6666
"chalk": "^4.1.2",
67+
"commander": "^12.1.0",
6768
"prettier": "^3.3.2",
6869
"strict-event-emitter": "^0.5.1",
6970
"try-flatten": "^1.3.2"

src/cli.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Command } from 'commander';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import { description } from '../package.json';
5+
import { pkgName, pkgVersion } from './const';
6+
import { resolveConfigFile, run } from './generators/command';
7+
8+
export function createCLI() {
9+
const program = new Command();
10+
11+
program
12+
.name(pkgName)
13+
.version(pkgVersion)
14+
.description(description)
15+
.action((options, command) => {
16+
if (command.args.length === 0) return run();
17+
18+
program.help();
19+
});
20+
21+
program
22+
.command('init')
23+
.description('初始化配置文件')
24+
.action(async () => {
25+
const configFile = resolveConfigFile(process.cwd());
26+
27+
if (configFile) {
28+
console.log(`配置文件已存在`, path.relative(process.cwd(), configFile));
29+
return;
30+
}
31+
32+
const configFilename = 'openapi.config.cjs';
33+
const configFilePath = path.join(process.cwd(), configFilename);
34+
35+
fs.writeFileSync(
36+
configFilePath,
37+
`
38+
/**
39+
* openapi-axios config
40+
* @link https://github.com/FrontEndDev-org/openapi-axios
41+
*/
42+
43+
const { defineConfig } = require('openapi-axios');
44+
45+
module.exports = defineConfig({
46+
modules: {
47+
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
48+
},
49+
});`.trim() + '\n',
50+
);
51+
console.log('生成配置文件', configFilename);
52+
});
53+
54+
program.parse();
55+
}

src/generators/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function resolveConfig(cwd: string): GeneratorOptions {
3030
const configFile = resolveConfigFile(cwd);
3131

3232
if (!configFile) {
33-
throw new Error(`配置文件未找到,配置文件可以是 ${configFileNameOrder.join('、')} 之一`);
33+
throw new Error(`配置文件未找到,配置文件可以是 ${configFileNameOrder.join('、')} 之一\n可以使用 npx openapi-axios init 自动生成`);
3434
}
3535

3636
const [err, config] = tryFlatten(() => {

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
export { defineConfig } from './generators/command';
2+
export { createCLI } from './cli';
13
export * from './const';
2-
export { Printer } from './printer';
3-
export { Generator } from './generators/Generator';
4-
export * from './generators/command';
54

65
// types
76
export type * from './types/openapi';

test/printer/ref-parameter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Printer } from '../../src';
1+
import { Printer } from '../../src/printer';
22

33
test('ref-parameter', () => {
44
const printer = new Printer({

test/printer/ref-request.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Printer } from '../../src';
1+
import { Printer } from '../../src/printer';
22

33
test('ref-request', () => {
44
const printer = new Printer({

test/printer/ref-response.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Printer } from '../../src';
1+
import { Printer } from '../../src/printer';
22

33
test('ref-response', () => {
44
const printer = new Printer({

0 commit comments

Comments
 (0)