(
message: string,
options: NumberInputOptions & { required: boolean },
)
| 158 | } |
| 159 | |
| 160 | export const internalNumberInput = async ( |
| 161 | message: string, |
| 162 | options: NumberInputOptions & { required: boolean }, |
| 163 | ): Promise<number | undefined> => { |
| 164 | // Using `inquirer`'s `number` function instead of `input` would be nice but it doesn't allow |
| 165 | // us to accept `?` for help text. |
| 166 | const prompt = async (): Promise<string> => await input({ |
| 167 | ...options, |
| 168 | message: options.helpText ? `${message} (? for help)` : message, |
| 169 | transformer: displayNoneForEmpty, |
| 170 | validate: input => { |
| 171 | if (options.helpText && input === '?') { |
| 172 | return true |
| 173 | } |
| 174 | if (input === '') { |
| 175 | return true |
| 176 | } |
| 177 | const asNumber = Number(input) |
| 178 | if (isNaN(asNumber)) { |
| 179 | return `"${input}" is not a valid number` |
| 180 | } |
| 181 | return options.validate ? options.validate(asNumber) : true |
| 182 | }, |
| 183 | default: (typeof options.default === 'function' ? options.default() : options.default)?.toString(), |
| 184 | required: options.required, |
| 185 | }) |
| 186 | |
| 187 | let entered = await prompt() |
| 188 | while (options.helpText && entered === '?') { |
| 189 | console.log(options.helpText) |
| 190 | entered = await prompt() |
| 191 | } |
| 192 | |
| 193 | return entered ? Number(entered) : undefined |
| 194 | } |
| 195 | |
| 196 | export const optionalNumberInput = async ( |
| 197 | message: string, |
no test coverage detected