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