|
1 | 1 | #!/usr/bin/env node |
| 2 | +import { cancel, intro, isCancel, multiselect, outro } from '@clack/prompts'; |
2 | 3 | import { type OptionValues, program } from 'commander'; |
3 | 4 | import { pickBy } from 'lodash'; |
4 | 5 | import { join } from 'path'; |
@@ -171,49 +172,61 @@ async function run() { |
171 | 172 | if (cnf) { |
172 | 173 | const tasks = []; |
173 | 174 | let configs: GenerateServiceProps[] = Array.isArray(cnf) ? cnf : [cnf]; |
| 175 | + /** 是否交互式 */ |
| 176 | + let isInteractive = false; |
174 | 177 |
|
175 | 178 | if (params.uniqueKey) { |
176 | 179 | configs = configs.filter( |
177 | 180 | (config) => config.uniqueKey === params.uniqueKey |
178 | 181 | ); |
| 182 | + } else if (configs.length > 1) { |
| 183 | + // 如果没有指定 uniqueKey,并且有多个配置,则交互式选择 |
| 184 | + isInteractive = true; |
| 185 | + |
| 186 | + console.log(''); // 添加一个空行 |
| 187 | + intro('🎉 欢迎使用 openapi-ts-request 生成器'); |
| 188 | + const selected = await multiselect({ |
| 189 | + message: '请选择要生成的 service', |
| 190 | + options: configs.map((config) => ({ |
| 191 | + value: config, |
| 192 | + label: config.describe || config.schemaPath, |
| 193 | + })), |
| 194 | + }); |
| 195 | + |
| 196 | + if (isCancel(selected)) { |
| 197 | + cancel('👋 Has cancelled'); |
| 198 | + process.exit(0); |
| 199 | + } |
| 200 | + |
| 201 | + configs = selected; |
179 | 202 | } |
180 | 203 |
|
181 | 204 | for (const config of configs) { |
182 | 205 | tasks.push(generateService(config)); |
183 | 206 | } |
184 | 207 |
|
185 | 208 | const results = await Promise.allSettled(tasks); |
186 | | - const errors: PromiseRejectedResult[] = results.filter( |
187 | | - (result) => result.status === 'rejected' |
188 | | - ); |
189 | 209 | let errorMsg = ''; |
190 | 210 |
|
191 | | - for (let i = 0; i < errors.length; i++) { |
192 | | - const error = errors[i]; |
193 | | - const cnf = configs[i]; |
194 | | - errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${error.reason}\n`; |
| 211 | + for (let i = 0; i < results.length; i++) { |
| 212 | + const result = results[i]; |
| 213 | + if (result.status === 'rejected') { |
| 214 | + const cnf = configs[i]; |
| 215 | + errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${result.reason}\n`; |
| 216 | + } |
195 | 217 | } |
196 | 218 |
|
197 | 219 | if (errorMsg) { |
198 | | - logError(errorMsg); |
199 | | - process.exit(1); |
200 | | - } |
201 | | - } else { |
202 | | - if (!params.input || !params.output) { |
203 | | - logError( |
204 | | - 'Please provide either input/output options or a configuration file path and name.' |
205 | | - ); |
206 | | - process.exit(1); |
| 220 | + throw new Error(errorMsg); |
207 | 221 | } |
208 | 222 |
|
209 | | - const options = baseGenerate(params); |
210 | | - await generateService( |
211 | | - pickBy( |
212 | | - options, |
213 | | - (value) => value !== null && value !== undefined && value !== '' |
214 | | - ) as GenerateServiceProps |
| 223 | + if (isInteractive && !errorMsg) { |
| 224 | + outro('🎉 All done!'); |
| 225 | + } |
| 226 | + } else { |
| 227 | + throw new Error( |
| 228 | + 'Please provide either input/output options or a configuration file path and name.' |
215 | 229 | ); |
216 | | - process.exit(0); |
217 | 230 | } |
218 | 231 | } catch (error) { |
219 | 232 | logError(error); |
|
0 commit comments