UNPKG

1.06 kBJavaScriptView Raw
1const inquirer = require('inquirer');
2const noop = Promise.resolve();
3
4class Prompt {
5 constructor({ container }) {
6 this.createPrompt = (container.inquirer || inquirer).prompt;
7 this.prompts = {};
8 }
9
10 register(pluginPrompts, namespace = 'default') {
11 this.prompts[namespace] = this.prompts[namespace] || {};
12 Object.assign(this.prompts[namespace], pluginPrompts);
13 }
14
15 async show({ enabled = true, prompt: promptName, namespace = 'default', task, context }) {
16 if (!enabled) return noop;
17
18 const prompt = this.prompts[namespace][promptName];
19 const options = Object.assign({}, prompt, {
20 name: promptName,
21 message: prompt.message(context),
22 choices: 'choices' in prompt && prompt.choices(context),
23 transformer: 'transformer' in prompt && prompt.transformer(context)
24 });
25
26 const answers = await this.createPrompt([options]);
27
28 const doExecute = prompt.type === 'confirm' ? answers[promptName] : true;
29
30 return doExecute && task ? await task(answers[promptName]) : noop;
31 }
32}
33
34module.exports = Prompt;