({
label,
defaultValue,
customValidate,
secret,
required,
}: {
label: string
defaultValue?: string
customValidate?: StringFieldCustomValidateFn
secret: boolean
required: boolean
})
| 19 | } |
| 20 | |
| 21 | export async function promptForStringField({ |
| 22 | label, |
| 23 | defaultValue, |
| 24 | customValidate, |
| 25 | secret, |
| 26 | required, |
| 27 | }: { |
| 28 | label: string |
| 29 | defaultValue?: string |
| 30 | customValidate?: StringFieldCustomValidateFn |
| 31 | secret: boolean |
| 32 | required: boolean |
| 33 | }): Promise<string> { |
| 34 | if (secret) { |
| 35 | // The password prompt does not support `default` values (input is masked), |
| 36 | // so we allow empty submission when a defaultValue exists and fall back to it. |
| 37 | const hasDefault = defaultValue != null |
| 38 | const result = await password({ |
| 39 | message: label, |
| 40 | validate: (value: string) => { |
| 41 | if (required && !value.trim() && !hasDefault) { |
| 42 | return `${label} is required` |
| 43 | } |
| 44 | if (value.trim() && customValidate) { |
| 45 | const customValidateResult = customValidate(value) |
| 46 | if (typeof customValidateResult === 'string') { |
| 47 | return customValidateResult |
| 48 | } |
| 49 | } |
| 50 | return true |
| 51 | }, |
| 52 | }) |
| 53 | if (hasDefault && !result.trim()) { |
| 54 | return defaultValue |
| 55 | } |
| 56 | return result |
| 57 | } |
| 58 | |
| 59 | return input({ |
| 60 | message: label, |
| 61 | default: defaultValue, |
| 62 | required: required === true, |
| 63 | validate: (value: string) => { |
| 64 | if (required && !value.trim()) { |
| 65 | return `${label} is required` |
| 66 | } |
| 67 | if (customValidate) { |
| 68 | const customValidateResult = customValidate(value) |
| 69 | if (typeof customValidateResult === 'string') { |
| 70 | return customValidateResult |
| 71 | } |
| 72 | } |
| 73 | return true |
| 74 | }, |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | export async function promptForRequiredStringField({ |
no outgoing calls
no test coverage detected