import inquirer , { Question as IQuestion } from 'inquirer' ; export type YNAnswer = 'Y' | 'N' ; export const positiveAnswers = [ 'y' , 'yes' ] ; export const negativeAnswers = [ 'n' , 'no' ] ; export const acceptableAnswers = positiveAnswers . concat ( negativeAnswers ) ; export interface Question { name : string ; message : string ; type ? : 'list' | 'input' | 'password' ; validate ? ( input ) : boolean | string | Promise < boolean | string > ; } param defaultAnswer export const _validateConfirmation = ( defaultAnswer ? ) => ( input ) : string | boolean => { if ( ! input && defaultAnswer ) { input = defaultAnswer ; } if ( acceptableAnswers . includes ( input . toLowerCase ( ) ) ) { return true ; } return 'Please answer with either "y" or "n"' ; } ; param question export const prompt = async ( question ) : Promise < Question [ 'name' ] > => { question . type = question . type || 'input' ; const result = await inquirer . prompt ( [ question as IQuestion ] ) ; const nameKey = Object . keys ( question ) . find ( ( k ) => k === 'name' ) as string ; return result [ question [ nameKey ] ] ; } ; param question param defaultAnswer export const confirm = async ( question , defaultAnswer ? ) : Promise < boolean > => { let suffix = '(y/n)' ; if ( defaultAnswer === 'Y' ) { suffix = '(Y/n)' ; } else if ( defaultAnswer === 'N' ) { suffix = '(y/N)' ; } const q = { name : 'answer' , message : `${ question . trim ( ) } ${ suffix }` , validate : _validateConfirmation ( defaultAnswer ) , } ; let answer = await prompt ( q ) ; if ( ! answer && defaultAnswer ) { answer = defaultAnswer ; } if ( positiveAnswers . includes ( answer . toLowerCase ( ) ) ) { return Promise . resolve ( true ) ; } return Promise . resolve ( false ) ; } ; param question param choices export const choose = async ( question , choices ) : Promise < Question [ 'name' ] > => { question . type = 'list' ; return prompt ( Object . assign ( question , { choices } ) ) ; } ; export default inquirer ;