inquirerer 2.0.7 → 2.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
@@ -16,8 +16,6 @@
16
16
 
17
17
  This library is designed to facilitate the creation of command-line utilities by providing a robust framework for capturing user input through interactive prompts. It supports a variety of question types, making it highly flexible and suitable for a wide range of applications.
18
18
 
19
- _This is the new and improved version of [pyramation/inquirer](https://github.com/pyramation/inquirerer). Soon this will completely replace it!_
20
-
21
19
  ## Install
22
20
 
23
21
  ```sh
package/esm/prompt.js CHANGED
@@ -1,6 +1,34 @@
1
1
  import chalk from 'chalk';
2
2
  import readline from 'readline';
3
3
  import { KEY_CODES, TerminalKeypress } from './keypress';
4
+ export function reorderQuestionsByDeps(questions) {
5
+ const nameToIndex = new Map();
6
+ questions.forEach((q, idx) => nameToIndex.set(q.name, idx));
7
+ const resolved = new Set();
8
+ const result = [];
9
+ function addQuestion(q) {
10
+ // If this question depends on others, ensure those are added first
11
+ if (q.dependsOn && q.dependsOn.length > 0) {
12
+ for (const dep of q.dependsOn) {
13
+ if (!resolved.has(dep)) {
14
+ const depIdx = nameToIndex.get(dep);
15
+ if (depIdx === undefined) {
16
+ throw new Error(`Unknown dependency: ${dep}`);
17
+ }
18
+ addQuestion(questions[depIdx]);
19
+ }
20
+ }
21
+ }
22
+ if (!resolved.has(q.name)) {
23
+ resolved.add(q.name);
24
+ result.push(q);
25
+ }
26
+ }
27
+ for (const q of questions) {
28
+ addQuestion(q);
29
+ }
30
+ return result;
31
+ }
4
32
  const validationMessage = (question, ctx) => {
5
33
  if (ctx.numTries === 0 || ctx.validation.success) {
6
34
  return ''; // No message if first attempt or validation was successful
@@ -328,8 +356,9 @@ export class Inquirerer {
328
356
  this.handleMissingArgsError(options);
329
357
  throw new Error('Missing required arguments. Please provide all required parameters.');
330
358
  }
331
- for (let index = 0; index < questions.length; index++) {
332
- const question = questions[index];
359
+ const ordered = reorderQuestionsByDeps(questions);
360
+ for (let index = 0; index < ordered.length; index++) {
361
+ const question = ordered[index];
333
362
  const ctx = new PromptContext();
334
363
  // obj is already either argv itself, or a clone, but let's check if it has the property
335
364
  if (question.name in obj) {
@@ -337,6 +366,10 @@ export class Inquirerer {
337
366
  ctx.nextQuestion();
338
367
  continue;
339
368
  }
369
+ if (question.when && !question.when(obj)) {
370
+ ctx.nextQuestion();
371
+ continue;
372
+ }
340
373
  // Apply default value if applicable
341
374
  // this is if useDefault is set, rare! not typical defaults which happen AFTER
342
375
  // this is mostly to avoid a prompt for "hidden" options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "2.0.7",
3
+ "version": "2.1.0",
4
4
  "author": "Dan Lynch <pyramation@gmail.com>",
5
5
  "description": "inquirerer",
6
6
  "main": "index.js",
@@ -35,5 +35,5 @@
35
35
  "minimist": "^1.2.8"
36
36
  },
37
37
  "keywords": [],
38
- "gitHead": "7287f9c5f9ad00ff16c8a5ce562c50a181df5074"
38
+ "gitHead": "e1b5c305be94ffa5f7c0664ad2d4e12f66ac046f"
39
39
  }
package/prompt.d.ts CHANGED
@@ -12,6 +12,7 @@ export interface PromptOptions {
12
12
  manPageInfo?: ManPageInfo;
13
13
  mutateArgs?: boolean;
14
14
  }
15
+ export declare function reorderQuestionsByDeps(questions: Question[]): Question[];
15
16
  declare class PromptContext {
16
17
  numTries: number;
17
18
  needsInput: boolean;
package/prompt.js CHANGED
@@ -3,10 +3,39 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Inquirerer = void 0;
6
+ exports.Inquirerer = exports.reorderQuestionsByDeps = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  const readline_1 = __importDefault(require("readline"));
9
9
  const keypress_1 = require("./keypress");
10
+ function reorderQuestionsByDeps(questions) {
11
+ const nameToIndex = new Map();
12
+ questions.forEach((q, idx) => nameToIndex.set(q.name, idx));
13
+ const resolved = new Set();
14
+ const result = [];
15
+ function addQuestion(q) {
16
+ // If this question depends on others, ensure those are added first
17
+ if (q.dependsOn && q.dependsOn.length > 0) {
18
+ for (const dep of q.dependsOn) {
19
+ if (!resolved.has(dep)) {
20
+ const depIdx = nameToIndex.get(dep);
21
+ if (depIdx === undefined) {
22
+ throw new Error(`Unknown dependency: ${dep}`);
23
+ }
24
+ addQuestion(questions[depIdx]);
25
+ }
26
+ }
27
+ }
28
+ if (!resolved.has(q.name)) {
29
+ resolved.add(q.name);
30
+ result.push(q);
31
+ }
32
+ }
33
+ for (const q of questions) {
34
+ addQuestion(q);
35
+ }
36
+ return result;
37
+ }
38
+ exports.reorderQuestionsByDeps = reorderQuestionsByDeps;
10
39
  const validationMessage = (question, ctx) => {
11
40
  if (ctx.numTries === 0 || ctx.validation.success) {
12
41
  return ''; // No message if first attempt or validation was successful
@@ -334,8 +363,9 @@ class Inquirerer {
334
363
  this.handleMissingArgsError(options);
335
364
  throw new Error('Missing required arguments. Please provide all required parameters.');
336
365
  }
337
- for (let index = 0; index < questions.length; index++) {
338
- const question = questions[index];
366
+ const ordered = reorderQuestionsByDeps(questions);
367
+ for (let index = 0; index < ordered.length; index++) {
368
+ const question = ordered[index];
339
369
  const ctx = new PromptContext();
340
370
  // obj is already either argv itself, or a clone, but let's check if it has the property
341
371
  if (question.name in obj) {
@@ -343,6 +373,10 @@ class Inquirerer {
343
373
  ctx.nextQuestion();
344
374
  continue;
345
375
  }
376
+ if (question.when && !question.when(obj)) {
377
+ ctx.nextQuestion();
378
+ continue;
379
+ }
346
380
  // Apply default value if applicable
347
381
  // this is if useDefault is set, rare! not typical defaults which happen AFTER
348
382
  // this is mostly to avoid a prompt for "hidden" options
@@ -23,6 +23,8 @@ export interface BaseQuestion {
23
23
  validate?: (input: any, obj: any) => Validation | boolean;
24
24
  sanitize?: (input: any, obj: any) => any;
25
25
  pattern?: string;
26
+ dependsOn?: string[];
27
+ when?: (answers: any) => boolean;
26
28
  }
27
29
  export interface ConfirmQuestion extends BaseQuestion {
28
30
  type: 'confirm';