( command: SmartThingsCommand<SelectFromListFlags>, config: SelectFromListConfig<L>, options: SelectOptions<L, ID>, )
| 139 | * @returns Selected id if one was chosen. Logs message if no items are found and exits. |
| 140 | */ |
| 141 | export async function selectFromList<L extends object, ID = string>( |
| 142 | command: SmartThingsCommand<SelectFromListFlags>, |
| 143 | config: SelectFromListConfig<L>, |
| 144 | options: SelectOptions<L, ID>, |
| 145 | ): Promise<ID> { |
| 146 | if (options.preselectedId) { |
| 147 | return options.preselectedId |
| 148 | } |
| 149 | |
| 150 | if (options.defaultValue) { |
| 151 | const configuredDefault = command.cliConfig.profile[options.defaultValue.configKey] as ID |
| 152 | if (configuredDefault) { |
| 153 | try { |
| 154 | const item = await options.defaultValue.getItem(configuredDefault) |
| 155 | if (item) { |
| 156 | console.log(options.defaultValue.userMessage(item)) |
| 157 | return configuredDefault |
| 158 | } |
| 159 | } catch (error) { |
| 160 | if (!error.response || error.response.status !== 404 && error.response.status !== 403) { |
| 161 | throw error |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | await resetManagedConfigKey(command.cliConfig, options.defaultValue.configKey) |
| 166 | command.logger.debug(`removed ${options.defaultValue.configKey} from ${command.cliConfig.profileName}`) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | const userSelected = await promptUser(command, config, options) |
| 171 | |
| 172 | const neverAgainKey = `${options.defaultValue?.configKey ?? ''}::neverAskForSaveAgain` |
| 173 | if (options.defaultValue && !command.cliConfig.booleanConfigValue(neverAgainKey)) { |
| 174 | const answer = await select({ |
| 175 | message: 'Do you want to save this as the default?', |
| 176 | choices: [ |
| 177 | { name: 'Yes', value: 'yes' }, |
| 178 | { name: 'No', value: 'no' }, |
| 179 | { name: 'No, and do not ask again', value: 'never' }, |
| 180 | ], |
| 181 | }) |
| 182 | |
| 183 | const resetInfo = 'You can reset these settings using the config:reset command.' |
| 184 | if (answer === 'yes') { |
| 185 | await setConfigKey(command.cliConfig, options.defaultValue.configKey, userSelected) |
| 186 | console.log(`${userSelected} is now the default.\n${resetInfo}`) |
| 187 | } else if (answer === 'never') { |
| 188 | await setConfigKey(command.cliConfig, neverAgainKey, true) |
| 189 | console.log(`You will not be asked again.\n${resetInfo}`) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return userSelected |
| 194 | } |
no test coverage detected