inquirerer 4.0.0 → 4.1.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
@@ -50,6 +50,7 @@ npm install inquirerer
50
50
  - [Checkbox Question](#checkbox-question)
51
51
  - [Advanced Question Options](#advanced-question-options)
52
52
  - [Positional Arguments](#positional-arguments)
53
+ - [Alias](#alias)
53
54
  - [Real-World Examples](#real-world-examples)
54
55
  - [Project Setup Wizard](#project-setup-wizard)
55
56
  - [Configuration Builder](#configuration-builder)
@@ -130,6 +131,7 @@ interface BaseQuestion {
130
131
  name: string; // Property name in result object
131
132
  type: string; // Question type
132
133
  _?: boolean; // Mark as positional argument (can be passed without --name flag)
134
+ alias?: string | string[]; // Short flag alias(es) for CLI (e.g., 'w' for --workspace)
133
135
  message?: string; // Prompt message to display
134
136
  description?: string; // Additional context
135
137
  default?: any; // Default value
@@ -481,6 +483,75 @@ const questions: Question[] = [
481
483
 
482
484
  In this example, `bar` gets its value from the named flag, so the two positional values go to `foo` and `baz`.
483
485
 
486
+ ### Alias
487
+
488
+ The `alias` property allows you to define short flag alternatives for question names, making CLI usage more convenient. Users can pass values using either the full name or any defined alias.
489
+
490
+ #### Basic Usage
491
+
492
+ ```typescript
493
+ const questions: Question[] = [
494
+ {
495
+ name: 'workspace',
496
+ type: 'confirm',
497
+ alias: 'w',
498
+ message: 'Create workspace?'
499
+ },
500
+ {
501
+ name: 'outputDir',
502
+ type: 'text',
503
+ alias: 'o',
504
+ message: 'Output directory',
505
+ default: './dist'
506
+ }
507
+ ];
508
+
509
+ const argv = minimist(process.argv.slice(2));
510
+ const result = await prompter.prompt(argv, questions);
511
+ ```
512
+
513
+ Now users can run either:
514
+ ```bash
515
+ node myprogram.js --workspace --outputDir ./build
516
+ # or equivalently:
517
+ node myprogram.js -w -o ./build
518
+ ```
519
+
520
+ #### Multiple Aliases
521
+
522
+ You can define multiple aliases for a single question using an array:
523
+
524
+ ```typescript
525
+ {
526
+ name: 'verbose',
527
+ type: 'confirm',
528
+ alias: ['v', 'V'], // Both -v and -V work
529
+ message: 'Enable verbose output?'
530
+ }
531
+ ```
532
+
533
+ #### Alias Priority
534
+
535
+ When both the main name and an alias are provided, the main name takes precedence:
536
+
537
+ ```typescript
538
+ // Running: node myprogram.js --workspace --w=false
539
+ // Results in: { workspace: true } (--workspace wins)
540
+ ```
541
+
542
+ When multiple aliases are provided in argv, the first alias in the definition order is used:
543
+
544
+ ```typescript
545
+ {
546
+ name: 'workspace',
547
+ type: 'text',
548
+ alias: ['w', 'ws']
549
+ }
550
+
551
+ // Running: node myprogram.js -w first --ws second
552
+ // Results in: { workspace: 'first' } (-w is checked first)
553
+ ```
554
+
484
555
  #### Positional with Options
485
556
 
486
557
  Positional arguments work with list, autocomplete, and checkbox questions. The value is mapped through the options:
package/esm/prompt.js CHANGED
@@ -84,8 +84,13 @@ class PromptContext {
84
84
  function generatePromptMessage(question, ctx) {
85
85
  const { message, name, type, default: def, options = [], description } = question;
86
86
  const lines = [];
87
- // 1. Main prompt label with --name inline
88
- let promptLine = whiteBright.bold(message || `${name}?`) + ' ' + dim(`(--${name})`);
87
+ // 1. Main prompt label with --name inline (and aliases if present)
88
+ const aliasInfo = question.alias
89
+ ? `, ${(Array.isArray(question.alias) ? question.alias : [question.alias])
90
+ .map(a => a.length === 1 ? `-${a}` : `--${a}`)
91
+ .join(', ')}`
92
+ : '';
93
+ let promptLine = whiteBright.bold(message || `${name}?`) + ' ' + dim(`(--${name}${aliasInfo})`);
89
94
  // 2. Append default inline (only if present)
90
95
  switch (type) {
91
96
  case 'confirm':
@@ -193,6 +198,11 @@ export class Inquirerer {
193
198
  opts.questions.forEach(question => {
194
199
  manPage += `${white(question.name.toUpperCase())}\n`;
195
200
  manPage += `\t${white('Type:')} ${gray(question.type)}\n`;
201
+ if (question.alias) {
202
+ const aliases = Array.isArray(question.alias) ? question.alias : [question.alias];
203
+ const aliasStr = aliases.map(a => a.length === 1 ? `-${a}` : `--${a}`).join(', ');
204
+ manPage += `\t${white('Alias:')} ${gray(aliasStr)}\n`;
205
+ }
196
206
  if (question.message) {
197
207
  manPage += `\t${white('Summary:')} ${gray(question.message)}\n`;
198
208
  }
@@ -300,6 +310,9 @@ export class Inquirerer {
300
310
  if (!shouldMutate && Array.isArray(argvAny._)) {
301
311
  obj._ = [...argvAny._];
302
312
  }
313
+ // Expand aliases before any other processing
314
+ // This allows users to use short flags like -w instead of --workspace
315
+ this.expandAliases(questions, obj);
303
316
  // Resolve dynamic defaults before processing questions
304
317
  await this.resolveDynamicDefaults(questions);
305
318
  // Resolve dynamic options before processing questions
@@ -435,6 +448,32 @@ export class Inquirerer {
435
448
  }
436
449
  }
437
450
  }
451
+ /**
452
+ * Expands aliases for all questions that have alias specified.
453
+ * If an alias key exists in obj but the main name doesn't, copies the value to the main name.
454
+ * This allows users to use short flags like -w instead of --workspace.
455
+ */
456
+ expandAliases(questions, obj) {
457
+ for (const question of questions) {
458
+ if ('alias' in question && question.alias) {
459
+ // Skip if the main name already has a value
460
+ if (question.name in obj) {
461
+ continue;
462
+ }
463
+ // Normalize alias to array
464
+ const aliases = Array.isArray(question.alias) ? question.alias : [question.alias];
465
+ // Check each alias and use the first one found
466
+ for (const alias of aliases) {
467
+ if (alias in obj) {
468
+ obj[question.name] = obj[alias];
469
+ // Optionally clean up the alias key to avoid confusion
470
+ delete obj[alias];
471
+ break;
472
+ }
473
+ }
474
+ }
475
+ }
476
+ }
438
477
  /**
439
478
  * Resolves optionsFrom values for all questions that have optionsFrom specified.
440
479
  * Updates the question.options property with the resolved array.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "4.0.0",
3
+ "version": "4.1.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",
@@ -41,5 +41,5 @@
41
41
  "clean-ansi": "0.1.6",
42
42
  "makage": "0.1.8"
43
43
  },
44
- "gitHead": "ab5945f162906da8fdf6776510ba30d82769b47f"
44
+ "gitHead": "bb739073064ebdfc5b6322cbf86a032f871b29a0"
45
45
  }
package/prompt.d.ts CHANGED
@@ -75,6 +75,12 @@ export declare class Inquirerer {
75
75
  * Sets the value directly in obj, bypassing the prompt entirely.
76
76
  */
77
77
  private resolveSetValues;
78
+ /**
79
+ * Expands aliases for all questions that have alias specified.
80
+ * If an alias key exists in obj but the main name doesn't, copies the value to the main name.
81
+ * This allows users to use short flags like -w instead of --workspace.
82
+ */
83
+ private expandAliases;
78
84
  /**
79
85
  * Resolves optionsFrom values for all questions that have optionsFrom specified.
80
86
  * Updates the question.options property with the resolved array.
package/prompt.js CHANGED
@@ -91,8 +91,13 @@ class PromptContext {
91
91
  function generatePromptMessage(question, ctx) {
92
92
  const { message, name, type, default: def, options = [], description } = question;
93
93
  const lines = [];
94
- // 1. Main prompt label with --name inline
95
- let promptLine = yanse_1.whiteBright.bold(message || `${name}?`) + ' ' + (0, yanse_1.dim)(`(--${name})`);
94
+ // 1. Main prompt label with --name inline (and aliases if present)
95
+ const aliasInfo = question.alias
96
+ ? `, ${(Array.isArray(question.alias) ? question.alias : [question.alias])
97
+ .map(a => a.length === 1 ? `-${a}` : `--${a}`)
98
+ .join(', ')}`
99
+ : '';
100
+ let promptLine = yanse_1.whiteBright.bold(message || `${name}?`) + ' ' + (0, yanse_1.dim)(`(--${name}${aliasInfo})`);
96
101
  // 2. Append default inline (only if present)
97
102
  switch (type) {
98
103
  case 'confirm':
@@ -200,6 +205,11 @@ class Inquirerer {
200
205
  opts.questions.forEach(question => {
201
206
  manPage += `${(0, yanse_1.white)(question.name.toUpperCase())}\n`;
202
207
  manPage += `\t${(0, yanse_1.white)('Type:')} ${(0, yanse_1.gray)(question.type)}\n`;
208
+ if (question.alias) {
209
+ const aliases = Array.isArray(question.alias) ? question.alias : [question.alias];
210
+ const aliasStr = aliases.map(a => a.length === 1 ? `-${a}` : `--${a}`).join(', ');
211
+ manPage += `\t${(0, yanse_1.white)('Alias:')} ${(0, yanse_1.gray)(aliasStr)}\n`;
212
+ }
203
213
  if (question.message) {
204
214
  manPage += `\t${(0, yanse_1.white)('Summary:')} ${(0, yanse_1.gray)(question.message)}\n`;
205
215
  }
@@ -307,6 +317,9 @@ class Inquirerer {
307
317
  if (!shouldMutate && Array.isArray(argvAny._)) {
308
318
  obj._ = [...argvAny._];
309
319
  }
320
+ // Expand aliases before any other processing
321
+ // This allows users to use short flags like -w instead of --workspace
322
+ this.expandAliases(questions, obj);
310
323
  // Resolve dynamic defaults before processing questions
311
324
  await this.resolveDynamicDefaults(questions);
312
325
  // Resolve dynamic options before processing questions
@@ -442,6 +455,32 @@ class Inquirerer {
442
455
  }
443
456
  }
444
457
  }
458
+ /**
459
+ * Expands aliases for all questions that have alias specified.
460
+ * If an alias key exists in obj but the main name doesn't, copies the value to the main name.
461
+ * This allows users to use short flags like -w instead of --workspace.
462
+ */
463
+ expandAliases(questions, obj) {
464
+ for (const question of questions) {
465
+ if ('alias' in question && question.alias) {
466
+ // Skip if the main name already has a value
467
+ if (question.name in obj) {
468
+ continue;
469
+ }
470
+ // Normalize alias to array
471
+ const aliases = Array.isArray(question.alias) ? question.alias : [question.alias];
472
+ // Check each alias and use the first one found
473
+ for (const alias of aliases) {
474
+ if (alias in obj) {
475
+ obj[question.name] = obj[alias];
476
+ // Optionally clean up the alias key to avoid confusion
477
+ delete obj[alias];
478
+ break;
479
+ }
480
+ }
481
+ }
482
+ }
483
+ }
445
484
  /**
446
485
  * Resolves optionsFrom values for all questions that have optionsFrom specified.
447
486
  * Updates the question.options property with the resolved array.
@@ -16,6 +16,7 @@ export interface BaseQuestion {
16
16
  name: string;
17
17
  type: string;
18
18
  _?: boolean;
19
+ alias?: string | string[];
19
20
  default?: any;
20
21
  defaultFrom?: string;
21
22
  setFrom?: string;