Skip to content

Commit

Permalink
refactor: fix output variable bug and add support for swagger version
Browse files Browse the repository at this point in the history
  • Loading branch information
gogoyqj committed Jul 2, 2023
1 parent 5b37dbf commit d413e0d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/initConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const schema = {
description: '请输入接口文档本地存储地址 url,例如:./swagger.json',
required: false
},
swaggerVersion: {
description: '请输入 swagger 接口文档版本,例如:v2、v3,默认 v2(如果接口文档类型是 yapi 则可直接忽略)',
required: false
},
outputType: {
description: '请输入 service 代码输出类型,例如:services(生成类型和接口调用代码), types(仅生成类型), global(生成通过同一个全局方法重载实现类型化调用的代码),默认 services',
required: false
},
output: {
description: '请输入 service 代码输出目录 -o,例如: src/services',
required: false
Expand All @@ -26,7 +34,9 @@ const schema = {
};

export default function initConfig(ConfigFile: string) {
const defaultConfigJSON = {
const defaultConfigJSON: Omit<Autos.JSON2Service, 'swaggerParser'> & {
swaggerParser: Partial<Autos.JSON2Service['swaggerParser']>
} = {
url: './swagger.json',
remoteUrl: 'http:https://**',
type: 'swagger',
Expand All @@ -40,11 +50,15 @@ export default function initConfig(ConfigFile: string) {
};
prompt.start();
prompt.get(schema, function(err, result) {
const { type, remoteUrl, url, '-o': output } = result as any;
const { type, remoteUrl, url, output, swaggerVersion, outputType } = result as any;
type && (defaultConfigJSON.type = type);
remoteUrl && (defaultConfigJSON.remoteUrl = remoteUrl);
url && (defaultConfigJSON.url = url);
output && (defaultConfigJSON.swaggerParser['-o'] = output);
output && (defaultConfigJSON.swaggerParser!['-o'] = output);
// 'plugins/typescript-tkit' | 'plugins/types-only' | 'plugins/typescript-tkit-autos' | 'v3/plugins/typescript-tkit' | 'v3/plugins/types-only' | 'v3/plugins/typescript-tkit-autos'
if (['v2', 'v3'].indexOf(swaggerVersion) !== -1 || ['services', 'types', 'global'].indexOf(outputType) !== -1) {
defaultConfigJSON.swaggerParser!['-t'] = `${swaggerVersion === 'v3' ? 'v3/' : ''}plugins/${outputType === 'global' ? 'typescript-tkit-autos' : outputType === 'types' ? 'types-only' : 'typescript-tkit'}`;
}

fs.writeFileSync(
ConfigFile,
Expand Down

0 comments on commit d413e0d

Please sign in to comment.