inquirerer 1.9.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/prompt.js +54 -7
- package/package.json +2 -2
- package/prompt.js +54 -7
- package/question/types.d.ts +10 -2
package/esm/prompt.js
CHANGED
|
@@ -68,7 +68,13 @@ function generatePromptMessage(question, ctx) {
|
|
|
68
68
|
promptMessage += ` [${question.default}]`;
|
|
69
69
|
}
|
|
70
70
|
break;
|
|
71
|
+
case 'number':
|
|
72
|
+
if (question.default) {
|
|
73
|
+
promptMessage += ` [${question.default}]`;
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
71
76
|
case 'autocomplete':
|
|
77
|
+
case 'list':
|
|
72
78
|
case 'checkbox':
|
|
73
79
|
// For these types, you might want to show the default selected options if any
|
|
74
80
|
if (question.options && question.default) {
|
|
@@ -275,6 +281,7 @@ export class Inquirerer {
|
|
|
275
281
|
}
|
|
276
282
|
// Apply default value if applicable
|
|
277
283
|
// this is if useDefault is set, rare! not typical defaults which happen AFTER
|
|
284
|
+
// this is mostly to avoid a prompt for "hidden" options
|
|
278
285
|
if ('default' in question && (this.useDefaults || question.useDefault)) {
|
|
279
286
|
obj[question.name] = question.default;
|
|
280
287
|
continue; // Skip to the next question since the default is applied
|
|
@@ -327,11 +334,13 @@ export class Inquirerer {
|
|
|
327
334
|
handleOverrides(argv, obj, question) {
|
|
328
335
|
switch (question.type) {
|
|
329
336
|
case 'text':
|
|
337
|
+
case 'number':
|
|
330
338
|
case 'confirm':
|
|
331
339
|
// do nothing, already set!
|
|
332
340
|
break;
|
|
333
341
|
case 'checkbox':
|
|
334
342
|
case 'autocomplete':
|
|
343
|
+
case 'list':
|
|
335
344
|
// get the value from options :)
|
|
336
345
|
this.handleOverridesWithOptions(argv, obj, question);
|
|
337
346
|
break;
|
|
@@ -355,11 +364,13 @@ export class Inquirerer {
|
|
|
355
364
|
return this.confirm(question, ctx);
|
|
356
365
|
case 'checkbox':
|
|
357
366
|
return this.checkbox(question, ctx);
|
|
367
|
+
case 'list':
|
|
358
368
|
case 'autocomplete':
|
|
359
369
|
return this.autocomplete(question, ctx);
|
|
360
370
|
case 'number':
|
|
361
371
|
return this.number(question, ctx);
|
|
362
372
|
case 'text':
|
|
373
|
+
return this.text(question, ctx);
|
|
363
374
|
default:
|
|
364
375
|
return this.text(question, ctx);
|
|
365
376
|
}
|
|
@@ -398,6 +409,9 @@ export class Inquirerer {
|
|
|
398
409
|
if (input.trim() !== '') {
|
|
399
410
|
resolve(input); // Return input if not empty
|
|
400
411
|
}
|
|
412
|
+
else if ('default' in question) {
|
|
413
|
+
resolve(question.default); // Use default if input is empty
|
|
414
|
+
}
|
|
401
415
|
else {
|
|
402
416
|
resolve(null); // Return null if empty and not required
|
|
403
417
|
}
|
|
@@ -422,18 +436,41 @@ export class Inquirerer {
|
|
|
422
436
|
resolve(num);
|
|
423
437
|
}
|
|
424
438
|
else {
|
|
425
|
-
resolve(null);
|
|
439
|
+
resolve(null); // Let validation handle bad input
|
|
426
440
|
}
|
|
427
441
|
}
|
|
442
|
+
else if ('default' in question) {
|
|
443
|
+
resolve(question.default); // Use default if input is empty
|
|
444
|
+
}
|
|
428
445
|
else {
|
|
429
|
-
resolve(null);
|
|
446
|
+
resolve(null); // Empty and no default
|
|
430
447
|
}
|
|
431
448
|
});
|
|
432
449
|
});
|
|
433
450
|
}
|
|
434
451
|
async checkbox(question, ctx) {
|
|
435
|
-
if (this.noTty || !this.rl)
|
|
436
|
-
|
|
452
|
+
if (this.noTty || !this.rl) {
|
|
453
|
+
const options = this.sanitizeOptions(question);
|
|
454
|
+
const defaults = Array.isArray(question.default)
|
|
455
|
+
? question.default
|
|
456
|
+
: [question.default];
|
|
457
|
+
// If returnFullResults is true, return all options with boolean selection
|
|
458
|
+
if (question.returnFullResults) {
|
|
459
|
+
return options.map(opt => ({
|
|
460
|
+
name: opt.name,
|
|
461
|
+
value: opt.value,
|
|
462
|
+
selected: defaults.includes(opt.name)
|
|
463
|
+
}));
|
|
464
|
+
}
|
|
465
|
+
// Otherwise, return only selected options
|
|
466
|
+
return options
|
|
467
|
+
.filter(opt => defaults.includes(opt.name) || defaults.includes(opt.value))
|
|
468
|
+
.map(opt => ({
|
|
469
|
+
name: opt.name,
|
|
470
|
+
value: opt.value,
|
|
471
|
+
selected: true
|
|
472
|
+
}));
|
|
473
|
+
}
|
|
437
474
|
if (!question.options.length) {
|
|
438
475
|
// no arguments don't make sense
|
|
439
476
|
throw new Error('checkbox requires options');
|
|
@@ -445,7 +482,15 @@ export class Inquirerer {
|
|
|
445
482
|
let selectedIndex = 0;
|
|
446
483
|
let startIndex = 0; // Start index for visible options
|
|
447
484
|
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
448
|
-
const selections = new Array(options.length).fill(false);
|
|
485
|
+
// const selections: boolean[] = new Array(options.length).fill(false);
|
|
486
|
+
const selections = options.map(opt => {
|
|
487
|
+
if (!question.default)
|
|
488
|
+
return false;
|
|
489
|
+
const defaults = Array.isArray(question.default)
|
|
490
|
+
? question.default
|
|
491
|
+
: [question.default];
|
|
492
|
+
return defaults.includes(opt.name);
|
|
493
|
+
});
|
|
449
494
|
const updateFilteredOptions = () => {
|
|
450
495
|
filteredOptions = this.filterOptions(options, input);
|
|
451
496
|
};
|
|
@@ -517,7 +562,8 @@ export class Inquirerer {
|
|
|
517
562
|
options.forEach((option, index) => {
|
|
518
563
|
result.push({
|
|
519
564
|
name: option.name,
|
|
520
|
-
value:
|
|
565
|
+
value: option.value,
|
|
566
|
+
selected: selections[index]
|
|
521
567
|
});
|
|
522
568
|
});
|
|
523
569
|
}
|
|
@@ -527,7 +573,8 @@ export class Inquirerer {
|
|
|
527
573
|
if (selections[index]) {
|
|
528
574
|
result.push({
|
|
529
575
|
name: option.name,
|
|
530
|
-
value:
|
|
576
|
+
value: option.value,
|
|
577
|
+
selected: selections[index]
|
|
531
578
|
});
|
|
532
579
|
}
|
|
533
580
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "inquirerer",
|
|
6
6
|
"main": "index.js",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"minimist": "^1.2.8"
|
|
36
36
|
},
|
|
37
37
|
"keywords": [],
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "aa6ac282c2a3019fef455f0c45e5cad1bbdeb53d"
|
|
39
39
|
}
|
package/prompt.js
CHANGED
|
@@ -74,7 +74,13 @@ function generatePromptMessage(question, ctx) {
|
|
|
74
74
|
promptMessage += ` [${question.default}]`;
|
|
75
75
|
}
|
|
76
76
|
break;
|
|
77
|
+
case 'number':
|
|
78
|
+
if (question.default) {
|
|
79
|
+
promptMessage += ` [${question.default}]`;
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
77
82
|
case 'autocomplete':
|
|
83
|
+
case 'list':
|
|
78
84
|
case 'checkbox':
|
|
79
85
|
// For these types, you might want to show the default selected options if any
|
|
80
86
|
if (question.options && question.default) {
|
|
@@ -281,6 +287,7 @@ class Inquirerer {
|
|
|
281
287
|
}
|
|
282
288
|
// Apply default value if applicable
|
|
283
289
|
// this is if useDefault is set, rare! not typical defaults which happen AFTER
|
|
290
|
+
// this is mostly to avoid a prompt for "hidden" options
|
|
284
291
|
if ('default' in question && (this.useDefaults || question.useDefault)) {
|
|
285
292
|
obj[question.name] = question.default;
|
|
286
293
|
continue; // Skip to the next question since the default is applied
|
|
@@ -333,11 +340,13 @@ class Inquirerer {
|
|
|
333
340
|
handleOverrides(argv, obj, question) {
|
|
334
341
|
switch (question.type) {
|
|
335
342
|
case 'text':
|
|
343
|
+
case 'number':
|
|
336
344
|
case 'confirm':
|
|
337
345
|
// do nothing, already set!
|
|
338
346
|
break;
|
|
339
347
|
case 'checkbox':
|
|
340
348
|
case 'autocomplete':
|
|
349
|
+
case 'list':
|
|
341
350
|
// get the value from options :)
|
|
342
351
|
this.handleOverridesWithOptions(argv, obj, question);
|
|
343
352
|
break;
|
|
@@ -361,11 +370,13 @@ class Inquirerer {
|
|
|
361
370
|
return this.confirm(question, ctx);
|
|
362
371
|
case 'checkbox':
|
|
363
372
|
return this.checkbox(question, ctx);
|
|
373
|
+
case 'list':
|
|
364
374
|
case 'autocomplete':
|
|
365
375
|
return this.autocomplete(question, ctx);
|
|
366
376
|
case 'number':
|
|
367
377
|
return this.number(question, ctx);
|
|
368
378
|
case 'text':
|
|
379
|
+
return this.text(question, ctx);
|
|
369
380
|
default:
|
|
370
381
|
return this.text(question, ctx);
|
|
371
382
|
}
|
|
@@ -404,6 +415,9 @@ class Inquirerer {
|
|
|
404
415
|
if (input.trim() !== '') {
|
|
405
416
|
resolve(input); // Return input if not empty
|
|
406
417
|
}
|
|
418
|
+
else if ('default' in question) {
|
|
419
|
+
resolve(question.default); // Use default if input is empty
|
|
420
|
+
}
|
|
407
421
|
else {
|
|
408
422
|
resolve(null); // Return null if empty and not required
|
|
409
423
|
}
|
|
@@ -428,18 +442,41 @@ class Inquirerer {
|
|
|
428
442
|
resolve(num);
|
|
429
443
|
}
|
|
430
444
|
else {
|
|
431
|
-
resolve(null);
|
|
445
|
+
resolve(null); // Let validation handle bad input
|
|
432
446
|
}
|
|
433
447
|
}
|
|
448
|
+
else if ('default' in question) {
|
|
449
|
+
resolve(question.default); // Use default if input is empty
|
|
450
|
+
}
|
|
434
451
|
else {
|
|
435
|
-
resolve(null);
|
|
452
|
+
resolve(null); // Empty and no default
|
|
436
453
|
}
|
|
437
454
|
});
|
|
438
455
|
});
|
|
439
456
|
}
|
|
440
457
|
async checkbox(question, ctx) {
|
|
441
|
-
if (this.noTty || !this.rl)
|
|
442
|
-
|
|
458
|
+
if (this.noTty || !this.rl) {
|
|
459
|
+
const options = this.sanitizeOptions(question);
|
|
460
|
+
const defaults = Array.isArray(question.default)
|
|
461
|
+
? question.default
|
|
462
|
+
: [question.default];
|
|
463
|
+
// If returnFullResults is true, return all options with boolean selection
|
|
464
|
+
if (question.returnFullResults) {
|
|
465
|
+
return options.map(opt => ({
|
|
466
|
+
name: opt.name,
|
|
467
|
+
value: opt.value,
|
|
468
|
+
selected: defaults.includes(opt.name)
|
|
469
|
+
}));
|
|
470
|
+
}
|
|
471
|
+
// Otherwise, return only selected options
|
|
472
|
+
return options
|
|
473
|
+
.filter(opt => defaults.includes(opt.name) || defaults.includes(opt.value))
|
|
474
|
+
.map(opt => ({
|
|
475
|
+
name: opt.name,
|
|
476
|
+
value: opt.value,
|
|
477
|
+
selected: true
|
|
478
|
+
}));
|
|
479
|
+
}
|
|
443
480
|
if (!question.options.length) {
|
|
444
481
|
// no arguments don't make sense
|
|
445
482
|
throw new Error('checkbox requires options');
|
|
@@ -451,7 +488,15 @@ class Inquirerer {
|
|
|
451
488
|
let selectedIndex = 0;
|
|
452
489
|
let startIndex = 0; // Start index for visible options
|
|
453
490
|
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
454
|
-
const selections = new Array(options.length).fill(false);
|
|
491
|
+
// const selections: boolean[] = new Array(options.length).fill(false);
|
|
492
|
+
const selections = options.map(opt => {
|
|
493
|
+
if (!question.default)
|
|
494
|
+
return false;
|
|
495
|
+
const defaults = Array.isArray(question.default)
|
|
496
|
+
? question.default
|
|
497
|
+
: [question.default];
|
|
498
|
+
return defaults.includes(opt.name);
|
|
499
|
+
});
|
|
455
500
|
const updateFilteredOptions = () => {
|
|
456
501
|
filteredOptions = this.filterOptions(options, input);
|
|
457
502
|
};
|
|
@@ -523,7 +568,8 @@ class Inquirerer {
|
|
|
523
568
|
options.forEach((option, index) => {
|
|
524
569
|
result.push({
|
|
525
570
|
name: option.name,
|
|
526
|
-
value:
|
|
571
|
+
value: option.value,
|
|
572
|
+
selected: selections[index]
|
|
527
573
|
});
|
|
528
574
|
});
|
|
529
575
|
}
|
|
@@ -533,7 +579,8 @@ class Inquirerer {
|
|
|
533
579
|
if (selections[index]) {
|
|
534
580
|
result.push({
|
|
535
581
|
name: option.name,
|
|
536
|
-
value:
|
|
582
|
+
value: option.value,
|
|
583
|
+
selected: selections[index]
|
|
537
584
|
});
|
|
538
585
|
}
|
|
539
586
|
});
|
package/question/types.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export interface Value {
|
|
|
5
5
|
export interface OptionValue {
|
|
6
6
|
name: string;
|
|
7
7
|
value: any;
|
|
8
|
+
selected?: boolean;
|
|
8
9
|
}
|
|
9
10
|
export interface Validation {
|
|
10
11
|
type?: string;
|
|
@@ -34,12 +35,19 @@ export interface AutocompleteQuestion extends BaseQuestion {
|
|
|
34
35
|
returnFullResults?: boolean;
|
|
35
36
|
allowCustomOptions?: boolean;
|
|
36
37
|
}
|
|
38
|
+
export interface ListQuestion extends BaseQuestion {
|
|
39
|
+
type: 'list';
|
|
40
|
+
options: (string | OptionValue)[];
|
|
41
|
+
maxDisplayLines?: number;
|
|
42
|
+
returnFullResults?: boolean;
|
|
43
|
+
allowCustomOptions?: boolean;
|
|
44
|
+
}
|
|
37
45
|
export interface CheckboxQuestion extends BaseQuestion {
|
|
38
46
|
type: 'checkbox';
|
|
39
47
|
options: (string | OptionValue)[];
|
|
40
48
|
maxDisplayLines?: number;
|
|
41
49
|
returnFullResults?: boolean;
|
|
42
|
-
default?:
|
|
50
|
+
default?: string[];
|
|
43
51
|
}
|
|
44
52
|
export interface TextQuestion extends BaseQuestion {
|
|
45
53
|
type: 'text';
|
|
@@ -49,4 +57,4 @@ export interface NumberQuestion extends BaseQuestion {
|
|
|
49
57
|
type: 'number';
|
|
50
58
|
default?: number;
|
|
51
59
|
}
|
|
52
|
-
export type Question = ConfirmQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;
|
|
60
|
+
export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;
|