{%= include("install-npm", {save: true}) %}
const List = require('{%= name %}');
const list = new List({
name: 'order',
message: 'What would you like to order?',
// choices may be defined as an array or a function that returns an array
choices: [
'Coke',
'Diet Coke',
'Cherry Coke',
{name: 'Sprite', disabled: 'Temporarily unavailable'},
'Water'
]
});
// async
list.ask(function(answer) {
console.log(answer);
});
// promise
list.run()
.then(function(answer) {
console.log(answer);
});
Register the prompt as an [enquirer][] plugin:
const Enquirer = require('enquirer');
const enquirer = new Enquirer();
enquirer.register('list', require('{%= name %}'));
Enquirer supports both declarative, inquirer-style questions, and an expressive format, using the .question
method:
Declarative
const questions = [
{
type: 'list',
name: 'order',
message: 'What would you like to order?',
choices: [
'Coke',
'Diet Coke',
'Cherry Coke',
{name: 'Sprite', disabled: 'Temporarily unavailable'},
'Water'
]
}
];
enquirer.ask(questions)
.then(function(answers) {
console.log(answers);
})
.catch(function(err) {
console.log(err);
});
Expressive
enquirer.question('order', 'What would you like to order?', {
type: 'list',
choices: [
'Coke',
'Diet Coke',
'Cherry Coke',
{name: 'Sprite', disabled: 'Temporarily unavailable'},
'Water'
]
});
enquirer.ask(['order'])
.then(function(answers) {
console.log(answers);
})
.catch(function(err) {
console.log(err);
});