Skip to content

Instantly share code, notes, and snippets.

@RobinRadic
Created June 15, 2017 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinRadic/fd744177d7a1e4dcff2b594e189ed191 to your computer and use it in GitHub Desktop.
Save RobinRadic/fd744177d7a1e4dcff2b594e189ed191 to your computer and use it in GitHub Desktop.
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