inquirerer 4.5.2 → 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,
@@ -146,8 +150,10 @@ interface BaseQuestion {
146
150
  default?: any; // Default value
147
151
  defaultFrom?: string; // Dynamic default from resolver (e.g., 'git.user.name')
148
152
  setFrom?: string; // Auto-set value from resolver, bypassing prompt entirely
153
+ optionsFrom?: string; // Dynamic options from another answer's value
149
154
  useDefault?: boolean; // Skip prompt and use default
150
155
  required?: boolean; // Validation requirement
156
+ skipPrompt?: boolean; // Skip prompting entirely (field still in man pages / CLI flags)
151
157
  validate?: (input: any, answers: any) => boolean | Validation;
152
158
  sanitize?: (input: any, answers: any) => any;
153
159
  pattern?: string; // Regex pattern for validation
@@ -156,6 +162,40 @@ interface BaseQuestion {
156
162
  }
157
163
  ```
158
164
 
165
+ #### Skipping Prompts
166
+
167
+ Use `skipPrompt: true` to skip interactive prompting for a question entirely. The field is omitted from the answers object unless the user explicitly passes it via a CLI flag. This is useful for fields with backend-managed defaults where the CLI should not prompt, but should still allow overrides.
168
+
169
+ ```typescript
170
+ const questions: Question[] = [
171
+ {
172
+ type: 'text',
173
+ name: 'username',
174
+ message: 'Username',
175
+ required: true
176
+ },
177
+ {
178
+ type: 'text',
179
+ name: 'status',
180
+ message: 'Account status',
181
+ skipPrompt: true // Won't prompt, but user can pass --status active
182
+ }
183
+ ];
184
+
185
+ const result = await prompter.prompt({}, questions);
186
+ // { username: 'john' } — status is not included
187
+
188
+ const result2 = await prompter.prompt({ status: 'active' }, questions);
189
+ // { username: 'john', status: 'active' } — CLI flag override works
190
+ ```
191
+
192
+ Key behaviors:
193
+ - The question still appears in generated man pages
194
+ - CLI flag overrides (e.g. `--status active`) still work
195
+ - The field is simply left out of the answers if not provided
196
+ - Different from `when`: `skipPrompt` is unconditional, while `when` depends on other answers
197
+ - Different from `useDefault`: `skipPrompt` does not apply a default value
198
+
159
199
  ### Non-Interactive Mode
160
200
 
161
201
  When running in CI/CD or without a TTY, inquirerer automatically falls back to default values:
@@ -284,6 +324,42 @@ Yes/no questions.
284
324
  }
285
325
  ```
286
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
+
287
363
  #### List Question
288
364
 
289
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) {
@@ -360,6 +367,14 @@ export class Inquirerer {
360
367
  ctx.nextQuestion();
361
368
  continue;
362
369
  }
370
+ // Skip prompt entirely if skipPrompt is set.
371
+ // The question still appears in man pages and CLI flag overrides still work
372
+ // (handled by the `question.name in obj` check above), but no interactive
373
+ // prompt is shown. The field is simply left out of the answers object.
374
+ if (question.skipPrompt) {
375
+ ctx.nextQuestion();
376
+ continue;
377
+ }
363
378
  // Apply default value if applicable
364
379
  // this is if useDefault is set, rare! not typical defaults which happen AFTER
365
380
  // this is mostly to avoid a prompt for "hidden" options
@@ -620,6 +635,10 @@ export class Inquirerer {
620
635
  switch (question.type) {
621
636
  case 'confirm':
622
637
  return this.confirm(question, ctx);
638
+ case 'boolean':
639
+ return this.confirm(question, ctx);
640
+ case 'json':
641
+ return this.json(question, ctx);
623
642
  case 'checkbox':
624
643
  return this.checkbox(question, ctx);
625
644
  case 'list':
@@ -679,6 +698,36 @@ export class Inquirerer {
679
698
  });
680
699
  });
681
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
+ }
682
731
  async number(question, ctx) {
683
732
  if (this.noTty || !this.rl) {
684
733
  if ('default' in question) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "4.5.2",
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": "d591a7599e3e6ac48ade4eea3c0efe0f9f8af87b"
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) {
@@ -367,6 +374,14 @@ class Inquirerer {
367
374
  ctx.nextQuestion();
368
375
  continue;
369
376
  }
377
+ // Skip prompt entirely if skipPrompt is set.
378
+ // The question still appears in man pages and CLI flag overrides still work
379
+ // (handled by the `question.name in obj` check above), but no interactive
380
+ // prompt is shown. The field is simply left out of the answers object.
381
+ if (question.skipPrompt) {
382
+ ctx.nextQuestion();
383
+ continue;
384
+ }
370
385
  // Apply default value if applicable
371
386
  // this is if useDefault is set, rare! not typical defaults which happen AFTER
372
387
  // this is mostly to avoid a prompt for "hidden" options
@@ -627,6 +642,10 @@ class Inquirerer {
627
642
  switch (question.type) {
628
643
  case 'confirm':
629
644
  return this.confirm(question, ctx);
645
+ case 'boolean':
646
+ return this.confirm(question, ctx);
647
+ case 'json':
648
+ return this.json(question, ctx);
630
649
  case 'checkbox':
631
650
  return this.checkbox(question, ctx);
632
651
  case 'list':
@@ -686,6 +705,36 @@ class Inquirerer {
686
705
  });
687
706
  });
688
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
+ }
689
738
  async number(question, ctx) {
690
739
  if (this.noTty || !this.rl) {
691
740
  if ('default' in question) {
@@ -30,11 +30,20 @@ export interface BaseQuestion {
30
30
  pattern?: string;
31
31
  dependsOn?: string[];
32
32
  when?: (answers: any) => boolean;
33
+ skipPrompt?: boolean;
33
34
  }
34
35
  export interface ConfirmQuestion extends BaseQuestion {
35
36
  type: 'confirm';
36
37
  default?: boolean;
37
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
+ }
38
47
  export interface AutocompleteQuestion extends BaseQuestion {
39
48
  type: 'autocomplete';
40
49
  options: (string | OptionValue)[];
@@ -67,4 +76,4 @@ export interface PasswordQuestion extends BaseQuestion {
67
76
  type: 'password';
68
77
  mask?: string;
69
78
  }
70
- export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion | PasswordQuestion;
79
+ export type Question = ConfirmQuestion | BooleanQuestion | JsonQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion | PasswordQuestion;