inquirerer 2.0.1 → 2.0.2

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 CHANGED
@@ -52,39 +52,95 @@ class PromptContext {
52
52
  return this.validation;
53
53
  }
54
54
  }
55
- function generatePromptMessage(question, ctx) {
56
- let promptMessage = '';
57
- if (question.message) {
58
- promptMessage = chalk.whiteBright(question.message) + '\n';
59
- }
60
- promptMessage += `${chalk.white('[')}${chalk.green('--' + question.name)}${chalk.white(']:')}\n`;
61
- promptMessage = validationMessage(question, ctx) + promptMessage;
62
- switch (question.type) {
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
- promptMessage += `(y/n)${question.default !== undefined ? ` [${question.default ? 'y' : 'n'}]` : ''}`;
69
+ inline = '(y/n)';
70
+ if (def !== undefined) {
71
+ inline += ` ${chalk.yellow(`[${def ? 'y' : 'n'}]`)}`;
72
+ }
65
73
  break;
66
74
  case 'text':
67
- if (question.default) {
68
- promptMessage += ` [${question.default}]`;
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 (question.default) {
73
- promptMessage += ` [${question.default}]`;
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
- // For these types, you might want to show the default selected options if any
80
- if (question.options && question.default) {
81
- const defaultOptions = Array.isArray(question.default) ? question.default : [question.default];
82
- const defaultText = defaultOptions.join(', ');
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
- return promptMessage;
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;
@@ -332,6 +388,9 @@ export class Inquirerer {
332
388
  });
333
389
  }
334
390
  handleOverrides(argv, obj, question) {
391
+ if (!Object.prototype.hasOwnProperty.call(argv, question.name)) {
392
+ return;
393
+ }
335
394
  switch (question.type) {
336
395
  case 'text':
337
396
  case 'number':
@@ -339,6 +398,8 @@ export class Inquirerer {
339
398
  // do nothing, already set!
340
399
  break;
341
400
  case 'checkbox':
401
+ this.handleOverridesForCheckboxOptions(argv, obj, question);
402
+ break;
342
403
  case 'autocomplete':
343
404
  case 'list':
344
405
  // get the value from options :)
@@ -348,11 +409,27 @@ export class Inquirerer {
348
409
  return;
349
410
  }
350
411
  }
412
+ handleOverridesForCheckboxOptions(argv, obj, question) {
413
+ const options = this.sanitizeOptions(question);
414
+ const input = argv[question.name];
415
+ if (typeof input === 'string') {
416
+ const found = options.filter(opt => opt.name === input || String(opt.value) === input);
417
+ if (found.length) {
418
+ obj[question.name] = found;
419
+ }
420
+ }
421
+ else if (Array.isArray(input)) {
422
+ const found = options.filter(opt => input.includes(opt.name) || input.includes(String(opt.value)));
423
+ if (found.length) {
424
+ obj[question.name] = found;
425
+ }
426
+ }
427
+ }
351
428
  handleOverridesWithOptions(argv, obj, question) {
352
- // obj is already either argv itself, or a clone, but let's check if it has the property
353
- if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
429
+ if (typeof argv[question.name] === 'string') {
354
430
  const options = this.sanitizeOptions(question);
355
- const found = options.find(option => option.name === argv[question.name]);
431
+ const input = argv[question.name];
432
+ const found = options.find(opt => opt.name === input || String(opt.value) === input);
356
433
  if (typeof found !== 'undefined') {
357
434
  obj[question.name] = found.value;
358
435
  }
@@ -711,20 +788,6 @@ export class Inquirerer {
711
788
  }
712
789
  };
713
790
  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
791
  // Navigation
729
792
  this.keypress.on(KEY_CODES.UP_ARROW, () => {
730
793
  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.1",
3
+ "version": "2.0.2",
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": "b20090e1514228a9010745404b3d17e7716f054b"
38
+ "gitHead": "f98034d733c0a9f3f1b1c9565698362534d2c720"
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 generatePromptMessage(question, ctx) {
62
- let promptMessage = '';
63
- if (question.message) {
64
- promptMessage = chalk_1.default.whiteBright(question.message) + '\n';
65
- }
66
- promptMessage += `${chalk_1.default.white('[')}${chalk_1.default.green('--' + question.name)}${chalk_1.default.white(']:')}\n`;
67
- promptMessage = validationMessage(question, ctx) + promptMessage;
68
- switch (question.type) {
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
- promptMessage += `(y/n)${question.default !== undefined ? ` [${question.default ? 'y' : 'n'}]` : ''}`;
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
- if (question.default) {
74
- promptMessage += ` [${question.default}]`;
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 (question.default) {
79
- promptMessage += ` [${question.default}]`;
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
- // For these types, you might want to show the default selected options if any
86
- if (question.options && question.default) {
87
- const defaultOptions = Array.isArray(question.default) ? question.default : [question.default];
88
- const defaultText = defaultOptions.join(', ');
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
- return promptMessage;
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;
@@ -338,6 +394,9 @@ class Inquirerer {
338
394
  });
339
395
  }
340
396
  handleOverrides(argv, obj, question) {
397
+ if (!Object.prototype.hasOwnProperty.call(argv, question.name)) {
398
+ return;
399
+ }
341
400
  switch (question.type) {
342
401
  case 'text':
343
402
  case 'number':
@@ -345,6 +404,8 @@ class Inquirerer {
345
404
  // do nothing, already set!
346
405
  break;
347
406
  case 'checkbox':
407
+ this.handleOverridesForCheckboxOptions(argv, obj, question);
408
+ break;
348
409
  case 'autocomplete':
349
410
  case 'list':
350
411
  // get the value from options :)
@@ -354,11 +415,27 @@ class Inquirerer {
354
415
  return;
355
416
  }
356
417
  }
418
+ handleOverridesForCheckboxOptions(argv, obj, question) {
419
+ const options = this.sanitizeOptions(question);
420
+ const input = argv[question.name];
421
+ if (typeof input === 'string') {
422
+ const found = options.filter(opt => opt.name === input || String(opt.value) === input);
423
+ if (found.length) {
424
+ obj[question.name] = found;
425
+ }
426
+ }
427
+ else if (Array.isArray(input)) {
428
+ const found = options.filter(opt => input.includes(opt.name) || input.includes(String(opt.value)));
429
+ if (found.length) {
430
+ obj[question.name] = found;
431
+ }
432
+ }
433
+ }
357
434
  handleOverridesWithOptions(argv, obj, question) {
358
- // obj is already either argv itself, or a clone, but let's check if it has the property
359
- if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
435
+ if (typeof argv[question.name] === 'string') {
360
436
  const options = this.sanitizeOptions(question);
361
- const found = options.find(option => option.name === argv[question.name]);
437
+ const input = argv[question.name];
438
+ const found = options.find(opt => opt.name === input || String(opt.value) === input);
362
439
  if (typeof found !== 'undefined') {
363
440
  obj[question.name] = found.value;
364
441
  }
@@ -717,20 +794,6 @@ class Inquirerer {
717
794
  }
718
795
  };
719
796
  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
797
  // Navigation
735
798
  this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
736
799
  selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;