inquirerer 1.9.0 → 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 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,9 +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);
370
+ case 'number':
371
+ return this.number(question, ctx);
360
372
  case 'text':
373
+ return this.text(question, ctx);
361
374
  default:
362
375
  return this.text(question, ctx);
363
376
  }
@@ -396,15 +409,68 @@ export class Inquirerer {
396
409
  if (input.trim() !== '') {
397
410
  resolve(input); // Return input if not empty
398
411
  }
412
+ else if ('default' in question) {
413
+ resolve(question.default); // Use default if input is empty
414
+ }
399
415
  else {
400
416
  resolve(null); // Return null if empty and not required
401
417
  }
402
418
  });
403
419
  });
404
420
  }
421
+ async number(question, ctx) {
422
+ if (this.noTty || !this.rl) {
423
+ if ('default' in question) {
424
+ return question.default;
425
+ }
426
+ return;
427
+ }
428
+ let input = '';
429
+ return new Promise((resolve) => {
430
+ this.clearScreen();
431
+ this.rl.question(this.getPrompt(question, ctx, input), (answer) => {
432
+ input = answer.trim();
433
+ if (input !== '') {
434
+ const num = Number(input);
435
+ if (!isNaN(num)) {
436
+ resolve(num);
437
+ }
438
+ else {
439
+ resolve(null); // Let validation handle bad input
440
+ }
441
+ }
442
+ else if ('default' in question) {
443
+ resolve(question.default); // Use default if input is empty
444
+ }
445
+ else {
446
+ resolve(null); // Empty and no default
447
+ }
448
+ });
449
+ });
450
+ }
405
451
  async checkbox(question, ctx) {
406
- if (this.noTty || !this.rl)
407
- return question.default ?? []; // Return default if non-interactive
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
+ }
408
474
  if (!question.options.length) {
409
475
  // no arguments don't make sense
410
476
  throw new Error('checkbox requires options');
@@ -416,7 +482,15 @@ export class Inquirerer {
416
482
  let selectedIndex = 0;
417
483
  let startIndex = 0; // Start index for visible options
418
484
  const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
419
- 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
+ });
420
494
  const updateFilteredOptions = () => {
421
495
  filteredOptions = this.filterOptions(options, input);
422
496
  };
@@ -488,7 +562,8 @@ export class Inquirerer {
488
562
  options.forEach((option, index) => {
489
563
  result.push({
490
564
  name: option.name,
491
- value: selections[index]
565
+ value: option.value,
566
+ selected: selections[index]
492
567
  });
493
568
  });
494
569
  }
@@ -498,7 +573,8 @@ export class Inquirerer {
498
573
  if (selections[index]) {
499
574
  result.push({
500
575
  name: option.name,
501
- value: selections[index]
576
+ value: option.value,
577
+ selected: selections[index]
502
578
  });
503
579
  }
504
580
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "1.9.0",
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": "9cda01fdb660defddb159c9a6afee578e833e9c1"
38
+ "gitHead": "aa6ac282c2a3019fef455f0c45e5cad1bbdeb53d"
39
39
  }
package/prompt.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable, Writable } from 'stream';
3
- import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, OptionValue, Question, TextQuestion, Validation } from './question';
3
+ import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, NumberQuestion, OptionValue, Question, TextQuestion, Validation } from './question';
4
4
  export interface ManPageInfo {
5
5
  commandName: string;
6
6
  questions: Question[];
@@ -63,6 +63,7 @@ export declare class Inquirerer {
63
63
  private handleQuestionType;
64
64
  confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
65
65
  text(question: TextQuestion, ctx: PromptContext): Promise<string | null>;
66
+ number(question: NumberQuestion, ctx: PromptContext): Promise<number | null>;
66
67
  checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<OptionValue[]>;
67
68
  autocomplete(question: AutocompleteQuestion, ctx: PromptContext): Promise<any>;
68
69
  private getOptionValue;
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,9 +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);
376
+ case 'number':
377
+ return this.number(question, ctx);
366
378
  case 'text':
379
+ return this.text(question, ctx);
367
380
  default:
368
381
  return this.text(question, ctx);
369
382
  }
@@ -402,15 +415,68 @@ class Inquirerer {
402
415
  if (input.trim() !== '') {
403
416
  resolve(input); // Return input if not empty
404
417
  }
418
+ else if ('default' in question) {
419
+ resolve(question.default); // Use default if input is empty
420
+ }
405
421
  else {
406
422
  resolve(null); // Return null if empty and not required
407
423
  }
408
424
  });
409
425
  });
410
426
  }
427
+ async number(question, ctx) {
428
+ if (this.noTty || !this.rl) {
429
+ if ('default' in question) {
430
+ return question.default;
431
+ }
432
+ return;
433
+ }
434
+ let input = '';
435
+ return new Promise((resolve) => {
436
+ this.clearScreen();
437
+ this.rl.question(this.getPrompt(question, ctx, input), (answer) => {
438
+ input = answer.trim();
439
+ if (input !== '') {
440
+ const num = Number(input);
441
+ if (!isNaN(num)) {
442
+ resolve(num);
443
+ }
444
+ else {
445
+ resolve(null); // Let validation handle bad input
446
+ }
447
+ }
448
+ else if ('default' in question) {
449
+ resolve(question.default); // Use default if input is empty
450
+ }
451
+ else {
452
+ resolve(null); // Empty and no default
453
+ }
454
+ });
455
+ });
456
+ }
411
457
  async checkbox(question, ctx) {
412
- if (this.noTty || !this.rl)
413
- return question.default ?? []; // Return default if non-interactive
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
+ }
414
480
  if (!question.options.length) {
415
481
  // no arguments don't make sense
416
482
  throw new Error('checkbox requires options');
@@ -422,7 +488,15 @@ class Inquirerer {
422
488
  let selectedIndex = 0;
423
489
  let startIndex = 0; // Start index for visible options
424
490
  const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
425
- 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
+ });
426
500
  const updateFilteredOptions = () => {
427
501
  filteredOptions = this.filterOptions(options, input);
428
502
  };
@@ -494,7 +568,8 @@ class Inquirerer {
494
568
  options.forEach((option, index) => {
495
569
  result.push({
496
570
  name: option.name,
497
- value: selections[index]
571
+ value: option.value,
572
+ selected: selections[index]
498
573
  });
499
574
  });
500
575
  }
@@ -504,7 +579,8 @@ class Inquirerer {
504
579
  if (selections[index]) {
505
580
  result.push({
506
581
  name: option.name,
507
- value: selections[index]
582
+ value: option.value,
583
+ selected: selections[index]
508
584
  });
509
585
  }
510
586
  });
@@ -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,15 +35,26 @@ 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?: Value[];
50
+ default?: string[];
43
51
  }
44
52
  export interface TextQuestion extends BaseQuestion {
45
53
  type: 'text';
46
54
  default?: string;
47
55
  }
48
- export type Question = ConfirmQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion;
56
+ export interface NumberQuestion extends BaseQuestion {
57
+ type: 'number';
58
+ default?: number;
59
+ }
60
+ export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;