I am building a command line interface in node.js using library: inquirer.
based on my need I want to render prompt, confirmation text etc when user input's. example.
var _questions = [{
'type': 'list',
'name': 'databasetype',
'message': 'Choose database :',
'choices': ['mongoDB', 'mysql [alpha]', 'firebase [alpha]', 'url [alpha]'],
'default': 'mongoDB'
}, {
'type': 'input',
'name': 'xfactor',
'message': 'X Factor [email, username etc..] :'
}]
// show question's.
Inquirer.prompt(_questions).then(async (__answers) => {
console.log(__answers)
})
if user chooses mongoDB than it should render another prompt asking mongodb url
You can use the when
question property, its value should be a function that returns a boolean; true
for show question, false
for don't show question
so using your example:
_questions = [{
type: 'list',
name: 'databasetype',
message: 'Choose database :',
choices: ['mongoDB', 'mysql [alpha]', 'firebase [alpha]', 'url [alpha]'],
default: 'mongoDB'
}, {
type: 'input',
name: 'url',
message: 'Enter the URL',
when: (answers) => answers.databasetype === 'mongoDB'
}]
see more examples here when usage examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With