-
Notifications
You must be signed in to change notification settings - Fork 3
/
input.js
746 lines (547 loc) · 17.8 KB
/
input.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/**
* Input
* Provides UI prompts and utility functions
*
* @author Mohammad Fares <[email protected]>
*/
/**
* Check if a field is supported as an input
*
* @param {Object} fieldEditMeta {name,operations,schema:{type,items,custom},allowedValues,autoCompleteUrl}
* - Optioanl keys are: allowedValues, autoCompleteUrl, schema.items, schema.custom.
* @return {Boolean}
*/
function isSupportedField(fieldEditMeta) {
return di.is.not.null(generateInputSchema(fieldEditMeta));
}
/**
* Recognize the titlesKeys for an item
*
* - Return the first avaliable key that is one of
* title, name, label, value, key, id, otherwise null
*
* @param {Object} item
* @return {String|Null}
*/
function recognizeTitleKey(item) {
var candidateKeys = Object.keys(di.lodash.pick(item, ['title', 'name', 'label', 'value', 'key', 'id']));
// No candidate for the summary key
if (!candidateKeys.length) {
return null;
}
return candidateKeys[0];
}
/**
* Recognize the summaryKeys for an item
*
* - Return the first avaliable key that is one of
* id, key, otherwise null
*
* @param {Object} item
* @return {String|Null}
*/
function recognizeSummaryKey(item) {
var candidateKeys = Object.keys(di.lodash.pick(item, ['id', 'key']));
// No candidate for the summary key
if (!candidateKeys.length) {
return null;
}
return candidateKeys[0];
}
/**
* Analyse a filed and generate an input schema
* or return null if we can't handle the field
*
* - Returns null if the passed fieldEditMeta
* is not an object.
*
* @param {Object} fieldEditMeta {name,operations,schema:{type,items,custom},allowedValues,autoCompleteUrl}
* - Optioanl keys are: allowedValues, autoCompleteUrl, schema.items, schema.custom.
* @return {Object|Null} {name,type,multiple,allowedValues,autoCompleteUrl}
*/
function generateInputSchema(fieldEditMeta) {
// Not an object
if (di.is.not.json(fieldEditMeta)) {
return null;
}
// The types that we can handle
var supportedTypes = ['number', 'string', 'datetime', 'date', 'user',
'priority', 'version', 'issuetype', 'resolution'];
// To store the result
var inputSchema = {
name: fieldEditMeta.name,
type: null,
multiple: false,
allowedValues: null,
autoCompleteUrl: null
};
// No operations can be performed on this field
if (di.is.not.propertyDefined(fieldEditMeta, 'operations') || di.is.empty(fieldEditMeta.operations)) {
return null;
}
// Is an array field
inputSchema.multiple = fieldEditMeta.schema.type == 'array';
// The type of the field for single value fields
// or the type of the items for multiple value fields
inputSchema.type = inputSchema.multiple ? fieldEditMeta.schema.items : fieldEditMeta.schema.type;
// Not a supported type
if (di.is.not.inArray(inputSchema.type, supportedTypes)) {
return null;
}
// Multiple value field with no provided allowedValues and autoCompleteUrl
if (inputSchema.multiple && di.is.not.propertyDefined(fieldEditMeta, 'allowedValues') &&
di.is.not.propertyDefined(fieldEditMeta, 'autoCompleteUrl')) {
return null;
}
// Not supported custom value field
if (di.is.propertyDefined(fieldEditMeta.schema, 'custom') &&
!fieldEditMeta.schema.custom.match(/^com\.atlassian\.jira/)) {
return null;
}
// An allowedValues is provided
if (di.is.propertyDefined(fieldEditMeta, 'allowedValues')) {
inputSchema.allowedValues = fieldEditMeta.allowedValues;
}
// An autoCompleteUrl is provided
if (di.is.propertyDefined(fieldEditMeta, 'autoCompleteUrl')) {
inputSchema.autoCompleteUrl = fieldEditMeta.autoCompleteUrl;
}
return inputSchema;
}
/**
* Generate a title by picking keys from an item
*
* - Pick the values from the item object of the specified keys
* - Remove emprty and non-existy values
* - Join by -
*
* @param {Array} titleKeys an array of keys to pick from the item
* @param {Object} item optional
* @return {Function|String} return a partial function that takes an item
* if the item is not passed, otherwise
* return the title
*/
function titleFormat(titleKeys, item) {
var parialFunction = function(item) {
var titleKeysValues = di.lodash.values(di.lodash.pick(item, titleKeys));
// Remove emprty and non-existy values
titleKeysValues = di.lodash.filter(titleKeysValues, di.lodash.overEvery([di.is.not.empty, di.is.existy]));
// Join by -
titleKeysValues = titleKeysValues.join(' - ');
return titleKeysValues;
};
if (typeof item === 'undefined') {
return parialFunction;
}
return parialFunction(item);
}
/**
* Show a searchable list to the user to select single or multiple items
*
* @param {Array|Function|Promise} items array of (objects|strings) or a function(input) that returns a promise
* @param {String} message a hint message for the user about the list
* @param {Array|Function} titleKeys an array of keys to be concatendated by ' - ' as title (default: [])
* @param {Array|Function} summaryKeys an array of keys to be concatendated by ' - ' as summary (default: [])
* @param {Boolean} multiple multiple items can be selected if true (default: false)
* @param {Mixed} defaultValue an initially selected item (default: null)
* @param {Boolean} allowEmpty allow choosing no elements for (default: true)
* @return {Promise} resolve with the selected project
*/
function selectItem(items, message, titleKeys, summaryKeys, multiple, defaultValue, allowEmpty) {
// Inquirer prompt options
var options = {
name: 'item',
message: message,
pageSize: 10
};
// A function to get the items as promise
var getItems = null;
// A function to get the title keys
var getTitleKeys = null;
// A function to get the title keys
var getSummaryKeys = null;
// Default value for titleKeys
if (typeof titleKeys === 'undefined') {
titleKeys = [];
}
// Default value for summaryKeys
if (typeof summaryKeys === 'undefined') {
summaryKeys = [];
}
// Default value for multiple
if (typeof multiple === 'undefined') {
multiple = false;
}
// Default value for defaultValue
if (typeof defaultValue === 'undefined') {
defaultValue = null;
}
// Default value for allowEmpty
if (typeof allowEmpty === 'undefined') {
allowEmpty = true;
}
// The items passed as a function
if (di.is.function(items)) {
getItems = items;
} else {
getItems = function() {
return Promise.resolve(items);
};
}
// The titleKeys passed as a function
if (di.is.function(titleKeys)) {
getTitleKeys = titleKeys;
} else {
getTitleKeys = function() {
return titleKeys;
};
}
// The summaryKeys passed as a function
if (di.is.function(summaryKeys)) {
getSummaryKeys = summaryKeys;
} else {
getSummaryKeys = function() {
return summaryKeys;
};
}
// Multiple items value
if (multiple) {
defaultValue = JSON.parse(JSON.stringify(defaultValue));
options.type = 'checkbox';
options.searchable = true;
options.highlight = true;
options.default = defaultValue;
// Single item value
} else {
options.type = 'autocomplete';
}
options.source = function(answersSoFar, input) {
input = input || '';
return getItems(input).then(function(items) {
var titleKeys = getTitleKeys(items);
var summaryKeys = getSummaryKeys(items);
// Single value element with defaultValue that is not applied yet
if (!multiple && defaultValue) {
// Put the defaultValue at the top of the list
items = items.sort(function(a, b) {
if (di.lodash.isEqual(a, defaultValue)) {
return -1;
}
if (di.lodash.isEqual(b, defaultValue)) {
return 1;
}
return 0;
});
// Reset
defaultValue = null;
}
var fuzzyResult = di.fuzzy.filter(input, items, {
extract: titleFormat(titleKeys)
});
var data = fuzzyResult.map(function(item) {
var itemTitle = titleFormat(titleKeys, item.original);
// Summary key is specified
if (di.is.not.empty(summaryKeys)) {
itemTitle += ' ' + di.chalk.dim(titleFormat(summaryKeys, item.original));
}
return {
name: itemTitle,
value: item.original
};
});
return data;
});
};
return di.inquirer.prompt([options]).then(function(answers) {
return answers.item;
});
}
/**
* Show a datepicker to the user to choose a date
*
* @param {String} message a hint message for the user
* @param {String} defaultValue a date in the format YYYY-MM-DD (default: today's date)
* @return {Promise} resolve with a date in the format YYYY-MM-DD
*/
function selectDate(message, defaultValue) {
// Default value for defaultValue
if (typeof defaultValue === 'undefined') {
defaultValue = di.moment().format('YYYY-MM-DD');
}
return di.inquirer.prompt([{
type: 'datetime',
name: 'date',
message: message,
format: ['yyyy', '-', 'mm', '-', 'dd'],
initial: di.moment(defaultValue, 'YYYY-MM-DD').toDate()
}]).then(function(answers) {
return di.moment(answers.date).format('YYYY-MM-DD');
});
}
/**
* Prompt the user to enter a string
*
* @param {String} key a hint key for the user
* @param {String} defaultValue a default string (default: '')
* @return {Promise} resolve with a string
*/
function enterString(key, defaultValue) {
// Default value for defaultValue
if (typeof defaultValue === 'undefined') {
defaultValue = '';
}
return di.inquirer.prompt([{
type: 'input',
name: 'input',
message: di.changeCase.titleCase(key),
default: defaultValue
}]).then(function(answers) {
return answers.input;
});
}
/**
* Prompt the user to enter a primitive value
*
* - the date and datetime returned as string
*
* @param {String} key a hint key for the user
* @param {String} type string, number, date, or datetime
* @param {String|Number} defaultValue a default value (default: '')
* @return {Promise} resolve with a string or number
*/
function enterPrimitiveValue(key, type, defaultValue) {
var message = di.changeCase.titleCase(key);
var options = {
type: 'input',
name: 'input',
message: message
};
if (typeof defaultValue !== 'undefined') {
options.default = defaultValue;
}
// Validate mumbers
if (type == 'number') {
// Add a validation option
options.validate = function(value) {
if (di.is.not.number(parseFloat(value))) {
return 'The value must be a valid number';
}
return true;
};
// To convert the value into a number
options.filter = parseFloat;
}
// For date and datetime types
if (type == 'date' || type == 'datetime') {
return selectDate(message, defaultValue);
}
// For string and number types
return di.inquirer.prompt([options]).then(function(answers) {
return answers.input;
});
}
/**
* Prompt the user to enter a list of strings
*
* @param {Array} keys array of keys (like: name, description)
* @param {Array} defaultValues array of values (same order as keys)
* @return {Promise} resolve with an object {key: value, ..}
*/
function enterStrings(keys, defaultValues) {
// Default value for defaultValue
if (typeof defaultValues === 'undefined') {
defaultValues = [];
}
var prompts = [];
// Foreach key
keys.forEach(function(key, index) {
prompts.push({
type: 'input',
name: key,
message: di.changeCase.titleCase(key),
default: defaultValues.length > index ? defaultValues[index] : null
});
});
return di.inquirer.prompt(prompts).then(function(answers) {
return answers;
});
}
/**
* Prompt the user to enter a Jira field
*
* @param {Object} fieldEditMeta {name,operations,schema:{type,items,custom},allowedValues,autoCompleteUrl}
* - Optioanl keys are: allowedValues, autoCompleteUrl, schema.items, schema.custom.
* @param {Array|String|Number|Object} defaultValue a default value (default: undefined)
* @return {Promise}
*/
function enterField(fieldEditMeta, defaultValue) {
var inputSchema = generateInputSchema(fieldEditMeta);
// Promise for the value
var value = Promise.resolve(null);
// An allowedValues, an autoCompleteUrl is provided
if (inputSchema.allowedValues || inputSchema.autoCompleteUrl) {
// Dynamically recognize the title keys
var titleKeys = function(items) {
// No items
if (!items.length) {
return [];
}
return [recognizeTitleKey(items[0])];
};
// A list of items or a function that returns a promise provides items
var source = null;
// An allowedValues is provided
if (inputSchema.allowedValues) {
// No allowedValues items
if (di.is.empty(inputSchema.allowedValues)) {
return Promise.reject('No available values');
}
source = inputSchema.allowedValues;
}
// An autoCompleteUrl is provided
if (inputSchema.autoCompleteUrl) {
source = function(input) {
var parsedUrl = di.url.parse(inputSchema.autoCompleteUrl + input, true);
return di.api.get(parsedUrl.pathname, parsedUrl.query, false).then(function(result) {
var items = [];
// The response is an array
if (di.is.array(result)) {
items = result;
}
// The response is an object
if (di.is.json(result)) {
// Find the first array
for (var key in result) {
if (di.is.array(result[key])) {
items = result[key];
break;
}
}
}
// No search query is entered yet
if (!input && defaultValue) {
// The default value is an array of values
if (di.is.array(defaultValue)) {
items = items.concat(defaultValue);
} else {
items.push(defaultValue);
}
// Reset the default value
defaultValue = null;
}
return items;
});
};
}
// Is a multiple values field
if (inputSchema.multiple) {
value = di.input.selectItem(source, inputSchema.name, titleKeys, [], true, defaultValue);
// Is a single value field
} else {
value = di.input.selectItem(source, inputSchema.name, titleKeys, [], false, defaultValue);
}
// Format the value (when the type is a primitive and the selected values are objects)
value = value.then(function(value) {
var primitiveTypes = ['number', 'string', 'datetime', 'date'];
// Not a primitive type
if (di.is.not.inArray(inputSchema.type, primitiveTypes)) {
return value;
}
/**
* Get the first key's value casted to the primitive type
*
* @param {Object} item
* @return {String|Number}
*/
var format = function(item) {
// The item is not an object
if (di.is.not.json(item)) {
return item;
}
// The value of the first key
var firstKeyValue = di.lodash.first(di.lodash.toArray(item));
// The type is number
if (inputSchema.type == 'number') {
return parseFloat(firstKeyValue);
}
return firstKeyValue;
};
// Is a multiple values field
if (inputSchema.multiple) {
value = di.lodash.map(value, format);
} else {
value = format(value);
}
return value;
});
} else {
value = di.input.enterPrimitiveValue(inputSchema.name, inputSchema.type, defaultValue);
}
return value;
}
/**
* Prompt the user to confirm something
*
* @param {String} message a hint message for the user about the confirmation
* @param {Boolean} defaultValue a default value (default: true)
* @return {Promise}
*/
function confirm(message, defaultValue) {
// Default value for defaultValue
if (typeof defaultValues === 'undefined') {
defaultValues = true;
}
return di.inquirer.prompt([{
type: 'confirm',
name: 'confirm',
message: message,
default: defaultValue
}]).then(function(answers) {
return answers.confirm;
});
}
/**
* Launches the user's preferred editor to edit a text
*
* @param {String} message a hint message for the user about the content
* @param {String} content a content to be edited (default: '')
* @return {Promise}
*/
function edit(message, content) {
// Default value for content
if (typeof content === 'undefined') {
content = '';
}
return di.inquirer.prompt([{
type: 'editor',
name: 'content',
message: message,
default: content
}]).then(function(answers) {
return answers.content;
});
}
/**
* Load plugins
*/
function load() {
di.inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
di.inquirer.registerPrompt('checkbox', require('inquirer-checkbox-plus-prompt'));
di.inquirer.registerPrompt('datetime', require('inquirer-datepicker-prompt'));
}
////////////////////////////////////////////////////
// Module //////////////////////////////////////////
////////////////////////////////////////////////////
module.exports = {
isSupportedField: isSupportedField,
generateInputSchema: generateInputSchema,
selectItem: selectItem,
selectDate: selectDate,
enterPrimitiveValue: enterPrimitiveValue,
enterString: enterString,
enterStrings: enterStrings,
enterField: enterField,
confirm: confirm,
edit: edit,
load: load
};