1 | 'use strict';
|
2 |
|
3 | const assert = require('assert');
|
4 | const Events = require('events');
|
5 | const utils = require('./lib/utils');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class Enquirer extends Events {
|
21 | constructor(options, answers) {
|
22 | super();
|
23 | this.options = utils.merge({}, options);
|
24 | this.answers = { ...answers };
|
25 | }
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | register(type, fn) {
|
43 | if (utils.isObject(type)) {
|
44 | for (let key of Object.keys(type)) this.register(key, type[key]);
|
45 | return this;
|
46 | }
|
47 | assert.equal(typeof fn, 'function', 'expected a function');
|
48 | let name = type.toLowerCase();
|
49 | if (fn.prototype instanceof this.Prompt) {
|
50 | this.prompts[name] = fn;
|
51 | } else {
|
52 | this.prompts[name] = fn(this.Prompt, this);
|
53 | }
|
54 | return this;
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | async prompt(questions = []) {
|
79 | for (let question of [].concat(questions)) {
|
80 | try {
|
81 | if (typeof question === 'function') question = await question.call(this);
|
82 | await this.ask(utils.merge({}, this.options, question));
|
83 | } catch (err) {
|
84 | return Promise.reject(err);
|
85 | }
|
86 | }
|
87 | return this.answers;
|
88 | }
|
89 |
|
90 | async ask(question) {
|
91 | if (typeof question === 'function') {
|
92 | question = await question.call(this);
|
93 | }
|
94 |
|
95 | let opts = utils.merge({}, this.options, question);
|
96 | let { type, name } = question;
|
97 | let { set, get } = utils;
|
98 |
|
99 | if (typeof type === 'function') {
|
100 | type = await type.call(this, question, this.answers);
|
101 | }
|
102 |
|
103 | if (!type) return this.answers[name];
|
104 |
|
105 | assert(this.prompts[type], `Prompt "${type}" is not registered`);
|
106 |
|
107 | let prompt = new this.prompts[type](opts);
|
108 | let value = get(this.answers, name);
|
109 |
|
110 | prompt.state.answers = this.answers;
|
111 | prompt.enquirer = this;
|
112 |
|
113 | if (name) {
|
114 | prompt.on('submit', value => {
|
115 | this.emit('answer', name, value, prompt);
|
116 | set(this.answers, name, value);
|
117 | });
|
118 | }
|
119 |
|
120 | // bubble events
|
121 | let emit = prompt.emit.bind(prompt);
|
122 | prompt.emit = (...args) => {
|
123 | this.emit.call(this, ...args);
|
124 | return emit(...args);
|
125 | };
|
126 |
|
127 | this.emit('prompt', prompt, this);
|
128 |
|
129 | if (opts.autofill && value != null) {
|
130 | prompt.value = prompt.input = value;
|
131 |
|
132 |
|
133 | if (opts.autofill === 'show') {
|
134 | await prompt.submit();
|
135 | }
|
136 | } else {
|
137 | value = prompt.value = await prompt.run();
|
138 | }
|
139 |
|
140 | return value;
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | use(plugin) {
|
161 | plugin.call(this, this);
|
162 | return this;
|
163 | }
|
164 |
|
165 | set Prompt(value) {
|
166 | this._Prompt = value;
|
167 | }
|
168 | get Prompt() {
|
169 | return this._Prompt || this.constructor.Prompt;
|
170 | }
|
171 |
|
172 | get prompts() {
|
173 | return this.constructor.prompts;
|
174 | }
|
175 |
|
176 | static set Prompt(value) {
|
177 | this._Prompt = value;
|
178 | }
|
179 | static get Prompt() {
|
180 | return this._Prompt || require('./lib/prompt');
|
181 | }
|
182 |
|
183 | static get prompts() {
|
184 | return require('./lib/prompts');
|
185 | }
|
186 |
|
187 | static get types() {
|
188 | return require('./lib/types');
|
189 | }
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | static get prompt() {
|
211 | const fn = (questions, ...rest) => {
|
212 | let enquirer = new this(...rest);
|
213 | let emit = enquirer.emit.bind(enquirer);
|
214 | enquirer.emit = (...args) => {
|
215 | fn.emit(...args);
|
216 | return emit(...args);
|
217 | };
|
218 | return enquirer.prompt(questions);
|
219 | };
|
220 | utils.mixinEmitter(fn, new Events());
|
221 | return fn;
|
222 | }
|
223 | }
|
224 |
|
225 | utils.mixinEmitter(Enquirer, new Events());
|
226 | const prompts = Enquirer.prompts;
|
227 |
|
228 | for (let name of Object.keys(prompts)) {
|
229 | let key = name.toLowerCase();
|
230 |
|
231 | let run = options => new prompts[name](options).run();
|
232 | Enquirer.prompt[key] = run;
|
233 | Enquirer[key] = run;
|
234 |
|
235 | if (!Enquirer[name]) {
|
236 | Reflect.defineProperty(Enquirer, name, { get: () => prompts[name] });
|
237 | }
|
238 | }
|
239 |
|
240 | const exp = name => {
|
241 | utils.defineExport(Enquirer, name, () => Enquirer.types[name]);
|
242 | };
|
243 |
|
244 | exp('ArrayPrompt');
|
245 | exp('AuthPrompt');
|
246 | exp('BooleanPrompt');
|
247 | exp('NumberPrompt');
|
248 | exp('StringPrompt');
|
249 |
|
250 | module.exports = Enquirer;
|