inquirerer 1.7.0 → 1.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/commander.js CHANGED
@@ -48,7 +48,8 @@ class CLI {
48
48
  process.exit(0);
49
49
  }
50
50
  const args = await this.commandHandler(this.argv, this.prompter, this.options);
51
- this.prompter.exit();
51
+ // this.prompter.exit();
52
+ this.prompter.close();
52
53
  return args;
53
54
  }
54
55
  }
package/esm/commander.js CHANGED
@@ -42,7 +42,8 @@ export class CLI {
42
42
  process.exit(0);
43
43
  }
44
44
  const args = await this.commandHandler(this.argv, this.prompter, this.options);
45
- this.prompter.exit();
45
+ // this.prompter.exit();
46
+ this.prompter.close();
46
47
  return args;
47
48
  }
48
49
  }
package/esm/prompt.js CHANGED
@@ -249,29 +249,19 @@ export class Inquirerer {
249
249
  // use local mutateArgs if defined, otherwise global mutateArgs
250
250
  const shouldMutate = options?.mutateArgs !== undefined ? options.mutateArgs : this.mutateArgs;
251
251
  let obj = shouldMutate ? argv : { ...argv };
252
- // Assuming questions is an array of Question objects and argv is the parsed command line arguments object.
253
- if (this.noTty && questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
254
- for (let index = 0; index < questions.length; index++) {
255
- const question = questions[index];
256
- // Apply default value if applicable
257
- if ('default' in question) {
258
- obj[question.name] = question.default;
259
- }
260
- }
261
- if (!questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
262
- return obj;
263
- }
264
- // If running in a non-interactive mode and any required argument is missing, handle the error.
265
- this.clearScreen();
266
- if (options?.usageText) {
267
- this.log(options.usageText);
268
- }
269
- else if (options?.manPageInfo) {
270
- this.log(this.generateManPage(options.manPageInfo));
271
- }
272
- else {
273
- this.log('Missing required arguments. Please provide all required parameters.');
274
- }
252
+ // first loop through the question, and set any overrides in case other questions use objs for validation
253
+ this.applyOverrides(argv, obj, questions);
254
+ // Check for required arguments when no terminal is available (non-interactive mode)
255
+ if (this.noTty && this.hasMissingRequiredArgs(questions, argv)) {
256
+ // Apply default values for all questions
257
+ this.applyDefaultValues(questions, obj);
258
+ // Recheck for missing required arguments after applying defaults
259
+ // NOT so sure this would ever happen, but possible if developer did something wrong
260
+ if (!this.hasMissingRequiredArgs(questions, argv)) {
261
+ return obj; // Return the updated object if no required arguments are missing
262
+ }
263
+ // Handle error for missing required arguments
264
+ this.handleMissingArgsError(options);
275
265
  throw new Error('Missing required arguments. Please provide all required parameters.');
276
266
  }
277
267
  for (let index = 0; index < questions.length; index++) {
@@ -279,10 +269,12 @@ export class Inquirerer {
279
269
  const ctx = new PromptContext();
280
270
  // obj is already either argv itself, or a clone, but let's check if it has the property
281
271
  if (question.name in obj) {
272
+ this.handleOverrides(argv, obj, question);
282
273
  ctx.nextQuestion();
283
274
  continue;
284
275
  }
285
276
  // Apply default value if applicable
277
+ // this is if useDefault is set, rare! not typical defaults which happen AFTER
286
278
  if ('default' in question && (this.useDefaults || question.useDefault)) {
287
279
  obj[question.name] = question.default;
288
280
  continue; // Skip to the next question since the default is applied
@@ -303,6 +295,60 @@ export class Inquirerer {
303
295
  }
304
296
  return obj;
305
297
  }
298
+ handleMissingArgsError(options) {
299
+ this.clearScreen();
300
+ if (options?.usageText) {
301
+ this.log(options.usageText);
302
+ }
303
+ else if (options?.manPageInfo) {
304
+ this.log(this.generateManPage(options.manPageInfo));
305
+ }
306
+ else {
307
+ this.log('Missing required arguments. Please provide all required parameters.');
308
+ }
309
+ }
310
+ hasMissingRequiredArgs(questions, argv) {
311
+ return questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]));
312
+ }
313
+ applyDefaultValues(questions, obj) {
314
+ questions.forEach(question => {
315
+ if ('default' in question) {
316
+ obj[question.name] = question.default; // Set default value if specified
317
+ }
318
+ });
319
+ }
320
+ applyOverrides(argv, obj, questions) {
321
+ questions.forEach(question => {
322
+ if (question.name in argv) {
323
+ this.handleOverrides(argv, obj, question);
324
+ }
325
+ });
326
+ }
327
+ handleOverrides(argv, obj, question) {
328
+ switch (question.type) {
329
+ case 'text':
330
+ case 'confirm':
331
+ // do nothing, already set!
332
+ break;
333
+ case 'checkbox':
334
+ case 'autocomplete':
335
+ // get the value from options :)
336
+ this.handleOverridesWithOptions(argv, obj, question);
337
+ break;
338
+ default:
339
+ return;
340
+ }
341
+ }
342
+ handleOverridesWithOptions(argv, obj, question) {
343
+ // obj is already either argv itself, or a clone, but let's check if it has the property
344
+ if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
345
+ const options = this.sanitizeOptions(question);
346
+ const found = options.find(option => option.name === argv[question.name]);
347
+ if (typeof found !== 'undefined') {
348
+ obj[question.name] = found.value;
349
+ }
350
+ }
351
+ }
306
352
  async handleQuestionType(question, ctx) {
307
353
  switch (question.type) {
308
354
  case 'confirm':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "1.7.0",
3
+ "version": "1.8.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": "273e730c2481312b2d332d3711b1728210799e83"
38
+ "gitHead": "b664b9a084200b70201447ebc3d37a15991e89c9"
39
39
  }
package/prompt.d.ts CHANGED
@@ -54,6 +54,12 @@ export declare class Inquirerer {
54
54
  private sanitizeAnswer;
55
55
  exit(): void;
56
56
  prompt<T extends object>(argv: T, questions: Question[], options?: PromptOptions): Promise<T>;
57
+ private handleMissingArgsError;
58
+ private hasMissingRequiredArgs;
59
+ private applyDefaultValues;
60
+ private applyOverrides;
61
+ private handleOverrides;
62
+ private handleOverridesWithOptions;
57
63
  private handleQuestionType;
58
64
  confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
59
65
  text(question: TextQuestion, ctx: PromptContext): Promise<string | null>;
@@ -63,6 +69,6 @@ export declare class Inquirerer {
63
69
  private sanitizeOptions;
64
70
  private filterOptions;
65
71
  private getMaxLines;
66
- private close;
72
+ close(): void;
67
73
  }
68
74
  export {};
package/prompt.js CHANGED
@@ -255,29 +255,19 @@ class Inquirerer {
255
255
  // use local mutateArgs if defined, otherwise global mutateArgs
256
256
  const shouldMutate = options?.mutateArgs !== undefined ? options.mutateArgs : this.mutateArgs;
257
257
  let obj = shouldMutate ? argv : { ...argv };
258
- // Assuming questions is an array of Question objects and argv is the parsed command line arguments object.
259
- if (this.noTty && questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
260
- for (let index = 0; index < questions.length; index++) {
261
- const question = questions[index];
262
- // Apply default value if applicable
263
- if ('default' in question) {
264
- obj[question.name] = question.default;
265
- }
266
- }
267
- if (!questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
268
- return obj;
269
- }
270
- // If running in a non-interactive mode and any required argument is missing, handle the error.
271
- this.clearScreen();
272
- if (options?.usageText) {
273
- this.log(options.usageText);
274
- }
275
- else if (options?.manPageInfo) {
276
- this.log(this.generateManPage(options.manPageInfo));
277
- }
278
- else {
279
- this.log('Missing required arguments. Please provide all required parameters.');
280
- }
258
+ // first loop through the question, and set any overrides in case other questions use objs for validation
259
+ this.applyOverrides(argv, obj, questions);
260
+ // Check for required arguments when no terminal is available (non-interactive mode)
261
+ if (this.noTty && this.hasMissingRequiredArgs(questions, argv)) {
262
+ // Apply default values for all questions
263
+ this.applyDefaultValues(questions, obj);
264
+ // Recheck for missing required arguments after applying defaults
265
+ // NOT so sure this would ever happen, but possible if developer did something wrong
266
+ if (!this.hasMissingRequiredArgs(questions, argv)) {
267
+ return obj; // Return the updated object if no required arguments are missing
268
+ }
269
+ // Handle error for missing required arguments
270
+ this.handleMissingArgsError(options);
281
271
  throw new Error('Missing required arguments. Please provide all required parameters.');
282
272
  }
283
273
  for (let index = 0; index < questions.length; index++) {
@@ -285,10 +275,12 @@ class Inquirerer {
285
275
  const ctx = new PromptContext();
286
276
  // obj is already either argv itself, or a clone, but let's check if it has the property
287
277
  if (question.name in obj) {
278
+ this.handleOverrides(argv, obj, question);
288
279
  ctx.nextQuestion();
289
280
  continue;
290
281
  }
291
282
  // Apply default value if applicable
283
+ // this is if useDefault is set, rare! not typical defaults which happen AFTER
292
284
  if ('default' in question && (this.useDefaults || question.useDefault)) {
293
285
  obj[question.name] = question.default;
294
286
  continue; // Skip to the next question since the default is applied
@@ -309,6 +301,60 @@ class Inquirerer {
309
301
  }
310
302
  return obj;
311
303
  }
304
+ handleMissingArgsError(options) {
305
+ this.clearScreen();
306
+ if (options?.usageText) {
307
+ this.log(options.usageText);
308
+ }
309
+ else if (options?.manPageInfo) {
310
+ this.log(this.generateManPage(options.manPageInfo));
311
+ }
312
+ else {
313
+ this.log('Missing required arguments. Please provide all required parameters.');
314
+ }
315
+ }
316
+ hasMissingRequiredArgs(questions, argv) {
317
+ return questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]));
318
+ }
319
+ applyDefaultValues(questions, obj) {
320
+ questions.forEach(question => {
321
+ if ('default' in question) {
322
+ obj[question.name] = question.default; // Set default value if specified
323
+ }
324
+ });
325
+ }
326
+ applyOverrides(argv, obj, questions) {
327
+ questions.forEach(question => {
328
+ if (question.name in argv) {
329
+ this.handleOverrides(argv, obj, question);
330
+ }
331
+ });
332
+ }
333
+ handleOverrides(argv, obj, question) {
334
+ switch (question.type) {
335
+ case 'text':
336
+ case 'confirm':
337
+ // do nothing, already set!
338
+ break;
339
+ case 'checkbox':
340
+ case 'autocomplete':
341
+ // get the value from options :)
342
+ this.handleOverridesWithOptions(argv, obj, question);
343
+ break;
344
+ default:
345
+ return;
346
+ }
347
+ }
348
+ handleOverridesWithOptions(argv, obj, question) {
349
+ // obj is already either argv itself, or a clone, but let's check if it has the property
350
+ if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
351
+ const options = this.sanitizeOptions(question);
352
+ const found = options.find(option => option.name === argv[question.name]);
353
+ if (typeof found !== 'undefined') {
354
+ obj[question.name] = found.value;
355
+ }
356
+ }
357
+ }
312
358
  async handleQuestionType(question, ctx) {
313
359
  switch (question.type) {
314
360
  case 'confirm':