Skip to content

Commit

Permalink
feat: use default config file request.config.ts and support array con…
Browse files Browse the repository at this point in the history
…figuration
  • Loading branch information
liangskyli committed Apr 20, 2023
1 parent df55e8f commit 5abcacd
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 29 deletions.
5 changes: 3 additions & 2 deletions packages/openapi-gen-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/test/gen-ts-dir/
/test/openapi/openapiv3.json
/test/gen-ts-dir/schema-api/
/test/gen-ts-dir2/schema-api/
/test/openapi/openapiv3.json
45 changes: 28 additions & 17 deletions packages/openapi-gen-ts/src/cli/code-gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { colors, getAbsolutePath, getConfig } from '@liangskyli/utils';
import { colors, getAbsolutePath, getConfig, lodash } from '@liangskyli/utils';
import { program } from 'commander';
import fs from 'fs-extra';
import type { IGenTsDataOpts } from '../gen';
import type { IGenTsDataOpts, IGenTsDataOptsCLI } from '../gen';
import genTsData from '../gen';

const commandCodeGenCli = (version: string) => {
Expand All @@ -10,30 +10,41 @@ const commandCodeGenCli = (version: string) => {
.option('-c, --configFile [configFile]', 'config file')
.parse(process.argv);

const { configFile } = program.opts();
let { configFile } = program.opts();

if (!configFile) {
console.error(colors.red('-c, --configFile [configFile] field need'));
process.exit(1);
configFile = './request.config.ts';
}
const configFilePath = getAbsolutePath(configFile);
if (!fs.existsSync(configFilePath)) {
if (fs.existsSync(configFilePath)) {
console.info(colors.green(`use configFile path: ${configFile}`));
} else {
console.error(colors.red(`-c, --configFile path not exits: ${configFile}`));
process.exit(1);
}

const data: IGenTsDataOpts = getConfig(configFilePath);
if (!data.openapiPath) {
console.error(
colors.red(`config file need openapiPath field: ${configFile}`),
);
}
let opts: IGenTsDataOptsCLI = getConfig(configFilePath);

try {
genTsData(data).then();
} catch (err: any) {
console.error(err);
}
const runningScript = async () => {
try {
if (!lodash.isArray(opts)) {
opts = [opts] as IGenTsDataOpts[];
}
for (let i = 0; i < opts.length; i++) {
const singleOpts = opts[i];
if (!singleOpts.openapiPath) {
console.error(
colors.red(`config file need openapiPath field: ${configFile}`),
);
}
await genTsData(singleOpts);
}
} catch (err: any) {
console.error(err);
}
};

runningScript();
};

export { commandCodeGenCli };
2 changes: 1 addition & 1 deletion packages/openapi-gen-ts/src/gen/gen-interface-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const genInterfaceFile = async (opts: IOpts) => {
const bodyInterfaces = bodyMediaTypes.map((bodyMediaType) => {
let bodyInterface = `paths['${url}']['${method}']['requestBody']['content']['${bodyMediaType}']`;
if (!requestBodyRequired) {
bodyInterface = `(paths['${url}']['${method}']['requestBody'] & {})['content']['${bodyMediaType}']`;
bodyInterface = `(paths['${url}']['${method}']['requestBody'] & Record<string, never>)['content']['${bodyMediaType}']`;
}
return omitKeys
? `Omit<${bodyInterface}, ${omitKeys}>`
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-gen-ts/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export type IGenTsDataOpts = {
requestBodyOmit?: string[];
};

export type IGenTsDataOptsCLI = IGenTsDataOpts | IGenTsDataOpts[];

const genTsData = async (opts: IGenTsDataOpts) => {
const {
genTsDir = './',
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-gen-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type PartialAll<T> = {
[P in keyof T]?: PartialAll<T[P]>;
};

export type { IGenTsDataOpts } from './gen/index';
export type { IGenTsDataOpts, IGenTsDataOptsCLI } from './gen/index';
export type { IAPIRequest, PartialAll, Definition };
export { commandCodeGenCli };
export default genTsData;
Empty file.
Empty file.
7 changes: 0 additions & 7 deletions packages/openapi-gen-ts/test/http/config.cli.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/openapi-gen-ts/test/http/gen-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as spawn from 'cross-spawn';

const result = spawn.sync(
'node',
'bin/index.js -c ./test/http/config.cli.ts'.split(' '),
'bin/index.js -c ./test/http/request.config.ts'.split(' '),
{
stdio: 'inherit',
},
Expand Down
13 changes: 13 additions & 0 deletions packages/openapi-gen-ts/test/http/request.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { IGenTsDataOptsCLI } from '../../lib';

const config: IGenTsDataOptsCLI = [
{
genTsDir: './test/gen-ts-dir',
openapiPath: './test/openapi/openapiv3-example.json',
},
{
genTsDir: './test/gen-ts-dir2',
openapiPath: './test/openapi/openapiv3-example.json',
},
];
export default config;

0 comments on commit 5abcacd

Please sign in to comment.