-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
326 lines (291 loc) · 7.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'use strict';
var debug = require('debug')('prompt-question');
var Choices = require('prompt-choices');
var define = require('define-property');
var isObject = require('isobject');
var clone = require('clone-deep');
var koalas = require('koalas');
var utils = require('./lib/utils');
/**
* Create a new question with the given `name`, `message` and `options`.
*
* ```js
* var question = new Question('first', 'What is your first name?');
* console.log(question);
* // {
* // type: 'input',
* // name: 'color',
* // message: 'What is your favorite color?'
* // }
* ```
* @param {String|Object} `name` Question name or options.
* @param {String|Object} `message` Question message or options.
* @param {String|Object} `options` Question options.
* @api public
*/
function Question(name, message, options) {
debug('initializing from <%s>', __filename);
if (arguments.length === 0) {
throw new TypeError('expected a string or object');
}
if (Question.isQuestion(name)) {
return name;
}
this.type = 'input';
this.options = {};
this.getDefault();
if (Array.isArray(message)) {
options = { choices: message };
message = name;
}
if (Array.isArray(options)) {
options = { choices: options };
}
define(this, 'Choices', Choices);
define(this, 'isQuestion', true);
utils.assign(this, {
name: name,
message: message,
options: options
});
}
/**
* Clone the question instance.
*
* ```js
* var clonedQuestion = question.clone();
* ```
* @return {Object} Returns the cloned question
* @api public
*/
Question.prototype.clone = function() {
var keys = Object.keys(this);
var cloned = new this.constructor(clone(this.cache));
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!(key in cloned)) {
cloned[key] = this[key];
}
}
return cloned;
};
/**
* Add formatted choice objects to the `question.choices` array.
* See [prompt-choices][] for more details.
*
* ```js
* question.addChoices(['foo', 'bar', 'baz']);
* ```
* @param {String|Array} `choices` One or more choices to add.
* @return {Object} Returns the question instance for chaining
* @api public
*/
Question.prototype.addChoices = function() {
this.choices.addChoices.apply(this.choices, arguments);
return this;
};
/**
* Add a choice to `question.choices` array.
* See [prompt-choices][] for more details.
*
* ```js
* question.addChoice('foo');
* ```
* @param {String|Object} `choice`
* @return {Object} Returns the question instance for chaining
* @api public
*/
Question.prototype.addChoice = function() {
this.choices.addChoice.apply(this.choices, arguments);
return this;
};
/**
* Returns the given `val` or `question.default` if `val` is undefined or null.
*
* ```js
* var question = new Question({
* name: 'first',
* message: 'First name'?,
* default: 'Bob'
* });
*
* console.log(question.getAnswer());
* //=> 'Bob'
* console.log(question.getAnswer('Joe'));
* //=> 'Joe'
* console.log(question.getAnswer(false));
* //=> false
* console.log(question.getAnswer(0));
* //=> 0
* ```
*
* @param {any} `val`
* @return {any}
* @api public
*/
Question.prototype.getDefault = function(val) {
var def = koalas(this.default, this.options.default, this.choices.default);
if (def == null) {
return def;
}
if (this.choices.length) {
var idx = this.choices.getIndex(def);
if (typeof idx === 'number') {
this.choices.default = idx;
this.choices.check(idx);
def = idx;
}
}
this.default = def;
return def;
};
Question.prototype.getAnswer = function(val) {
if (this._choices && !this.choices.checked.length && this.default != null) {
this.choices.check(utils.decrement(this.default));
}
if (this._choices && this.choices.length) {
return this.choices.checked;
}
return koalas(val, this.default);
};
/**
* Get the given choice from `questions.choices`.
*
* ```js
* var Question = require('prompt-question');
* var question = new Question('color', 'What is your favorite color?', {
* choices: ['red', 'blue', 'yellow']
* });
* console.log(question.getChoice('red'));
* //=> Choice { name: 'red', short: 'red', value: 'red', checked: false }
* ```
*
* @param {any} `val`
* @return {any}
* @api public
*/
Question.prototype.getChoice = function() {
return this.choices.get.apply(this.choices, arguments);
};
/**
* Create a separator using [choices-separator][].
* @api public
*/
Question.prototype.separator = function() {
return this.choices.separator.apply(this.choices, arguments);
};
/**
* Getter that returns true if a `default` value has been defined.
*
* @name .hasDefault
* @return {Boolean} True if a default value is defined.
* @api public
*/
Object.defineProperty(Question.prototype, 'hasDefault', {
get: function() {
return this.default != null;
}
});
/**
* Getter/setter for the checkbox symbols to use.
*
* ```js
* var question = new Question({
* name: 'foo',
* checkbox: {off: '[ ]', on: '[x]', disabled: 'X'}
* });
* // or
* question.checkbox = {off: '[ ]', on: '[x]', disabled: 'X'};
* ```
* @name .checkbox
* @return {Object} Checkbox object with `.on`, `.off` and `.disabled` properties.
* @api public
*/
Object.defineProperty(Question.prototype, 'checkbox', {
set: function(checkbox) {
if (utils.isObject(checkbox)) {
throw new TypeError('expected checkbox symbols to be an object');
}
this.choices.checkbox = checkbox;
},
get: function() {
return this.choices.checkbox;
}
});
/**
* Getter/setter for getting and setting choices (if applicable).
*
* ```js
* var question = new Question();
* question.choices = ['a', 'b', 'c'];
* ```
* @name .choices
* @return {Object} Returns an instance of [prompt-choices]
* @api public
*/
Object.defineProperty(Question.prototype, 'choices', {
configurable: true,
enumerable: true,
set: function(choices) {
define(this, '_choices', choices);
},
get: function() {
if (typeof this._choices === 'function') {
this._choices = this._choices.call(this);
}
if (this._choices == null) {
define(this, '_choices', this.options.choices);
}
if (!(this._choices instanceof Choices)) {
this._choices = new Choices(this._choices, this);
}
return this._choices;
}
});
/**
* Static method that returns true if `question` is a valid question object.
*
* ```js
* console.log(Question.isQuestion('foo'));
* //=> false
* console.log(Question.isQuestion(new Question('What is your name?')));
* //=> true
* ```
* @name Question.isQuestion
* @param {Object} `question`
* @return {Boolean}
* @api public
*/
Question.isQuestion = function(question) {
return utils.isObject(question) && question.isQuestion === true;
};
/**
* Static method for creating a new `Choices` object. See [prompt-choices][]
* for more details.
*
* ```js
* var choices = new Question.Choices(['foo', 'bar', 'baz']);
* ```
* @name Question.choices
* @param {Array} `choices` Array of choices
* @return {Object} Returns an intance of Choices.
* @api public
*/
Question.Choices = Choices;
/**
* Static method for creating a new `Separator` object.
* See [choices-separator][] for more details.
*
* ```js
* new Question.Separator();
* ```
* @name Question.Separator
* @param {String} `separator` Optionally pass a string to use as the separator.
* @return {Object} Returns a separator object.
* @api public
*/
Question.Separator = Choices.Separator;
/**
* Expose `Question`
*/
module.exports = Question;