inquirerer 4.6.0 → 4.7.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/README.md CHANGED
@@ -45,6 +45,8 @@ npm install inquirerer
45
45
  - [Text Question](#text-question)
46
46
  - [Number Question](#number-question)
47
47
  - [Confirm Question](#confirm-question)
48
+ - [Boolean Question](#boolean-question)
49
+ - [JSON Question](#json-question)
48
50
  - [List Question](#list-question)
49
51
  - [Autocomplete Question](#autocomplete-question)
50
52
  - [Checkbox Question](#checkbox-question)
@@ -112,6 +114,8 @@ import {
112
114
  TextQuestion,
113
115
  NumberQuestion,
114
116
  ConfirmQuestion,
117
+ BooleanQuestion,
118
+ JsonQuestion,
115
119
  ListQuestion,
116
120
  AutocompleteQuestion,
117
121
  CheckboxQuestion,
@@ -320,6 +324,42 @@ Yes/no questions.
320
324
  }
321
325
  ```
322
326
 
327
+ #### Boolean Question
328
+
329
+ Alias for `confirm` — provides a semantic name for boolean fields. Behaves identically to `confirm` (y/n prompt).
330
+
331
+ ```typescript
332
+ {
333
+ type: 'boolean',
334
+ name: 'isActive',
335
+ message: 'Is this record active?',
336
+ default: true
337
+ }
338
+ ```
339
+
340
+ Useful when generating CLI prompts from schema types where the field type is `Boolean` rather than a yes/no confirmation.
341
+
342
+ #### JSON Question
343
+
344
+ Collect structured JSON input. Validates input with `JSON.parse()` — invalid JSON returns `null`.
345
+
346
+ ```typescript
347
+ {
348
+ type: 'json',
349
+ name: 'metadata',
350
+ message: 'Enter metadata',
351
+ default: { key: 'value' }
352
+ }
353
+ ```
354
+
355
+ The prompt displays a `(JSON)` hint. Users enter raw JSON strings:
356
+ ```bash
357
+ $ Enter metadata (JSON)
358
+ > {"email":"user@example.com","role":"admin"}
359
+ ```
360
+
361
+ In non-interactive mode, returns the `default` value if provided, otherwise `undefined`.
362
+
323
363
  #### List Question
324
364
 
325
365
  Select one option from a list (no search).
package/esm/prompt.js CHANGED
@@ -94,11 +94,18 @@ function generatePromptMessage(question, ctx) {
94
94
  // 2. Append default inline (only if present)
95
95
  switch (type) {
96
96
  case 'confirm':
97
+ case 'boolean':
97
98
  promptLine += ' (y/n)';
98
99
  if (def !== undefined) {
99
100
  promptLine += ` ${yellow(`[${def ? 'y' : 'n'}]`)}`;
100
101
  }
101
102
  break;
103
+ case 'json':
104
+ promptLine += dim(' (JSON)');
105
+ if (def !== undefined) {
106
+ promptLine += ` ${yellow(`[${JSON.stringify(def)}]`)}`;
107
+ }
108
+ break;
102
109
  case 'text':
103
110
  case 'number':
104
111
  if (def !== undefined) {
@@ -628,6 +635,10 @@ export class Inquirerer {
628
635
  switch (question.type) {
629
636
  case 'confirm':
630
637
  return this.confirm(question, ctx);
638
+ case 'boolean':
639
+ return this.confirm(question, ctx);
640
+ case 'json':
641
+ return this.json(question, ctx);
631
642
  case 'checkbox':
632
643
  return this.checkbox(question, ctx);
633
644
  case 'list':
@@ -687,6 +698,36 @@ export class Inquirerer {
687
698
  });
688
699
  });
689
700
  }
701
+ async json(question, ctx) {
702
+ if (this.noTty || !this.rl) {
703
+ if ('default' in question) {
704
+ return question.default;
705
+ }
706
+ return;
707
+ }
708
+ let input = '';
709
+ return new Promise((resolve) => {
710
+ this.clearScreen();
711
+ this.rl.question(this.getPrompt(question, ctx, input), (answer) => {
712
+ input = answer.trim();
713
+ if (input !== '') {
714
+ try {
715
+ const parsed = JSON.parse(input);
716
+ resolve(parsed);
717
+ }
718
+ catch {
719
+ resolve(null); // Let validation handle invalid JSON
720
+ }
721
+ }
722
+ else if ('default' in question) {
723
+ resolve(question.default);
724
+ }
725
+ else {
726
+ resolve(null);
727
+ }
728
+ });
729
+ });
730
+ }
690
731
  async number(question, ctx) {
691
732
  if (this.noTty || !this.rl) {
692
733
  if ('default' in question) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
6
6
  "main": "index.js",
@@ -46,5 +46,5 @@
46
46
  "makage": "0.1.10",
47
47
  "string-width": "4.2.3"
48
48
  },
49
- "gitHead": "52e4d3ecdb5d323b70c2bb0269ff3ccd41cc85b9"
49
+ "gitHead": "51bb1c60e7a4fa76e59bd333dd6db67659d31856"
50
50
  }
package/prompt.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Readable, Writable } from 'stream';
2
- import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, ListQuestion, NumberQuestion, OptionValue, PasswordQuestion, Question, TextQuestion, Validation } from './question';
2
+ import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, JsonQuestion, ListQuestion, NumberQuestion, OptionValue, PasswordQuestion, Question, TextQuestion, Validation } from './question';
3
3
  import { DefaultResolverRegistry } from './resolvers';
4
4
  export interface ManPageInfo {
5
5
  commandName: string;
@@ -109,6 +109,7 @@ export declare class Inquirerer {
109
109
  private handleQuestionType;
110
110
  confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
111
111
  text(question: TextQuestion, ctx: PromptContext): Promise<string | null>;
112
+ json(question: JsonQuestion, ctx: PromptContext): Promise<Record<string, unknown> | null>;
112
113
  number(question: NumberQuestion, ctx: PromptContext): Promise<number | null>;
113
114
  password(question: PasswordQuestion, ctx: PromptContext): Promise<string | null>;
114
115
  checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<OptionValue[]>;
package/prompt.js CHANGED
@@ -101,11 +101,18 @@ function generatePromptMessage(question, ctx) {
101
101
  // 2. Append default inline (only if present)
102
102
  switch (type) {
103
103
  case 'confirm':
104
+ case 'boolean':
104
105
  promptLine += ' (y/n)';
105
106
  if (def !== undefined) {
106
107
  promptLine += ` ${(0, yanse_1.yellow)(`[${def ? 'y' : 'n'}]`)}`;
107
108
  }
108
109
  break;
110
+ case 'json':
111
+ promptLine += (0, yanse_1.dim)(' (JSON)');
112
+ if (def !== undefined) {
113
+ promptLine += ` ${(0, yanse_1.yellow)(`[${JSON.stringify(def)}]`)}`;
114
+ }
115
+ break;
109
116
  case 'text':
110
117
  case 'number':
111
118
  if (def !== undefined) {
@@ -635,6 +642,10 @@ class Inquirerer {
635
642
  switch (question.type) {
636
643
  case 'confirm':
637
644
  return this.confirm(question, ctx);
645
+ case 'boolean':
646
+ return this.confirm(question, ctx);
647
+ case 'json':
648
+ return this.json(question, ctx);
638
649
  case 'checkbox':
639
650
  return this.checkbox(question, ctx);
640
651
  case 'list':
@@ -694,6 +705,36 @@ class Inquirerer {
694
705
  });
695
706
  });
696
707
  }
708
+ async json(question, ctx) {
709
+ if (this.noTty || !this.rl) {
710
+ if ('default' in question) {
711
+ return question.default;
712
+ }
713
+ return;
714
+ }
715
+ let input = '';
716
+ return new Promise((resolve) => {
717
+ this.clearScreen();
718
+ this.rl.question(this.getPrompt(question, ctx, input), (answer) => {
719
+ input = answer.trim();
720
+ if (input !== '') {
721
+ try {
722
+ const parsed = JSON.parse(input);
723
+ resolve(parsed);
724
+ }
725
+ catch {
726
+ resolve(null); // Let validation handle invalid JSON
727
+ }
728
+ }
729
+ else if ('default' in question) {
730
+ resolve(question.default);
731
+ }
732
+ else {
733
+ resolve(null);
734
+ }
735
+ });
736
+ });
737
+ }
697
738
  async number(question, ctx) {
698
739
  if (this.noTty || !this.rl) {
699
740
  if ('default' in question) {
@@ -36,6 +36,14 @@ export interface ConfirmQuestion extends BaseQuestion {
36
36
  type: 'confirm';
37
37
  default?: boolean;
38
38
  }
39
+ export interface BooleanQuestion extends BaseQuestion {
40
+ type: 'boolean';
41
+ default?: boolean;
42
+ }
43
+ export interface JsonQuestion extends BaseQuestion {
44
+ type: 'json';
45
+ default?: Record<string, unknown>;
46
+ }
39
47
  export interface AutocompleteQuestion extends BaseQuestion {
40
48
  type: 'autocomplete';
41
49
  options: (string | OptionValue)[];
@@ -68,4 +76,4 @@ export interface PasswordQuestion extends BaseQuestion {
68
76
  type: 'password';
69
77
  mask?: string;
70
78
  }
71
- export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion | PasswordQuestion;
79
+ export type Question = ConfirmQuestion | BooleanQuestion | JsonQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion | PasswordQuestion;