Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

Set up reactive subscription before calling prompt

Open kbrandwijk opened this issue 7 years ago • 1 comments

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.

kbrandwijk avatar Jan 09 '18 02:01 kbrandwijk

+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;
  }

atian25 avatar Jul 21 '18 05:07 atian25