inquirerer 2.0.1 → 2.0.3
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 +101 -37
- package/package.json +2 -2
- package/prompt.d.ts +1 -0
- package/prompt.js +101 -37
package/esm/prompt.js
CHANGED
|
@@ -52,39 +52,95 @@ class PromptContext {
|
|
|
52
52
|
return this.validation;
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
function generatePromptMessageSuccinct(question, ctx) {
|
|
56
|
+
const { message, name, type, default: def, options = [] } = question;
|
|
57
|
+
const lines = [];
|
|
58
|
+
// 1. Main prompt label
|
|
59
|
+
lines.push(chalk.whiteBright.bold(message || `${name}?`));
|
|
60
|
+
// 2. Validation message if applicable
|
|
61
|
+
const validation = validationMessage(question, ctx);
|
|
62
|
+
if (validation) {
|
|
63
|
+
lines.push(validation); // already styled red
|
|
64
|
+
}
|
|
65
|
+
// 3. Append default inline (only if present)
|
|
66
|
+
let inline = '';
|
|
67
|
+
switch (type) {
|
|
63
68
|
case 'confirm':
|
|
64
|
-
|
|
69
|
+
inline = '(y/n)';
|
|
70
|
+
if (def !== undefined) {
|
|
71
|
+
inline += ` ${chalk.yellow(`[${def ? 'y' : 'n'}]`)}`;
|
|
72
|
+
}
|
|
65
73
|
break;
|
|
66
74
|
case 'text':
|
|
67
|
-
|
|
68
|
-
|
|
75
|
+
case 'number':
|
|
76
|
+
if (def !== undefined) {
|
|
77
|
+
inline += `${chalk.yellow(`[${def}]`)}`;
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case 'autocomplete':
|
|
81
|
+
case 'list':
|
|
82
|
+
case 'checkbox':
|
|
83
|
+
if (def !== undefined) {
|
|
84
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
85
|
+
const rendered = defaults.map(d => chalk.yellow(d)).join(chalk.gray(', '));
|
|
86
|
+
inline += `${chalk.yellow(`[${rendered}]`)}`;
|
|
69
87
|
}
|
|
70
88
|
break;
|
|
89
|
+
}
|
|
90
|
+
if (inline) {
|
|
91
|
+
lines[lines.length - 1] += ' ' + inline; // append to prompt line
|
|
92
|
+
}
|
|
93
|
+
// 4. Final input line
|
|
94
|
+
lines.push('> ');
|
|
95
|
+
return lines.join('\n');
|
|
96
|
+
}
|
|
97
|
+
function generatePromptMessage(question, ctx) {
|
|
98
|
+
const { message, name, type, default: def, options = [], description } = question;
|
|
99
|
+
const lines = [];
|
|
100
|
+
// 1. Title Message
|
|
101
|
+
lines.push(chalk.whiteBright.bold(message || `${name}?`));
|
|
102
|
+
// 2. Optional description below title
|
|
103
|
+
if (description) {
|
|
104
|
+
lines.push(chalk.dim(description));
|
|
105
|
+
}
|
|
106
|
+
// 3. Validation warning (if failed before)
|
|
107
|
+
const validation = validationMessage(question, ctx);
|
|
108
|
+
if (validation) {
|
|
109
|
+
lines.push(validation); // already red-colored
|
|
110
|
+
}
|
|
111
|
+
// 4. Metadata (name/type)
|
|
112
|
+
lines.push(`${chalk.dim('Argument')} ${chalk.green(`--${name}`)} ${chalk.dim('type')} ${chalk.cyan(`[${type}]`)}`);
|
|
113
|
+
// 5. Default value or guidance
|
|
114
|
+
let guidance = '';
|
|
115
|
+
switch (type) {
|
|
116
|
+
case 'confirm':
|
|
117
|
+
guidance = '(y/n)';
|
|
118
|
+
if (def !== undefined) {
|
|
119
|
+
guidance += ` ${chalk.yellow(`[default: ${def ? 'y' : 'n'}]`)}`;
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
case 'text':
|
|
71
123
|
case 'number':
|
|
72
|
-
if (
|
|
73
|
-
|
|
124
|
+
if (def !== undefined) {
|
|
125
|
+
guidance = chalk.yellow(`[default: ${def}]`);
|
|
74
126
|
}
|
|
75
127
|
break;
|
|
76
128
|
case 'autocomplete':
|
|
77
129
|
case 'list':
|
|
78
130
|
case 'checkbox':
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
promptMessage += ` [${defaultText}]`;
|
|
131
|
+
if (def !== undefined) {
|
|
132
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
133
|
+
const rendered = defaults.map(d => chalk.yellow(d)).join(chalk.gray(', '));
|
|
134
|
+
guidance += `${chalk.yellow(`[default: ${rendered}]`)}`;
|
|
84
135
|
}
|
|
85
136
|
break;
|
|
86
137
|
}
|
|
87
|
-
|
|
138
|
+
if (guidance) {
|
|
139
|
+
lines.push(guidance);
|
|
140
|
+
}
|
|
141
|
+
// 6. Final input prompt
|
|
142
|
+
lines.push(chalk.white('> ') + chalk.dim('Your input:'));
|
|
143
|
+
return lines.join('\n') + '\n';
|
|
88
144
|
}
|
|
89
145
|
export class Inquirerer {
|
|
90
146
|
rl;
|
|
@@ -112,6 +168,7 @@ export class Inquirerer {
|
|
|
112
168
|
}
|
|
113
169
|
else {
|
|
114
170
|
this.rl = null;
|
|
171
|
+
this.keypress = null;
|
|
115
172
|
}
|
|
116
173
|
}
|
|
117
174
|
clearScreen() {
|
|
@@ -332,6 +389,9 @@ export class Inquirerer {
|
|
|
332
389
|
});
|
|
333
390
|
}
|
|
334
391
|
handleOverrides(argv, obj, question) {
|
|
392
|
+
if (!Object.prototype.hasOwnProperty.call(argv, question.name)) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
335
395
|
switch (question.type) {
|
|
336
396
|
case 'text':
|
|
337
397
|
case 'number':
|
|
@@ -339,6 +399,8 @@ export class Inquirerer {
|
|
|
339
399
|
// do nothing, already set!
|
|
340
400
|
break;
|
|
341
401
|
case 'checkbox':
|
|
402
|
+
this.handleOverridesForCheckboxOptions(argv, obj, question);
|
|
403
|
+
break;
|
|
342
404
|
case 'autocomplete':
|
|
343
405
|
case 'list':
|
|
344
406
|
// get the value from options :)
|
|
@@ -348,18 +410,34 @@ export class Inquirerer {
|
|
|
348
410
|
return;
|
|
349
411
|
}
|
|
350
412
|
}
|
|
413
|
+
handleOverridesForCheckboxOptions(argv, obj, question) {
|
|
414
|
+
const options = this.sanitizeOptions(question);
|
|
415
|
+
const input = argv[question.name];
|
|
416
|
+
if (typeof input === 'string') {
|
|
417
|
+
const found = options.filter(opt => opt.name === input || String(opt.value) === input);
|
|
418
|
+
if (found.length) {
|
|
419
|
+
obj[question.name] = found;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else if (Array.isArray(input)) {
|
|
423
|
+
const found = options.filter(opt => input.includes(opt.name) || input.includes(String(opt.value)));
|
|
424
|
+
if (found.length) {
|
|
425
|
+
obj[question.name] = found;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
351
429
|
handleOverridesWithOptions(argv, obj, question) {
|
|
352
|
-
|
|
353
|
-
if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
|
|
430
|
+
if (typeof argv[question.name] === 'string') {
|
|
354
431
|
const options = this.sanitizeOptions(question);
|
|
355
|
-
const
|
|
432
|
+
const input = argv[question.name];
|
|
433
|
+
const found = options.find(opt => opt.name === input || String(opt.value) === input);
|
|
356
434
|
if (typeof found !== 'undefined') {
|
|
357
435
|
obj[question.name] = found.value;
|
|
358
436
|
}
|
|
359
437
|
}
|
|
360
438
|
}
|
|
361
439
|
async handleQuestionType(question, ctx) {
|
|
362
|
-
this.keypress
|
|
440
|
+
this.keypress?.clearHandlers();
|
|
363
441
|
switch (question.type) {
|
|
364
442
|
case 'confirm':
|
|
365
443
|
return this.confirm(question, ctx);
|
|
@@ -711,20 +789,6 @@ export class Inquirerer {
|
|
|
711
789
|
}
|
|
712
790
|
};
|
|
713
791
|
display();
|
|
714
|
-
// // Handling these because otherwise they're bound by OTHER methods! memory leak.
|
|
715
|
-
// // e.g. you have checkbox before, then you have a list.... if you don't also do keys and backspace
|
|
716
|
-
// // it will start to display the closure from the
|
|
717
|
-
// // @ts-ignore
|
|
718
|
-
// // this.keypress.input.removeAllListeners('data');
|
|
719
|
-
// // Handling BACKSPACE key
|
|
720
|
-
// this.keypress.off(KEY_CODES.BACKSPACE);
|
|
721
|
-
// // Register alphanumeric and space keypresses to accumulate input
|
|
722
|
-
// 'abcdefghijklmnopqrstuvwxyz0123456789 '.split('').forEach(char => {
|
|
723
|
-
// this.keypress.off(char);
|
|
724
|
-
// });
|
|
725
|
-
// this.keypress.on(KEY_CODES.SPACE, () => {
|
|
726
|
-
// display();
|
|
727
|
-
// });
|
|
728
792
|
// Navigation
|
|
729
793
|
this.keypress.on(KEY_CODES.UP_ARROW, () => {
|
|
730
794
|
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
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": "9f50b55ef2ade53350fb6983785257b68639e032"
|
|
39
39
|
}
|
package/prompt.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ export declare class Inquirerer {
|
|
|
59
59
|
private applyDefaultValues;
|
|
60
60
|
private applyOverrides;
|
|
61
61
|
private handleOverrides;
|
|
62
|
+
private handleOverridesForCheckboxOptions;
|
|
62
63
|
private handleOverridesWithOptions;
|
|
63
64
|
private handleQuestionType;
|
|
64
65
|
confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
|
package/prompt.js
CHANGED
|
@@ -58,39 +58,95 @@ class PromptContext {
|
|
|
58
58
|
return this.validation;
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
function generatePromptMessageSuccinct(question, ctx) {
|
|
62
|
+
const { message, name, type, default: def, options = [] } = question;
|
|
63
|
+
const lines = [];
|
|
64
|
+
// 1. Main prompt label
|
|
65
|
+
lines.push(chalk_1.default.whiteBright.bold(message || `${name}?`));
|
|
66
|
+
// 2. Validation message if applicable
|
|
67
|
+
const validation = validationMessage(question, ctx);
|
|
68
|
+
if (validation) {
|
|
69
|
+
lines.push(validation); // already styled red
|
|
70
|
+
}
|
|
71
|
+
// 3. Append default inline (only if present)
|
|
72
|
+
let inline = '';
|
|
73
|
+
switch (type) {
|
|
69
74
|
case 'confirm':
|
|
70
|
-
|
|
75
|
+
inline = '(y/n)';
|
|
76
|
+
if (def !== undefined) {
|
|
77
|
+
inline += ` ${chalk_1.default.yellow(`[${def ? 'y' : 'n'}]`)}`;
|
|
78
|
+
}
|
|
71
79
|
break;
|
|
72
80
|
case 'text':
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
case 'number':
|
|
82
|
+
if (def !== undefined) {
|
|
83
|
+
inline += `${chalk_1.default.yellow(`[${def}]`)}`;
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
case 'autocomplete':
|
|
87
|
+
case 'list':
|
|
88
|
+
case 'checkbox':
|
|
89
|
+
if (def !== undefined) {
|
|
90
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
91
|
+
const rendered = defaults.map(d => chalk_1.default.yellow(d)).join(chalk_1.default.gray(', '));
|
|
92
|
+
inline += `${chalk_1.default.yellow(`[${rendered}]`)}`;
|
|
75
93
|
}
|
|
76
94
|
break;
|
|
95
|
+
}
|
|
96
|
+
if (inline) {
|
|
97
|
+
lines[lines.length - 1] += ' ' + inline; // append to prompt line
|
|
98
|
+
}
|
|
99
|
+
// 4. Final input line
|
|
100
|
+
lines.push('> ');
|
|
101
|
+
return lines.join('\n');
|
|
102
|
+
}
|
|
103
|
+
function generatePromptMessage(question, ctx) {
|
|
104
|
+
const { message, name, type, default: def, options = [], description } = question;
|
|
105
|
+
const lines = [];
|
|
106
|
+
// 1. Title Message
|
|
107
|
+
lines.push(chalk_1.default.whiteBright.bold(message || `${name}?`));
|
|
108
|
+
// 2. Optional description below title
|
|
109
|
+
if (description) {
|
|
110
|
+
lines.push(chalk_1.default.dim(description));
|
|
111
|
+
}
|
|
112
|
+
// 3. Validation warning (if failed before)
|
|
113
|
+
const validation = validationMessage(question, ctx);
|
|
114
|
+
if (validation) {
|
|
115
|
+
lines.push(validation); // already red-colored
|
|
116
|
+
}
|
|
117
|
+
// 4. Metadata (name/type)
|
|
118
|
+
lines.push(`${chalk_1.default.dim('Argument')} ${chalk_1.default.green(`--${name}`)} ${chalk_1.default.dim('type')} ${chalk_1.default.cyan(`[${type}]`)}`);
|
|
119
|
+
// 5. Default value or guidance
|
|
120
|
+
let guidance = '';
|
|
121
|
+
switch (type) {
|
|
122
|
+
case 'confirm':
|
|
123
|
+
guidance = '(y/n)';
|
|
124
|
+
if (def !== undefined) {
|
|
125
|
+
guidance += ` ${chalk_1.default.yellow(`[default: ${def ? 'y' : 'n'}]`)}`;
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
case 'text':
|
|
77
129
|
case 'number':
|
|
78
|
-
if (
|
|
79
|
-
|
|
130
|
+
if (def !== undefined) {
|
|
131
|
+
guidance = chalk_1.default.yellow(`[default: ${def}]`);
|
|
80
132
|
}
|
|
81
133
|
break;
|
|
82
134
|
case 'autocomplete':
|
|
83
135
|
case 'list':
|
|
84
136
|
case 'checkbox':
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
promptMessage += ` [${defaultText}]`;
|
|
137
|
+
if (def !== undefined) {
|
|
138
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
139
|
+
const rendered = defaults.map(d => chalk_1.default.yellow(d)).join(chalk_1.default.gray(', '));
|
|
140
|
+
guidance += `${chalk_1.default.yellow(`[default: ${rendered}]`)}`;
|
|
90
141
|
}
|
|
91
142
|
break;
|
|
92
143
|
}
|
|
93
|
-
|
|
144
|
+
if (guidance) {
|
|
145
|
+
lines.push(guidance);
|
|
146
|
+
}
|
|
147
|
+
// 6. Final input prompt
|
|
148
|
+
lines.push(chalk_1.default.white('> ') + chalk_1.default.dim('Your input:'));
|
|
149
|
+
return lines.join('\n') + '\n';
|
|
94
150
|
}
|
|
95
151
|
class Inquirerer {
|
|
96
152
|
rl;
|
|
@@ -118,6 +174,7 @@ class Inquirerer {
|
|
|
118
174
|
}
|
|
119
175
|
else {
|
|
120
176
|
this.rl = null;
|
|
177
|
+
this.keypress = null;
|
|
121
178
|
}
|
|
122
179
|
}
|
|
123
180
|
clearScreen() {
|
|
@@ -338,6 +395,9 @@ class Inquirerer {
|
|
|
338
395
|
});
|
|
339
396
|
}
|
|
340
397
|
handleOverrides(argv, obj, question) {
|
|
398
|
+
if (!Object.prototype.hasOwnProperty.call(argv, question.name)) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
341
401
|
switch (question.type) {
|
|
342
402
|
case 'text':
|
|
343
403
|
case 'number':
|
|
@@ -345,6 +405,8 @@ class Inquirerer {
|
|
|
345
405
|
// do nothing, already set!
|
|
346
406
|
break;
|
|
347
407
|
case 'checkbox':
|
|
408
|
+
this.handleOverridesForCheckboxOptions(argv, obj, question);
|
|
409
|
+
break;
|
|
348
410
|
case 'autocomplete':
|
|
349
411
|
case 'list':
|
|
350
412
|
// get the value from options :)
|
|
@@ -354,18 +416,34 @@ class Inquirerer {
|
|
|
354
416
|
return;
|
|
355
417
|
}
|
|
356
418
|
}
|
|
419
|
+
handleOverridesForCheckboxOptions(argv, obj, question) {
|
|
420
|
+
const options = this.sanitizeOptions(question);
|
|
421
|
+
const input = argv[question.name];
|
|
422
|
+
if (typeof input === 'string') {
|
|
423
|
+
const found = options.filter(opt => opt.name === input || String(opt.value) === input);
|
|
424
|
+
if (found.length) {
|
|
425
|
+
obj[question.name] = found;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
else if (Array.isArray(input)) {
|
|
429
|
+
const found = options.filter(opt => input.includes(opt.name) || input.includes(String(opt.value)));
|
|
430
|
+
if (found.length) {
|
|
431
|
+
obj[question.name] = found;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
357
435
|
handleOverridesWithOptions(argv, obj, question) {
|
|
358
|
-
|
|
359
|
-
if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
|
|
436
|
+
if (typeof argv[question.name] === 'string') {
|
|
360
437
|
const options = this.sanitizeOptions(question);
|
|
361
|
-
const
|
|
438
|
+
const input = argv[question.name];
|
|
439
|
+
const found = options.find(opt => opt.name === input || String(opt.value) === input);
|
|
362
440
|
if (typeof found !== 'undefined') {
|
|
363
441
|
obj[question.name] = found.value;
|
|
364
442
|
}
|
|
365
443
|
}
|
|
366
444
|
}
|
|
367
445
|
async handleQuestionType(question, ctx) {
|
|
368
|
-
this.keypress
|
|
446
|
+
this.keypress?.clearHandlers();
|
|
369
447
|
switch (question.type) {
|
|
370
448
|
case 'confirm':
|
|
371
449
|
return this.confirm(question, ctx);
|
|
@@ -717,20 +795,6 @@ class Inquirerer {
|
|
|
717
795
|
}
|
|
718
796
|
};
|
|
719
797
|
display();
|
|
720
|
-
// // Handling these because otherwise they're bound by OTHER methods! memory leak.
|
|
721
|
-
// // e.g. you have checkbox before, then you have a list.... if you don't also do keys and backspace
|
|
722
|
-
// // it will start to display the closure from the
|
|
723
|
-
// // @ts-ignore
|
|
724
|
-
// // this.keypress.input.removeAllListeners('data');
|
|
725
|
-
// // Handling BACKSPACE key
|
|
726
|
-
// this.keypress.off(KEY_CODES.BACKSPACE);
|
|
727
|
-
// // Register alphanumeric and space keypresses to accumulate input
|
|
728
|
-
// 'abcdefghijklmnopqrstuvwxyz0123456789 '.split('').forEach(char => {
|
|
729
|
-
// this.keypress.off(char);
|
|
730
|
-
// });
|
|
731
|
-
// this.keypress.on(KEY_CODES.SPACE, () => {
|
|
732
|
-
// display();
|
|
733
|
-
// });
|
|
734
798
|
// Navigation
|
|
735
799
|
this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
|
|
736
800
|
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
|