0

I’m working on an application that sets a series of questions to generate an employee profile. As it is intended to be recursive, the user will be asked at the beginning and the end of the prompt to exit and terminate the enquire.

const employeeQuestions = [
    // Role of employee
    {
        type: 'rawlist',
        name: 'role',
        message: 'What is the role of the employee?',
        choices: [
            'Engineer',
            'Intern',
            new inquirer.Separator(),
            'Finish building the team', <=== THIS IS THE ANSWER THAT SHOULD TERMINATE THE PROMPT
        ],
        default: 3,
    },

    // Employee questions
    {
        type: 'input',
        name: `employee_name`,
        message: answer => `Enter name of the ${answer.role}`,
    },
    {
        type: 'number',
        name: `employee_id`,
        message: answer => `Enter ${answer.role} ID`,
        validate(answer) {
            const valid = !isNaN(parseFloat(answer));
            return valid || 'Please enter a number';
        },
        filter: Number,
    },
    {
        type: 'input',
        name: `employee_email`,
        message: answer => `Enter ${answer.role} Email address`,
    },

    // Engineer questions
    {
        when(answer) {
            return answer.role === 'Engineer';
        },

        type: 'input',
        name: `engineer_github`,
        message: 'Enter GitHub username',
    },

    // Intern questions
    {
        when(answer) {
            return answer.role === 'Intern';
        },

        type: 'input',
        name: `intern_school`,
        message: 'Enter intern school',
    },

    // add more employees
    {
        type: 'confirm',
        name: `add_more`,
        message: 'Do you want to add another employee?', <=== THIS IS THE QUESTION THAT SHOULD TERMINATE THE PROMPT
        default: true,
    },
];

// # Functions
// * Inquires all over if add_more = true
function inquireAgain() {
    inquirer.prompt(employeeQuestions).then(answers => {
        employeesInfo.push(answers);

        if (answers.add_more) {
            inquireAgain();
        } else {
            console.log(JSON.stringify(employeesInfo, null, ' '));
        }
    });
}

// * Initialize the inquirer prompts
async function init() {
    const inquireManager = await inquirer.prompt(managerQuestions);
    employeesInfo.push(inquireManager);

    inquireAgain();
}

// # Initialisation
init();

this is the closest thing I found related to terminate the prompt, but is not working for me, Thanks in advance.

Sagos
  • 1
  • 2

1 Answers1

0
inquirer.prompt({
    type: "list",
    name: "what",
    message: "What would you like to do?",
    choices: ['Exit']
})
.then((answer) => {
    if (answer.what === 'Exit') {
        process.exit();
    }
});

Just use process.exit(); to exit the inquirer prompt.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31