(name: string, validItems: T[], options?: ListSelectionDefOptions<T>)
| 154 | * Create an `InputDefinition` for selecting items from a list. |
| 155 | */ |
| 156 | export const listSelectionDef = <T=string>(name: string, validItems: T[], options?: ListSelectionDefOptions<T>): InputDefinition<T> => { |
| 157 | const summarizeForEdit = options?.summarizeForEdit ?? stringFromUnknown |
| 158 | |
| 159 | const updateFromUserInput = async (original?: T): Promise<T | CancelAction> => { |
| 160 | const choices: Choice<T | CancelAction>[] = validItems.map((validItem: T) => ({ |
| 161 | name: summarizeForEdit(validItem), |
| 162 | value: validItem, |
| 163 | })) |
| 164 | choices.push(cancelOption) |
| 165 | |
| 166 | const chosen = await select({ message: `Select ${name}:`, choices, default: original }) |
| 167 | |
| 168 | return chosen |
| 169 | } |
| 170 | |
| 171 | const buildFromUserInput = async (): Promise<T | CancelAction> => updateFromUserInput(options?.default) |
| 172 | |
| 173 | return { name, buildFromUserInput, summarizeForEdit, updateFromUserInput } |
| 174 | } |
| 175 | |
| 176 | export type OptionalDefPredicateFn = (context?: unknown[]) => boolean |
| 177 | export type OptionalDefOptions = { |
no outgoing calls
no test coverage detected