Skip to content

Commit

Permalink
feat: 丰富命令行操作
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 22, 2024
1 parent 8222d04 commit b6925ca
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 19 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ npm i -D openapi-axios
const { defineConfig } = require('openapi-axios');

module.exports = defineConfig({
modules: {
// 将会生成 src/apis/petStore3.ts 文件
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
modules: {
// 将会生成 src/apis/petStore3.ts 文件
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
},
],
});
```

Expand Down
4 changes: 2 additions & 2 deletions bin/index.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

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

run();
createCLI();
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"license": "MIT",
"dependencies": {
"chalk": "^4.1.2",
"commander": "^12.1.0",
"prettier": "^3.3.2",
"strict-event-emitter": "^0.5.1",
"try-flatten": "^1.3.2"
Expand Down
55 changes: 55 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Command } from 'commander';
import fs from 'fs';
import path from 'path';
import { description } from '../package.json';
import { pkgName, pkgVersion } from './const';
import { resolveConfigFile, run } from './generators/command';

export function createCLI() {
const program = new Command();

program
.name(pkgName)
.version(pkgVersion)
.description(description)
.action((options, command) => {
if (command.args.length === 0) return run();

program.help();
});

program
.command('init')
.description('初始化配置文件')
.action(async () => {
const configFile = resolveConfigFile(process.cwd());

if (configFile) {
console.log(`配置文件已存在`, path.relative(process.cwd(), configFile));
return;
}

const configFilename = 'openapi.config.cjs';
const configFilePath = path.join(process.cwd(), configFilename);

fs.writeFileSync(
configFilePath,
`
/**
* openapi-axios config
* @link https://github.com/FrontEndDev-org/openapi-axios
*/
const { defineConfig } = require('openapi-axios');
module.exports = defineConfig({
modules: {
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
},
});`.trim() + '\n',
);
console.log('生成配置文件', configFilename);
});

program.parse();
}
2 changes: 1 addition & 1 deletion src/generators/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function resolveConfig(cwd: string): GeneratorOptions {
const configFile = resolveConfigFile(cwd);

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

const [err, config] = tryFlatten(() => {
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { defineConfig } from './generators/command';
export { createCLI } from './cli';
export * from './const';
export { Printer } from './printer';
export { Generator } from './generators/Generator';
export * from './generators/command';

// types
export type * from './types/openapi';
Expand Down
2 changes: 1 addition & 1 deletion test/printer/ref-parameter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Printer } from '../../src';
import { Printer } from '../../src/printer';

test('ref-parameter', () => {
const printer = new Printer({
Expand Down
2 changes: 1 addition & 1 deletion test/printer/ref-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Printer } from '../../src';
import { Printer } from '../../src/printer';

test('ref-request', () => {
const printer = new Printer({
Expand Down
2 changes: 1 addition & 1 deletion test/printer/ref-response.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Printer } from '../../src';
import { Printer } from '../../src/printer';

test('ref-response', () => {
const printer = new Printer({
Expand Down
3 changes: 1 addition & 2 deletions test/printer/unique-name.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from 'vitest';
import { Printer } from '../../src';
import { Printer } from '../../src/printer';

test('unique name', () => {
const printer = new Printer({
Expand Down
2 changes: 1 addition & 1 deletion test/printer/v31.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Printer } from '../../src';
import { Printer } from '../../src/printer';

test('v3.1 schema', () => {
const printer = new Printer({
Expand Down

0 comments on commit b6925ca

Please sign in to comment.