| 335 | } |
| 336 | |
| 337 | export function makeCLIPrompt(): Prompt { |
| 338 | return async ({defaultValue, message, multiple, options, required, type}) => { |
| 339 | if (!options?.length && multiple) { |
| 340 | throw new Error('Multiple selection requires options to be provided'); |
| 341 | } |
| 342 | if (type === 'string') { |
| 343 | if (options) { |
| 344 | if (multiple) { |
| 345 | return checkbox<string>({ |
| 346 | message, |
| 347 | choices: (options as string[]).map((key) => ({value: key})), |
| 348 | }); |
| 349 | } |
| 350 | return search<string>({ |
| 351 | message, |
| 352 | source: filterInput<string>(options as string[]), |
| 353 | pageSize: 10, |
| 354 | }); |
| 355 | } |
| 356 | |
| 357 | return input({ |
| 358 | message, |
| 359 | default: defaultValue as string | undefined, |
| 360 | required, |
| 361 | }); |
| 362 | } |
| 363 | if (type === 'number') { |
| 364 | if (options) { |
| 365 | throw new Error('Number type does not support options'); |
| 366 | } |
| 367 | const res = await input({ |
| 368 | message, |
| 369 | default: defaultValue?.toString() as string | undefined, |
| 370 | required, |
| 371 | }); |
| 372 | const num = Number(res); |
| 373 | if (isNaN(num)) { |
| 374 | throw new Error(`Input for ${message} must be a number`); |
| 375 | } |
| 376 | return num as any; // For some reason setting this to any fixes the type issue for this function |
| 377 | } |
| 378 | if (type === 'boolean') { |
| 379 | return confirm({ |
| 380 | message, |
| 381 | default: defaultValue as boolean | undefined, |
| 382 | }); |
| 383 | } |
| 384 | |
| 385 | throw new Error(`Unsupported prompt type: ${type}`); |
| 386 | }; |
| 387 | } |
| 388 | |
| 389 | export function formatErrorCauses(e: Error): string { |
| 390 | let message = e.message; |