inquirerer 4.6.0 → 4.8.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)
@@ -71,6 +73,7 @@ npm install inquirerer
71
73
  - [Progress Bar](#progress-bar)
72
74
  - [Streaming Text](#streaming-text)
73
75
  - [Custom UI with UIEngine](#custom-ui-with-uiengine)
76
+ - [Testing](#testing)
74
77
  - [Developing](#developing)
75
78
 
76
79
  ## Quick Start
@@ -112,6 +115,8 @@ import {
112
115
  TextQuestion,
113
116
  NumberQuestion,
114
117
  ConfirmQuestion,
118
+ BooleanQuestion,
119
+ JsonQuestion,
115
120
  ListQuestion,
116
121
  AutocompleteQuestion,
117
122
  CheckboxQuestion,
@@ -320,6 +325,42 @@ Yes/no questions.
320
325
  }
321
326
  ```
322
327
 
328
+ #### Boolean Question
329
+
330
+ Alias for `confirm` — provides a semantic name for boolean fields. Behaves identically to `confirm` (y/n prompt).
331
+
332
+ ```typescript
333
+ {
334
+ type: 'boolean',
335
+ name: 'isActive',
336
+ message: 'Is this record active?',
337
+ default: true
338
+ }
339
+ ```
340
+
341
+ Useful when generating CLI prompts from schema types where the field type is `Boolean` rather than a yes/no confirmation.
342
+
343
+ #### JSON Question
344
+
345
+ Collect structured JSON input. Validates input with `JSON.parse()` — invalid JSON returns `null`.
346
+
347
+ ```typescript
348
+ {
349
+ type: 'json',
350
+ name: 'metadata',
351
+ message: 'Enter metadata',
352
+ default: { key: 'value' }
353
+ }
354
+ ```
355
+
356
+ The prompt displays a `(JSON)` hint. Users enter raw JSON strings:
357
+ ```bash
358
+ $ Enter metadata (JSON)
359
+ > {"email":"user@example.com","role":"admin"}
360
+ ```
361
+
362
+ In non-interactive mode, returns the `default` value if provided, otherwise `undefined`.
363
+
323
364
  #### List Question
324
365
 
325
366
  Select one option from a list (no search).
@@ -1602,6 +1643,51 @@ await engine.run({
1602
1643
  });
1603
1644
  ```
1604
1645
 
1646
+ ## Testing
1647
+
1648
+ For testing CLI applications built with `inquirerer`, use the companion
1649
+ package [`@inquirerer/test`](https://www.npmjs.com/package/@inquirerer/test)
1650
+ (also in this repo). It provides everything you need to mock stdin/stdout,
1651
+ queue keypresses and readline responses, capture and snapshot output, and
1652
+ even drive the built executable as a subprocess for end-to-end tests —
1653
+ without you having to hand-roll any of that scaffolding.
1654
+
1655
+ ```bash
1656
+ npm install --save-dev @inquirerer/test
1657
+ ```
1658
+
1659
+ **In-process testing** (mock streams):
1660
+
1661
+ ```typescript
1662
+ import { createTestEnvironment, KEY_SEQUENCES } from '@inquirerer/test';
1663
+ import { Inquirerer } from 'inquirerer';
1664
+
1665
+ const env = createTestEnvironment();
1666
+ env.sendKey(KEY_SEQUENCES.ENTER);
1667
+
1668
+ const prompter = new Inquirerer(env.options);
1669
+ const result = await prompter.prompt({}, [
1670
+ { name: 'confirm', type: 'confirm', message: 'Continue?' }
1671
+ ]);
1672
+
1673
+ expect(result.confirm).toBe(true);
1674
+ expect(env.getOutput()).toContain('Continue?');
1675
+ ```
1676
+
1677
+ **Subprocess (E2E) testing** — drive the actual built CLI:
1678
+
1679
+ ```typescript
1680
+ import { runCli } from '@inquirerer/test';
1681
+
1682
+ const { stdout, exitCode } = await runCli('node', [CLI_ENTRY, 'search', 'hello']);
1683
+ expect(exitCode).toBe(0);
1684
+ expect(stdout).toContain('1 result');
1685
+ ```
1686
+
1687
+ See the [@inquirerer/test README](https://github.com/constructive-io/dev-utils/tree/main/packages/inquirerer-test#readme) for the full API, including `createTestFixture` (temp-dir + commands runner), key-sequence constants, and snapshot normalisers.
1688
+
1689
+ ## Developing
1690
+
1605
1691
  **Run the demos:**
1606
1692
 
1607
1693
  ```bash
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.8.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": "d0e1a1b963e849c3e65a83e1cd7a183d9b5928a0"
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;