inquirerer 3.0.0 → 4.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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # inquirerer
2
2
 
3
3
  <p align="center" width="100%">
4
- <img height="90" src="https://raw.githubusercontent.com/constructive-io/dev-utils/refs/heads/main/docs/img/genomic.svg" />
4
+ <img height="90" src="https://raw.githubusercontent.com/constructive-io/dev-utils/refs/heads/main/docs/img/inquirerer.svg" />
5
5
  </p>
6
6
 
7
7
  <p align="center" width="100%">
@@ -11,36 +11,10 @@
11
11
  <a href="https://github.com/constructive-io/dev-utils/blob/main/LICENSE">
12
12
  <img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
13
13
  </a>
14
- <a href="https://www.npmjs.com/package/inquirerer"><img height="20" src="https://img.shields.io/npm/dt/inquirerer"></a>
15
14
  <a href="https://www.npmjs.com/package/inquirerer"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/dev-utils?filename=packages%2Finquirerer%2Fpackage.json"></a>
16
15
  </p>
17
16
 
18
- > **Note:** This package has been renamed to [`genomic`](https://www.npmjs.com/package/genomic). Please migrate to `genomic` for the latest features and updates.
19
-
20
- A powerful, TypeScript-first library for building beautiful command-line interfaces. Create interactive CLI tools with ease using intuitive prompts, validation, and rich user experiences.
21
-
22
- ## Migration to genomic
23
-
24
- This package is now maintained as `genomic`. To migrate:
25
-
26
- ```bash
27
- npm uninstall inquirerer
28
- npm install genomic
29
- ```
30
-
31
- Then update your imports:
32
-
33
- ```typescript
34
- // Before
35
- import { Inquirerer, InquirererOptions } from 'inquirerer';
36
-
37
- // After
38
- import { Prompter, PrompterOptions } from 'genomic';
39
- ```
40
-
41
- The API is the same, only the class names have changed:
42
- - `Inquirerer` -> `Prompter`
43
- - `InquirererOptions` -> `PrompterOptions`
17
+ A powerful, TypeScript-first library for building beautiful command-line interfaces.Create interactive CLI tools with ease using intuitive prompts, validation, and rich user experiences.
44
18
 
45
19
  ## Installation
46
20
 
@@ -50,22 +24,53 @@ npm install inquirerer
50
24
 
51
25
  ## Features
52
26
 
53
- - **CLI Builder** - Build command-line utilities fast
54
- - **Multiple Question Types** - Support for text, autocomplete, checkbox, and confirm questions
55
- - **Non-Interactive Mode** - Fallback to defaults for CI/CD environments, great for testing
56
- - **Smart Validation** - Built-in pattern matching, custom validators, and sanitizers
57
- - **Conditional Logic** - Show/hide questions based on previous answers
58
- - **Interactive UX** - Fuzzy search, keyboard navigation, and visual feedback
59
- - **Dynamic Defaults** - Auto-populate defaults from git config, date/time, or custom resolvers
27
+ - 🔌 **CLI Builder** - Build command-line utilties fast
28
+ - 🖊 **Multiple Question Types** - Support for text, autocomplete, checkbox, and confirm questions
29
+ - 🤖 **Non-Interactive Mode** - Fallback to defaults for CI/CD environments, great for testing
30
+ - ✅ **Smart Validation** - Built-in pattern matching, custom validators, and sanitizers
31
+ - 🔀 **Conditional Logic** - Show/hide questions based on previous answers
32
+ - 🎨 **Interactive UX** - Fuzzy search, keyboard navigation, and visual feedback
33
+ - 🔄 **Dynamic Defaults** - Auto-populate defaults from git config, date/time, or custom resolvers
34
+
35
+ ## Table of Contents
36
+
37
+ - [Quick Start](#quick-start)
38
+ - [Core Concepts](#core-concepts)
39
+ - [TypeScript Support](#typescript-support)
40
+ - [Question Types](#question-types)
41
+ - [Non-Interactive Mode](#non-interactive-mode)
42
+ - [API Reference](#api-reference)
43
+ - [Inquirerer Class](#inquirerer-class)
44
+ - [Question Types](#question-types-1)
45
+ - [Text Question](#text-question)
46
+ - [Number Question](#number-question)
47
+ - [Confirm Question](#confirm-question)
48
+ - [List Question](#list-question)
49
+ - [Autocomplete Question](#autocomplete-question)
50
+ - [Checkbox Question](#checkbox-question)
51
+ - [Advanced Question Options](#advanced-question-options)
52
+ - [Positional Arguments](#positional-arguments)
53
+ - [Real-World Examples](#real-world-examples)
54
+ - [Project Setup Wizard](#project-setup-wizard)
55
+ - [Configuration Builder](#configuration-builder)
56
+ - [CLI with Commander Integration](#cli-with-commander-integration)
57
+ - [Dynamic Dependencies](#dynamic-dependencies)
58
+ - [Custom Validation](#custom-validation)
59
+ - [Dynamic Defaults with Resolvers](#dynamic-defaults-with-resolvers)
60
+ - [Built-in Resolvers](#built-in-resolvers)
61
+ - [Custom Resolvers](#custom-resolvers)
62
+ - [Resolver Examples](#resolver-examples)
63
+ - [CLI Helper](#cli-helper)
64
+ - [Developing](#developing)
60
65
 
61
66
  ## Quick Start
62
67
 
63
68
  ```typescript
64
69
  import { Inquirerer } from 'inquirerer';
65
70
 
66
- const inquirerer = new Inquirerer();
71
+ const prompter = new Inquirerer();
67
72
 
68
- const answers = await inquirerer.prompt({}, [
73
+ const answers = await prompter.prompt({}, [
69
74
  {
70
75
  type: 'text',
71
76
  name: 'username',
@@ -112,7 +117,7 @@ interface UserConfig {
112
117
  newsletter: boolean;
113
118
  }
114
119
 
115
- const answers = await inquirerer.prompt<UserConfig>({}, questions);
120
+ const answers = await prompter.prompt<UserConfig>({}, questions);
116
121
  // answers is typed as UserConfig
117
122
  ```
118
123
 
@@ -145,7 +150,7 @@ interface BaseQuestion {
145
150
  When running in CI/CD or without a TTY, inquirerer automatically falls back to default values:
146
151
 
147
152
  ```typescript
148
- const inquirerer = new Inquirerer({
153
+ const prompter = new Inquirerer({
149
154
  noTty: true, // Force non-interactive mode
150
155
  useDefaults: true // Use defaults without prompting
151
156
  });
@@ -168,7 +173,7 @@ interface InquirererOptions {
168
173
  resolverRegistry?: DefaultResolverRegistry; // Custom resolver registry
169
174
  }
170
175
 
171
- const inquirerer = new Inquirerer(options);
176
+ const prompter = new Inquirerer(options);
172
177
  ```
173
178
 
174
179
  #### Methods
@@ -185,13 +190,987 @@ close(): void
185
190
  exit(): void
186
191
  ```
187
192
 
188
- ## Full Documentation
193
+ #### Managing Multiple Instances
194
+
195
+ When working with multiple `Prompter` instances that share the same input stream (typically `process.stdin`), only one instance should be actively prompting at a time. Each instance attaches its own keyboard listener, so having multiple active instances will cause duplicate or unexpected keypress behavior.
196
+
197
+ **Best practices:**
198
+
199
+ 1. **Reuse a single instance** - Create one `Prompter` instance and reuse it for all prompts:
200
+ ```typescript
201
+ const prompter = new Inquirerer();
202
+
203
+ // Use the same instance for multiple prompt sessions
204
+ const answers1 = await prompter.prompt({}, questions1);
205
+ const answers2 = await prompter.prompt({}, questions2);
206
+
207
+ prompter.close(); // Clean up when done
208
+ ```
209
+
210
+ 2. **Close before creating another** - If you need separate instances, close the first before using the second:
211
+ ```typescript
212
+ const prompter1 = new Prompter();
213
+ const answers1 = await prompter1.prompt({}, questions1);
214
+ prompter1.close(); // Important: close before creating another
215
+
216
+ const prompter2 = new Prompter();
217
+ const answers2 = await prompter2.prompt({}, questions2);
218
+ prompter2.close();
219
+ ```
220
+
221
+ ### Question Types
222
+
223
+ #### Text Question
224
+
225
+ Collect string input from users.
226
+
227
+ ```typescript
228
+ {
229
+ type: 'text',
230
+ name: 'projectName',
231
+ message: 'What is your project name?',
232
+ default: 'my-app',
233
+ required: true,
234
+ pattern: '^[a-z0-9-]+$', // Regex validation
235
+ validate: (input) => {
236
+ if (input.length < 3) {
237
+ return { success: false, reason: 'Name must be at least 3 characters' };
238
+ }
239
+ return true;
240
+ }
241
+ }
242
+ ```
243
+
244
+ #### Number Question
245
+
246
+ Collect numeric input.
247
+
248
+ ```typescript
249
+ {
250
+ type: 'number',
251
+ name: 'port',
252
+ message: 'Which port to use?',
253
+ default: 3000,
254
+ validate: (input) => {
255
+ if (input < 1 || input > 65535) {
256
+ return { success: false, reason: 'Port must be between 1 and 65535' };
257
+ }
258
+ return true;
259
+ }
260
+ }
261
+ ```
262
+
263
+ #### Confirm Question
264
+
265
+ Yes/no questions.
266
+
267
+ ```typescript
268
+ {
269
+ type: 'confirm',
270
+ name: 'useTypeScript',
271
+ message: 'Use TypeScript?',
272
+ default: true // Default to 'yes'
273
+ }
274
+ ```
275
+
276
+ #### List Question
277
+
278
+ Select one option from a list (no search).
279
+
280
+ ```typescript
281
+ {
282
+ type: 'list',
283
+ name: 'license',
284
+ message: 'Choose a license',
285
+ options: ['MIT', 'Apache-2.0', 'GPL-3.0', 'BSD-3-Clause'],
286
+ default: 'MIT',
287
+ maxDisplayLines: 5
288
+ }
289
+ ```
290
+
291
+ #### Autocomplete Question
292
+
293
+ Select with fuzzy search capabilities.
294
+
295
+ ```typescript
296
+ {
297
+ type: 'autocomplete',
298
+ name: 'framework',
299
+ message: 'Choose your framework',
300
+ options: [
301
+ { name: 'React', value: 'react' },
302
+ { name: 'Vue.js', value: 'vue' },
303
+ { name: 'Angular', value: 'angular' },
304
+ { name: 'Svelte', value: 'svelte' }
305
+ ],
306
+ allowCustomOptions: true, // Allow user to enter custom value
307
+ maxDisplayLines: 8
308
+ }
309
+ ```
310
+
311
+ #### Checkbox Question
312
+
313
+ Multi-select with search.
314
+
315
+ ```typescript
316
+ {
317
+ type: 'checkbox',
318
+ name: 'features',
319
+ message: 'Select features to include',
320
+ options: [
321
+ 'Authentication',
322
+ 'Database',
323
+ 'API Routes',
324
+ 'Testing',
325
+ 'Documentation'
326
+ ],
327
+ default: ['Authentication', 'API Routes'],
328
+ returnFullResults: false, // Only return selected items
329
+ required: true
330
+ }
331
+ ```
332
+
333
+ With `returnFullResults: true`, returns all options with selection status:
334
+
335
+ ```typescript
336
+ [
337
+ { name: 'Authentication', value: 'Authentication', selected: true },
338
+ { name: 'Database', value: 'Database', selected: false },
339
+ // ...
340
+ ]
341
+ ```
189
342
 
190
- For complete documentation, examples, and advanced usage, please see the [`genomic` package documentation](https://www.npmjs.com/package/genomic).
343
+ ### Advanced Question Options
344
+
345
+ #### Custom Validation
346
+
347
+ ```typescript
348
+ {
349
+ type: 'text',
350
+ name: 'email',
351
+ message: 'Enter your email',
352
+ pattern: '^[^@]+@[^@]+\\.[^@]+$',
353
+ validate: (email, answers) => {
354
+ // Custom async validation possible
355
+ if (email.endsWith('@example.com')) {
356
+ return {
357
+ success: false,
358
+ reason: 'Please use a real email address'
359
+ };
360
+ }
361
+ return { success: true };
362
+ }
363
+ }
364
+ ```
365
+
366
+ #### Value Sanitization
367
+
368
+ ```typescript
369
+ {
370
+ type: 'text',
371
+ name: 'tags',
372
+ message: 'Enter tags (comma-separated)',
373
+ sanitize: (input) => {
374
+ return input.split(',').map(tag => tag.trim());
375
+ }
376
+ }
377
+ ```
191
378
 
192
- ## License
379
+ #### Conditional Questions
193
380
 
194
- MIT
381
+ ```typescript
382
+ const questions: Question[] = [
383
+ {
384
+ type: 'confirm',
385
+ name: 'useDatabase',
386
+ message: 'Do you need a database?',
387
+ default: false
388
+ },
389
+ {
390
+ type: 'list',
391
+ name: 'database',
392
+ message: 'Which database?',
393
+ options: ['PostgreSQL', 'MySQL', 'MongoDB', 'SQLite'],
394
+ when: (answers) => answers.useDatabase === true // Only show if useDatabase is true
395
+ }
396
+ ];
397
+ ```
398
+
399
+ #### Question Dependencies
400
+
401
+ Ensure questions appear in the correct order:
402
+
403
+ ```typescript
404
+ [
405
+ {
406
+ type: 'checkbox',
407
+ name: 'services',
408
+ message: 'Select services',
409
+ options: ['Auth', 'Storage', 'Functions']
410
+ },
411
+ {
412
+ type: 'text',
413
+ name: 'authProvider',
414
+ message: 'Which auth provider?',
415
+ dependsOn: ['services'], // Wait for services question
416
+ when: (answers) => {
417
+ const selected = answers.services.find(s => s.name === 'Auth');
418
+ return selected?.selected === true;
419
+ }
420
+ }
421
+ ]
422
+ ```
423
+
424
+ ### Positional Arguments
425
+
426
+ The `_` property allows you to name positional parameters, enabling users to pass values without flags. This is useful for CLI tools where the first few arguments have obvious meanings.
427
+
428
+ #### Basic Usage
429
+
430
+ ```typescript
431
+ const questions: Question[] = [
432
+ {
433
+ _: true,
434
+ name: 'database',
435
+ type: 'text',
436
+ message: 'Database name',
437
+ required: true
438
+ }
439
+ ];
440
+
441
+ const argv = minimist(process.argv.slice(2));
442
+ const result = await prompter.prompt(argv, questions);
443
+ ```
444
+
445
+ Now users can run either:
446
+ ```bash
447
+ node myprogram.js mydb1
448
+ # or equivalently:
449
+ node myprogram.js --database mydb1
450
+ ```
451
+
452
+ #### Multiple Positional Arguments
453
+
454
+ Positional arguments are assigned in declaration order:
455
+
456
+ ```typescript
457
+ const questions: Question[] = [
458
+ { _: true, name: 'source', type: 'text', message: 'Source file' },
459
+ { name: 'verbose', type: 'confirm', default: false },
460
+ { _: true, name: 'destination', type: 'text', message: 'Destination file' }
461
+ ];
462
+
463
+ // Running: node copy.js input.txt output.txt --verbose
464
+ // Results in: { source: 'input.txt', destination: 'output.txt', verbose: true }
465
+ ```
466
+
467
+ #### Named Arguments Take Precedence
468
+
469
+ When both positional and named arguments are provided, named arguments win and the positional slot is preserved for the next positional question:
470
+
471
+ ```typescript
472
+ const questions: Question[] = [
473
+ { _: true, name: 'foo', type: 'text' },
474
+ { _: true, name: 'bar', type: 'text' },
475
+ { _: true, name: 'baz', type: 'text' }
476
+ ];
477
+
478
+ // Running: node myprogram.js pos1 pos2 --bar named-bar
479
+ // Results in: { foo: 'pos1', bar: 'named-bar', baz: 'pos2' }
480
+ ```
481
+
482
+ In this example, `bar` gets its value from the named flag, so the two positional values go to `foo` and `baz`.
483
+
484
+ #### Positional with Options
485
+
486
+ Positional arguments work with list, autocomplete, and checkbox questions. The value is mapped through the options:
487
+
488
+ ```typescript
489
+ const questions: Question[] = [
490
+ {
491
+ _: true,
492
+ name: 'framework',
493
+ type: 'list',
494
+ options: [
495
+ { name: 'React', value: 'react' },
496
+ { name: 'Vue', value: 'vue' }
497
+ ]
498
+ }
499
+ ];
500
+
501
+ // Running: node setup.js React
502
+ // Results in: { framework: 'react' }
503
+ ```
504
+
505
+ ## Real-World Examples
506
+
507
+ ### Project Setup Wizard
508
+
509
+ ```typescript
510
+ import { Inquirerer, Question } from 'inquirerer';
511
+ import minimist from 'minimist';
512
+
513
+ const argv = minimist(process.argv.slice(2));
514
+ const prompter = new Inquirerer();
515
+
516
+ const questions: Question[] = [
517
+ {
518
+ type: 'text',
519
+ name: 'projectName',
520
+ message: 'Project name',
521
+ required: true,
522
+ pattern: '^[a-z0-9-]+$'
523
+ },
524
+ {
525
+ type: 'text',
526
+ name: 'description',
527
+ message: 'Project description',
528
+ default: 'My awesome project'
529
+ },
530
+ {
531
+ type: 'confirm',
532
+ name: 'typescript',
533
+ message: 'Use TypeScript?',
534
+ default: true
535
+ },
536
+ {
537
+ type: 'autocomplete',
538
+ name: 'framework',
539
+ message: 'Choose a framework',
540
+ options: ['React', 'Vue', 'Svelte', 'None'],
541
+ default: 'React'
542
+ },
543
+ {
544
+ type: 'checkbox',
545
+ name: 'tools',
546
+ message: 'Additional tools',
547
+ options: ['ESLint', 'Prettier', 'Jest', 'Husky'],
548
+ default: ['ESLint', 'Prettier']
549
+ }
550
+ ];
551
+
552
+ const config = await prompter.prompt(argv, questions);
553
+ console.log('Creating project with:', config);
554
+ ```
555
+
556
+ Run interactively:
557
+ ```bash
558
+ node setup.js
559
+ ```
560
+
561
+ Or with CLI args:
562
+ ```bash
563
+ node setup.js --projectName=my-app --typescript --framework=React
564
+ ```
565
+
566
+ ### Configuration Builder
567
+
568
+ ```typescript
569
+ interface AppConfig {
570
+ port: number;
571
+ host: string;
572
+ ssl: boolean;
573
+ sslCert?: string;
574
+ sslKey?: string;
575
+ database: string;
576
+ logLevel: string;
577
+ }
578
+
579
+ const questions: Question[] = [
580
+ {
581
+ type: 'number',
582
+ name: 'port',
583
+ message: 'Server port',
584
+ default: 3000,
585
+ validate: (port) => port > 0 && port < 65536
586
+ },
587
+ {
588
+ type: 'text',
589
+ name: 'host',
590
+ message: 'Server host',
591
+ default: '0.0.0.0'
592
+ },
593
+ {
594
+ type: 'confirm',
595
+ name: 'ssl',
596
+ message: 'Enable SSL?',
597
+ default: false
598
+ },
599
+ {
600
+ type: 'text',
601
+ name: 'sslCert',
602
+ message: 'SSL certificate path',
603
+ when: (answers) => answers.ssl === true,
604
+ required: true
605
+ },
606
+ {
607
+ type: 'text',
608
+ name: 'sslKey',
609
+ message: 'SSL key path',
610
+ when: (answers) => answers.ssl === true,
611
+ required: true
612
+ },
613
+ {
614
+ type: 'list',
615
+ name: 'database',
616
+ message: 'Database type',
617
+ options: ['PostgreSQL', 'MySQL', 'SQLite'],
618
+ default: 'PostgreSQL'
619
+ },
620
+ {
621
+ type: 'list',
622
+ name: 'logLevel',
623
+ message: 'Log level',
624
+ options: ['error', 'warn', 'info', 'debug'],
625
+ default: 'info'
626
+ }
627
+ ];
628
+
629
+ const config = await prompter.prompt<AppConfig>(argv, questions);
630
+
631
+ // Write config to file
632
+ fs.writeFileSync('config.json', JSON.stringify(config, null, 2));
633
+ ```
634
+
635
+ ### CLI with Commander Integration
636
+
637
+ ```typescript
638
+ import { CLI, CommandHandler } from 'inquirerer';
639
+ import { Question } from 'inquirerer';
640
+
641
+ const handler: CommandHandler = async (argv, prompter, options) => {
642
+ const questions: Question[] = [
643
+ {
644
+ type: 'text',
645
+ name: 'name',
646
+ message: 'What is your name?',
647
+ required: true
648
+ },
649
+ {
650
+ type: 'number',
651
+ name: 'age',
652
+ message: 'What is your age?',
653
+ validate: (age) => age >= 0 && age <= 120
654
+ }
655
+ ];
656
+
657
+ const answers = await prompter.prompt(argv, questions);
658
+ console.log('Hello,', answers.name);
659
+ };
660
+
661
+ const cli = new CLI(handler, {
662
+ version: 'myapp@1.0.0',
663
+ minimistOpts: {
664
+ alias: {
665
+ n: 'name',
666
+ a: 'age',
667
+ v: 'version'
668
+ }
669
+ }
670
+ });
671
+
672
+ await cli.run();
673
+ ```
674
+
675
+ ### Dynamic Dependencies
676
+
677
+ ```typescript
678
+ const questions: Question[] = [
679
+ {
680
+ type: 'checkbox',
681
+ name: 'cloud',
682
+ message: 'Select cloud services',
683
+ options: ['AWS', 'Azure', 'GCP'],
684
+ returnFullResults: true
685
+ },
686
+ {
687
+ type: 'text',
688
+ name: 'awsRegion',
689
+ message: 'AWS Region',
690
+ dependsOn: ['cloud'],
691
+ when: (answers) => {
692
+ const aws = answers.cloud?.find(c => c.name === 'AWS');
693
+ return aws?.selected === true;
694
+ },
695
+ default: 'us-east-1'
696
+ },
697
+ {
698
+ type: 'text',
699
+ name: 'azureLocation',
700
+ message: 'Azure Location',
701
+ dependsOn: ['cloud'],
702
+ when: (answers) => {
703
+ const azure = answers.cloud?.find(c => c.name === 'Azure');
704
+ return azure?.selected === true;
705
+ },
706
+ default: 'eastus'
707
+ },
708
+ {
709
+ type: 'text',
710
+ name: 'gcpZone',
711
+ message: 'GCP Zone',
712
+ dependsOn: ['cloud'],
713
+ when: (answers) => {
714
+ const gcp = answers.cloud?.find(c => c.name === 'GCP');
715
+ return gcp?.selected === true;
716
+ },
717
+ default: 'us-central1-a'
718
+ }
719
+ ];
720
+
721
+ const config = await prompter.prompt({}, questions);
722
+ ```
723
+
724
+ ### Custom Validation
725
+
726
+ ```typescript
727
+ const questions: Question[] = [
728
+ {
729
+ type: 'text',
730
+ name: 'username',
731
+ message: 'Choose a username',
732
+ required: true,
733
+ pattern: '^[a-zA-Z0-9_]{3,20}$',
734
+ validate: async (username) => {
735
+ // Simulate API call to check availability
736
+ const available = await checkUsernameAvailability(username);
737
+ if (!available) {
738
+ return {
739
+ success: false,
740
+ reason: 'Username is already taken'
741
+ };
742
+ }
743
+ return { success: true };
744
+ }
745
+ },
746
+ {
747
+ type: 'text',
748
+ name: 'password',
749
+ message: 'Choose a password',
750
+ required: true,
751
+ validate: (password) => {
752
+ if (password.length < 8) {
753
+ return {
754
+ success: false,
755
+ reason: 'Password must be at least 8 characters'
756
+ };
757
+ }
758
+ if (!/[A-Z]/.test(password)) {
759
+ return {
760
+ success: false,
761
+ reason: 'Password must contain an uppercase letter'
762
+ };
763
+ }
764
+ if (!/[0-9]/.test(password)) {
765
+ return {
766
+ success: false,
767
+ reason: 'Password must contain a number'
768
+ };
769
+ }
770
+ return { success: true };
771
+ }
772
+ },
773
+ {
774
+ type: 'text',
775
+ name: 'confirmPassword',
776
+ message: 'Confirm password',
777
+ required: true,
778
+ dependsOn: ['password'],
779
+ validate: (confirm, answers) => {
780
+ if (confirm !== answers.password) {
781
+ return {
782
+ success: false,
783
+ reason: 'Passwords do not match'
784
+ };
785
+ }
786
+ return { success: true };
787
+ }
788
+ }
789
+ ];
790
+ ```
791
+
792
+ ## Dynamic Defaults with Resolvers
793
+
794
+ The `defaultFrom` feature allows you to automatically populate question defaults from dynamic sources like git configuration, environment variables, date/time values, or custom resolvers. This eliminates repetitive boilerplate code for common default values.
795
+
796
+ ### Quick Example
797
+
798
+ ```typescript
799
+ import { Inquirerer } from 'inquirerer';
800
+
801
+ const questions = [
802
+ {
803
+ type: 'text',
804
+ name: 'authorName',
805
+ message: 'Author name?',
806
+ defaultFrom: 'git.user.name' // Auto-fills from git config
807
+ },
808
+ {
809
+ type: 'text',
810
+ name: 'authorEmail',
811
+ message: 'Author email?',
812
+ defaultFrom: 'git.user.email' // Auto-fills from git config
813
+ },
814
+ {
815
+ type: 'text',
816
+ name: 'npmUser',
817
+ message: 'NPM username?',
818
+ defaultFrom: 'npm.whoami' // Auto-fills from npm whoami
819
+ },
820
+ {
821
+ type: 'text',
822
+ name: 'copyrightYear',
823
+ message: 'Copyright year?',
824
+ defaultFrom: 'date.year' // Auto-fills current year
825
+ }
826
+ ];
827
+
828
+ const prompter = new Inquirerer();
829
+ const answers = await prompter.prompt({}, questions);
830
+ ```
831
+
832
+ ### Built-in Resolvers
833
+
834
+ Prompter comes with several built-in resolvers ready to use:
835
+
836
+ #### Git Configuration
837
+
838
+ | Resolver | Description | Example Output |
839
+ |----------|-------------|----------------|
840
+ | `git.user.name` | Git global user name | `"John Doe"` |
841
+ | `git.user.email` | Git global user email | `"john@example.com"` |
842
+
843
+ #### NPM
844
+
845
+ | Resolver | Description | Example Output |
846
+ |----------|-------------|----------------|
847
+ | `npm.whoami` | Currently logged in npm user | `"johndoe"` |
848
+
849
+ #### Date & Time
850
+
851
+ | Resolver | Description | Example Output |
852
+ |----------|-------------|----------------|
853
+ | `date.year` | Current year | `"2025"` |
854
+ | `date.month` | Current month (zero-padded) | `"11"` |
855
+ | `date.day` | Current day (zero-padded) | `"23"` |
856
+ | `date.iso` | ISO date (YYYY-MM-DD) | `"2025-11-23"` |
857
+ | `date.now` | ISO timestamp | `"2025-11-23T15:30:45.123Z"` |
858
+ | `date.timestamp` | Unix timestamp (ms) | `"1732375845123"` |
859
+
860
+ #### Workspace (nearest package.json)
861
+
862
+ | Resolver | Description | Example Output |
863
+ |----------|-------------|----------------|
864
+ | `workspace.name` | Repo slug from `repository` URL (fallback: `package.json` `name`) | `"dev-utils"` |
865
+ | `workspace.repo.name` | Repo name from `repository` URL | `"dev-utils"` |
866
+ | `workspace.repo.organization` | Repo org/owner from `repository` URL | `"constructive-io"` |
867
+ | `workspace.organization.name` | Alias for `workspace.repo.organization` | `"constructive-io"` |
868
+ | `workspace.license` | License field from `package.json` | `"MIT"` |
869
+ | `workspace.author` | Author name from `package.json` | `"Constructive"` |
870
+ | `workspace.author.name` | Author name from `package.json` | `"Constructive"` |
871
+ | `workspace.author.email` | Author email from `package.json` | `"email@example.org"` |
872
+
873
+ ### Priority Order
874
+
875
+ When resolving default values, inquirerer follows this priority:
876
+
877
+ 1. **CLI Arguments** - Values passed via command line (highest priority)
878
+ 2. **`setFrom`** - Auto-set values (bypasses prompt entirely)
879
+ 3. **`defaultFrom`** - Dynamically resolved default values
880
+ 4. **`default`** - Static default values
881
+ 5. **`undefined`** - No default available
882
+
883
+ ```typescript
884
+ {
885
+ type: 'text',
886
+ name: 'author',
887
+ defaultFrom: 'git.user.name', // Try git first
888
+ default: 'Anonymous' // Fallback if git not configured
889
+ }
890
+ ```
891
+
892
+ ### `setFrom` vs `defaultFrom`
893
+
894
+ Both `setFrom` and `defaultFrom` use resolvers to get values, but they behave differently:
895
+
896
+ | Feature | `defaultFrom` | `setFrom` |
897
+ |---------|---------------|-----------|
898
+ | Sets value as | Default (user can override) | Final value (no prompt) |
899
+ | User prompted? | Yes, with pre-filled default | No, question is skipped |
900
+ | Use case | Suggested values | Auto-computed values |
901
+
902
+ **`defaultFrom`** - The resolved value becomes the default, but the user is still prompted and can change it:
903
+
904
+ ```typescript
905
+ {
906
+ type: 'text',
907
+ name: 'authorName',
908
+ message: 'Author name?',
909
+ defaultFrom: 'git.user.name' // User sees "Author name? [John Doe]" and can change it
910
+ }
911
+ ```
912
+
913
+ **`setFrom`** - The resolved value is set directly and the question is skipped entirely:
914
+
915
+ ```typescript
916
+ {
917
+ type: 'text',
918
+ name: 'year',
919
+ message: 'Copyright year?',
920
+ setFrom: 'date.year' // Automatically set to "2025", no prompt shown
921
+ }
922
+ ```
923
+
924
+ #### When to use each
925
+
926
+ Use `defaultFrom` when:
927
+ - The value is a suggestion the user might want to change
928
+ - User confirmation is desired
929
+
930
+ Use `setFrom` when:
931
+ - The value should be computed automatically
932
+ - No user input is needed (e.g., timestamps, computed fields)
933
+ - You want to reduce the number of prompts
934
+
935
+ #### Combined example
936
+
937
+ ```typescript
938
+ const questions = [
939
+ {
940
+ type: 'text',
941
+ name: 'authorName',
942
+ message: 'Author name?',
943
+ defaultFrom: 'git.user.name' // User can override
944
+ },
945
+ {
946
+ type: 'text',
947
+ name: 'createdAt',
948
+ setFrom: 'date.iso' // Auto-set, no prompt
949
+ },
950
+ {
951
+ type: 'text',
952
+ name: 'copyrightYear',
953
+ setFrom: 'date.year' // Auto-set, no prompt
954
+ }
955
+ ];
956
+
957
+ // User only sees prompt for authorName
958
+ // createdAt and copyrightYear are set automatically
959
+ ```
960
+
961
+ ### Custom Resolvers
962
+
963
+ Register your own custom resolvers for project-specific needs:
964
+
965
+ ```typescript
966
+ import { registerDefaultResolver } from 'inquirerer';
967
+
968
+ // Register a resolver for current directory name
969
+ registerDefaultResolver('cwd.name', () => {
970
+ return process.cwd().split('/').pop();
971
+ });
972
+
973
+ // Register a resolver for environment variable
974
+ registerDefaultResolver('env.user', () => {
975
+ return process.env.USER;
976
+ });
977
+
978
+ // Use in questions
979
+ const questions = [
980
+ {
981
+ type: 'text',
982
+ name: 'projectName',
983
+ message: 'Project name?',
984
+ defaultFrom: 'cwd.name',
985
+ default: 'my-project'
986
+ },
987
+ {
988
+ type: 'text',
989
+ name: 'author',
990
+ message: 'Author?',
991
+ defaultFrom: 'env.user'
992
+ }
993
+ ];
994
+ ```
995
+
996
+ ### Instance-Specific Resolvers
997
+
998
+ For isolated resolver registries, use a custom resolver registry per Prompter instance:
999
+
1000
+ ```typescript
1001
+ import { DefaultResolverRegistry, Prompter } from 'inquirerer';
1002
+
1003
+ const customRegistry = new DefaultResolverRegistry();
1004
+
1005
+ // Register resolvers specific to this instance
1006
+ customRegistry.register('app.name', () => 'my-app');
1007
+ customRegistry.register('app.port', () => 3000);
1008
+
1009
+ const prompter = new Inquirerer({
1010
+ resolverRegistry: customRegistry // Use custom registry
1011
+ });
1012
+
1013
+ const questions = [
1014
+ {
1015
+ type: 'text',
1016
+ name: 'appName',
1017
+ defaultFrom: 'app.name'
1018
+ },
1019
+ {
1020
+ type: 'number',
1021
+ name: 'port',
1022
+ defaultFrom: 'app.port'
1023
+ }
1024
+ ];
1025
+
1026
+ const answers = await prompter.prompt({}, questions);
1027
+ ```
1028
+
1029
+ ### Resolver Examples
1030
+
1031
+ #### System Information
1032
+
1033
+ ```typescript
1034
+ import os from 'os';
1035
+ import { registerDefaultResolver } from 'inquirerer';
1036
+
1037
+ registerDefaultResolver('system.hostname', () => os.hostname());
1038
+ registerDefaultResolver('system.username', () => os.userInfo().username);
1039
+
1040
+ const questions = [
1041
+ {
1042
+ type: 'text',
1043
+ name: 'hostname',
1044
+ message: 'Hostname?',
1045
+ defaultFrom: 'system.hostname'
1046
+ }
1047
+ ];
1048
+ ```
1049
+
1050
+ #### Conditional Defaults
1051
+
1052
+ ```typescript
1053
+ registerDefaultResolver('app.port', () => {
1054
+ return process.env.NODE_ENV === 'production' ? 80 : 3000;
1055
+ });
1056
+
1057
+ const questions = [
1058
+ {
1059
+ type: 'number',
1060
+ name: 'port',
1061
+ message: 'Port?',
1062
+ defaultFrom: 'app.port'
1063
+ }
1064
+ ];
1065
+ ```
1066
+
1067
+ ### Error Handling
1068
+
1069
+ Resolvers fail silently by default. If a resolver throws an error or returns `undefined`, inquirerer falls back to the static `default` value (if provided):
1070
+
1071
+ ```typescript
1072
+ {
1073
+ type: 'text',
1074
+ name: 'author',
1075
+ defaultFrom: 'git.user.name', // May fail if git not configured
1076
+ default: 'Anonymous', // Used if resolver fails
1077
+ required: true
1078
+ }
1079
+ ```
1080
+
1081
+ For debugging, set `DEBUG=inquirerer` to see resolver errors:
1082
+
1083
+ ```bash
1084
+ DEBUG=inquirerer node your-cli.js
1085
+ ```
1086
+
1087
+ ### Real-World Use Case
1088
+
1089
+ ```typescript
1090
+ import { Inquirerer, registerDefaultResolver } from 'inquirerer';
1091
+
1092
+ // Register a resolver for current directory name
1093
+ registerDefaultResolver('cwd.name', () => {
1094
+ return process.cwd().split('/').pop();
1095
+ });
1096
+
1097
+ const questions = [
1098
+ {
1099
+ type: 'text',
1100
+ name: 'projectName',
1101
+ message: 'Project name?',
1102
+ defaultFrom: 'cwd.name',
1103
+ required: true
1104
+ },
1105
+ {
1106
+ type: 'text',
1107
+ name: 'author',
1108
+ message: 'Author?',
1109
+ defaultFrom: 'git.user.name',
1110
+ required: true
1111
+ },
1112
+ {
1113
+ type: 'text',
1114
+ name: 'email',
1115
+ message: 'Email?',
1116
+ defaultFrom: 'git.user.email',
1117
+ required: true
1118
+ },
1119
+ {
1120
+ type: 'text',
1121
+ name: 'year',
1122
+ message: 'Copyright year?',
1123
+ defaultFrom: 'date.year'
1124
+ }
1125
+ ];
1126
+
1127
+ const prompter = new Inquirerer();
1128
+ const config = await prompter.prompt({}, questions);
1129
+ ```
1130
+
1131
+ With git configured, the prompts will show:
1132
+
1133
+ ```bash
1134
+ Project name? (my-project-dir)
1135
+ Author? (John Doe)
1136
+ Email? (john@example.com)
1137
+ Copyright year? (2025)
1138
+ ```
1139
+
1140
+ All defaults automatically populated from git config, directory name, and current date!
1141
+
1142
+ ## CLI Helper
1143
+
1144
+ The `CLI` class provides integration with command-line argument parsing:
1145
+
1146
+ ```typescript
1147
+ import { CLI, CommandHandler, CLIOptions } from 'inquirerer';
1148
+
1149
+ const options: Partial<CLIOptions> = {
1150
+ version: 'myapp@1.0.0',
1151
+ minimistOpts: {
1152
+ alias: {
1153
+ v: 'version',
1154
+ h: 'help'
1155
+ },
1156
+ boolean: ['help', 'version'],
1157
+ string: ['name', 'output']
1158
+ }
1159
+ };
1160
+
1161
+ const handler: CommandHandler = async (argv, prompter) => {
1162
+ if (argv.help) {
1163
+ console.log('Usage: myapp [options]');
1164
+ process.exit(0);
1165
+ }
1166
+
1167
+ const answers = await prompter.prompt(argv, questions);
1168
+ // Handle answers
1169
+ };
1170
+
1171
+ const cli = new CLI(handler, options);
1172
+ await cli.run();
1173
+ ```
195
1174
 
196
1175
  ---
197
1176