(command: SmartThingsCommand<PromptUserFlags>, config: SelectFromListConfig<L>, options: PromptUserOptions<L, ID>)
| 72 | * @returns Selected id if one was chosen. Logs message if no items are found and exits. |
| 73 | */ |
| 74 | export async function promptUser<L extends object, ID = string>(command: SmartThingsCommand<PromptUserFlags>, |
| 75 | config: SelectFromListConfig<L>, options: PromptUserOptions<L, ID>): Promise<ID> { |
| 76 | const items = await options.listItems() |
| 77 | if (options.autoChoose && items.length === 1) { |
| 78 | return items[0][config.primaryKeyName] as unknown as ID |
| 79 | } |
| 80 | const list = await outputList( |
| 81 | command, |
| 82 | config, |
| 83 | async () => items, |
| 84 | { includeIndex: true, forUserQuery: true, customNotFoundMessage: options.customNotFoundMessage }, |
| 85 | ) |
| 86 | if (list.length === 0) { |
| 87 | // Nothing was found; user was already notified by `outputList` above. |
| 88 | // eslint-disable-next-line no-process-exit |
| 89 | process.exit(0) |
| 90 | } |
| 91 | |
| 92 | const getIdFromUser = (options.getIdFromUser ?? stringGetIdFromUser) as IdRetrievalFunction<ID, L> |
| 93 | return await getIdFromUser(config, list, options.promptMessage ?? promptFromNaming(config)) |
| 94 | } |
| 95 | |
| 96 | export type SelectFromListFlags = PromptUserFlags |
| 97 | export const selectFromListBuilder = promptUserBuilder |
no test coverage detected