Inquirer.js
Inquirer.js copied to clipboard
Set up reactive subscription before calling prompt
Is there any way to set up a reactive subscription before calling prompt on inquirer?
The reason is that I am working on a modular system, and that I would like to set up a subscription on inquirer before actually calling prompt with a Rx.Subject, because that's created a lot later in the process.
+1 for this.
workaround
_initInquirer() {
const originPrompt = inquirer.createPromptModule();
const prompt = (questions, cb) => {
if (!Array.isArray(questions)) questions = [].concat(questions);
const task = originPrompt(questions, cb);
let sendCount = 0;
// will trigger after each prompt result
task.ui.process.subscribe(() => {
// only auto answer if current questions list is not finish
if (sendCount < questions.length) {
process.send({ type: 'prompt' });
sendCount++;
}
});
// send first key
process.send({ type: 'prompt' });
sendCount++;
return task;
};
return prompt;
}