Inquirer.js: ask multiple questions if one prompt is true

2k views Asked by At

I'm writing an account creator using Inquirer.js, and wanted to ask for the user's e-mail and password only if the user asked for such protection. It goes like this:

inquirer.prompt([
        {type:'input', name:'username', message:'Choose an username:'}, 
        {type:'confirm', name:'protect_ask', message:'Do you want to password-protect your game list?'},
        {type:'input', name:'email', message:'E-mail (for password recovering):', when:function(answers){return answers.protect_ask}},
        {type:'password', name:'password', message:'Password:', when:function(answers){return answers.protect_ask}},
        {type:'password', name:'confirm_password', message:'Confirm you password:', when:function(answers){return answers.protect_ask}}
    ]

As you can see, I'm running the exact same when statement three times, for it verifies if the password-protection was chosen or not. I want to know if there's a more intelligent, pragmatic way to do so, this is, to run this verification without repeating the when function.

Maybe nesting the three questions would do it, but how to do so without breaking the prompt flow?

0

There are 0 answers