|
1 | 1 | #!/usr/bin/env node |
2 | | -import { program } from 'commander'; |
| 2 | +import { type OptionValues, program } from 'commander'; |
3 | 3 | import { pickBy } from 'lodash'; |
4 | 4 | import { join } from 'path'; |
5 | 5 |
|
@@ -109,7 +109,52 @@ function getPath(path: string) { |
109 | 109 | return join(process.cwd(), path); |
110 | 110 | } |
111 | 111 |
|
| 112 | +const baseGenerate = (_params_: OptionValues): GenerateServiceProps => { |
| 113 | + const input = getPath(_params_.input as string); |
| 114 | + const output = getPath(_params_.output as string); |
| 115 | + const options: GenerateServiceProps = { |
| 116 | + schemaPath: input, |
| 117 | + serversPath: output, |
| 118 | + requestLibPath: _params_.requestLibPath as string, |
| 119 | + enableLogging: JSON.parse(_params_.enableLogging as string) === true, |
| 120 | + priorityRule: _params_.priorityRule as IPriorityRule, |
| 121 | + includeTags: _params_.includeTags as string[], |
| 122 | + includePaths: _params_.includePaths as string[], |
| 123 | + excludeTags: _params_.excludeTags as string[], |
| 124 | + excludePaths: _params_.excludePaths as string[], |
| 125 | + requestOptionsType: _params_.requestOptionsType as string, |
| 126 | + apiPrefix: _params_.apiPrefix as string, |
| 127 | + isGenReactQuery: JSON.parse(_params_.isGenReactQuery as string) === true, |
| 128 | + reactQueryMode: _params_.reactQueryMode as IReactQueryMode, |
| 129 | + isGenJavaScript: JSON.parse(_params_.isGenJavaScript as string) === true, |
| 130 | + isDisplayTypeLabel: |
| 131 | + JSON.parse(_params_.isDisplayTypeLabel as string) === true, |
| 132 | + isGenJsonSchemas: JSON.parse(_params_.isGenJsonSchemas as string) === true, |
| 133 | + mockFolder: _params_.mockFolder as string, |
| 134 | + authorization: _params_.authorization as string, |
| 135 | + nullable: JSON.parse(_params_.nullable as string) === true, |
| 136 | + isTranslateToEnglishTag: |
| 137 | + JSON.parse(_params_.isTranslateToEnglishTag as string) === true, |
| 138 | + isOnlyGenTypeScriptType: |
| 139 | + JSON.parse(_params_.isOnlyGenTypeScriptType as string) === true, |
| 140 | + isCamelCase: JSON.parse(_params_.isCamelCase as string) === true, |
| 141 | + isSupportParseEnumDesc: |
| 142 | + JSON.parse(_params_.isSupportParseEnumDesc as string) === true, |
| 143 | + }; |
| 144 | + return options; |
| 145 | +}; |
| 146 | + |
112 | 147 | async function run() { |
| 148 | + if (params.input && params.output) { |
| 149 | + const options = baseGenerate(params); |
| 150 | + await generateService( |
| 151 | + pickBy( |
| 152 | + options, |
| 153 | + (value) => value !== null && value !== undefined && value !== '' |
| 154 | + ) as GenerateServiceProps |
| 155 | + ); |
| 156 | + process.exit(0); |
| 157 | + } |
113 | 158 | const cnf = await readConfig<GenerateServiceProps | GenerateServiceProps[]>({ |
114 | 159 | fallbackName: 'openapi-ts-request', |
115 | 160 | filePath: params.configFilePath as string, |
@@ -148,40 +193,7 @@ async function run() { |
148 | 193 | ); |
149 | 194 | process.exit(1); |
150 | 195 | } |
151 | | - const input = getPath(params.input as string); |
152 | | - const output = getPath(params.output as string); |
153 | | - |
154 | | - const options: GenerateServiceProps = { |
155 | | - schemaPath: input, |
156 | | - serversPath: output, |
157 | | - requestLibPath: params.requestLibPath as string, |
158 | | - enableLogging: JSON.parse(params.enableLogging as string) === true, |
159 | | - priorityRule: params.priorityRule as IPriorityRule, |
160 | | - includeTags: params.includeTags as string[], |
161 | | - includePaths: params.includePaths as string[], |
162 | | - excludeTags: params.excludeTags as string[], |
163 | | - excludePaths: params.excludePaths as string[], |
164 | | - requestOptionsType: params.requestOptionsType as string, |
165 | | - apiPrefix: params.apiPrefix as string, |
166 | | - isGenReactQuery: JSON.parse(params.isGenReactQuery as string) === true, |
167 | | - reactQueryMode: params.reactQueryMode as IReactQueryMode, |
168 | | - isGenJavaScript: JSON.parse(params.isGenJavaScript as string) === true, |
169 | | - isDisplayTypeLabel: |
170 | | - JSON.parse(params.isDisplayTypeLabel as string) === true, |
171 | | - isGenJsonSchemas: |
172 | | - JSON.parse(params.isGenJsonSchemas as string) === true, |
173 | | - mockFolder: params.mockFolder as string, |
174 | | - authorization: params.authorization as string, |
175 | | - nullable: JSON.parse(params.nullable as string) === true, |
176 | | - isTranslateToEnglishTag: |
177 | | - JSON.parse(params.isTranslateToEnglishTag as string) === true, |
178 | | - isOnlyGenTypeScriptType: |
179 | | - JSON.parse(params.isOnlyGenTypeScriptType as string) === true, |
180 | | - isCamelCase: JSON.parse(params.isCamelCase as string) === true, |
181 | | - isSupportParseEnumDesc: |
182 | | - JSON.parse(params.isSupportParseEnumDesc as string) === true, |
183 | | - }; |
184 | | - |
| 196 | + const options = baseGenerate(params); |
185 | 197 | await generateService( |
186 | 198 | pickBy( |
187 | 199 | options, |
|
0 commit comments