Created
June 15, 2017 09:52
-
-
Save RobinRadic/fd744177d7a1e4dcff2b594e189ed191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class InputHelper { | |
@lazyInject('cli.config') | |
protected config: Config | |
async ask(question: string, def?: string): Promise<string> { | |
return <Promise<string>> new Promise((resolve, reject) => { | |
inquirer.prompt({ name: 'ask', default: def, type: 'input', message: question }) | |
.then(answers => resolve(answers.ask)) | |
// .catch(err => reject(err)); | |
}); | |
} | |
async checkbox(msg, choices: Array<ChoiceOption>, validate?: (answer) => boolean) { | |
if ( kindOf(choices) === 'array' ) { | |
choices.map(name => { | |
return { | |
name : name, | |
value: name, | |
type : 'string' | |
} | |
}) | |
} | |
let prompt = { | |
type : 'checkbox', | |
message: msg, | |
name : 'toppings', | |
choices: choices, | |
} | |
if ( validate ) { | |
prompt[ 'validate' ] = validate; | |
} | |
return <Promise<Answers>> new Promise((resolve, reject) => { | |
return inquirer.prompt([ prompt ]).then(function (answers) { | |
return resolve(<Answers> answers['toppings']); | |
}).catch(e => reject(e)) | |
}); | |
} | |
async list(msg, choices: Array<ChoiceOption>, validate?: (answer) => boolean) { | |
return <Promise<string>> new Promise((resolve, reject) => { | |
let prompt = { | |
type : 'list', | |
message: msg, | |
name : 'ask', | |
choices: choices, | |
} | |
if ( validate ) { | |
prompt[ 'validate' ] = validate; | |
} | |
inquirer.prompt([ prompt ]).then(function (answers) { | |
resolve(<string> answers.ask); | |
}) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment