|
| 1 | +import commander from 'commander'; |
| 2 | +import * as inquirer from 'inquirer'; |
| 3 | + |
| 4 | +/** |
| 5 | + * A namespace for handling command-line argument parsing and interactive prompts. |
| 6 | + */ |
| 7 | +export namespace ArgumentParser { |
| 8 | + /** |
| 9 | + * A type representing an inquirer function that interacts with the user. |
| 10 | + * |
| 11 | + * @template T - The type of the options object to be returned. |
| 12 | + * @param command - The commander command instance. |
| 13 | + * @param prompt - Function to create a prompt module. |
| 14 | + * @param action - Function to execute the action. |
| 15 | + */ |
| 16 | + export type Inquirer<T> = ( |
| 17 | + command: commander.Command, |
| 18 | + prompt: (opt?: inquirer.SeparatorOptions) => inquirer.PromptModule, |
| 19 | + action: (closure: (options: Partial<T>) => Promise<T>) => Promise<T>, |
| 20 | + ) => Promise<T>; |
| 21 | + |
| 22 | + /** |
| 23 | + * Interface defining the structure of the prompt utility functions. |
| 24 | + */ |
| 25 | + export interface Prompt { |
| 26 | + select: (name: string) => (message: string) => <Choice extends string>(choices: Choice[]) => Promise<Choice>; |
| 27 | + boolean: (name: string) => (message: string) => Promise<boolean>; |
| 28 | + number: (name: string) => (message: string, init?: number) => Promise<number>; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Parses command-line arguments and interacts with the user using prompts. |
| 33 | + * |
| 34 | + * @template T - The type of the options object to be returned. |
| 35 | + * @param inquiry - A function that defines the interaction logic. |
| 36 | + * @returns A promise resolving to the options object of type T. |
| 37 | + */ |
| 38 | + export const parse = async <T>( |
| 39 | + inquiry: ( |
| 40 | + commad: commander.Command, |
| 41 | + prompt: Prompt, |
| 42 | + action: (closure: (options: Partial<T>) => Promise<T>) => Promise<T>, |
| 43 | + ) => Promise<T>, |
| 44 | + ): Promise<T> => { |
| 45 | + /** |
| 46 | + * Wraps the action logic in a promise to handle command execution. |
| 47 | + * |
| 48 | + * @param closure - A function that processes the options and returns a promise. |
| 49 | + * @returns A promise resolving to the options object of type T. |
| 50 | + */ |
| 51 | + const action = (closure: (options: Partial<T>) => Promise<T>) => |
| 52 | + new Promise<T>((resolve, reject) => { |
| 53 | + commander.program.action(async (options) => { |
| 54 | + try { |
| 55 | + resolve(await closure(options)); |
| 56 | + } catch (exp) { |
| 57 | + reject(exp); |
| 58 | + } |
| 59 | + }); |
| 60 | + commander.program.parseAsync().catch(reject); |
| 61 | + }); |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a select prompt for choosing from a list of options. |
| 65 | + * |
| 66 | + * @param name - The name of the prompt. |
| 67 | + * @param message - The message to display to the user. |
| 68 | + * @returns A function that takes choices and returns a promise resolving to the selected choice. |
| 69 | + */ |
| 70 | + const select = |
| 71 | + (name: string) => |
| 72 | + (message: string) => |
| 73 | + async <Choice extends string>(choices: Choice[]) => |
| 74 | + ( |
| 75 | + await inquirer.createPromptModule()({ |
| 76 | + type: 'list', |
| 77 | + name, |
| 78 | + message, |
| 79 | + choices, |
| 80 | + }) |
| 81 | + )[name]; |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a boolean prompt for yes/no questions. |
| 85 | + * |
| 86 | + * @param name - The name of the prompt. |
| 87 | + * @returns A function that takes a message and returns a promise resolving to a boolean. |
| 88 | + */ |
| 89 | + const boolean = (name: string) => async (message: string) => |
| 90 | + ( |
| 91 | + await inquirer.createPromptModule()({ |
| 92 | + type: 'confirm', |
| 93 | + name, |
| 94 | + message, |
| 95 | + }) |
| 96 | + )[name] as boolean; |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a number prompt for numeric input. |
| 100 | + * |
| 101 | + * @param name - The name of the prompt. |
| 102 | + * @returns A function that takes a message and an optional initial value, returning a promise resolving to a number. |
| 103 | + */ |
| 104 | + const number = (name: string) => async (message: string, init?: number) => { |
| 105 | + const value = Number( |
| 106 | + ( |
| 107 | + await inquirer.createPromptModule()({ |
| 108 | + type: 'number', |
| 109 | + name, |
| 110 | + message, |
| 111 | + }) |
| 112 | + )[name], |
| 113 | + ); |
| 114 | + return init !== undefined && isNaN(value) ? init : value; |
| 115 | + }; |
| 116 | + |
| 117 | + const output: T | Error = await (async () => { |
| 118 | + try { |
| 119 | + return await inquiry(commander.program, { select, boolean, number }, action); |
| 120 | + } catch (error) { |
| 121 | + return error as Error; |
| 122 | + } |
| 123 | + })(); |
| 124 | + |
| 125 | + if (output instanceof Error) throw output; |
| 126 | + return output; |
| 127 | + }; |
| 128 | +} |
0 commit comments