inquirerer 1.6.1 → 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
@@ -100,13 +100,7 @@ export class Inquirerer {
100
100
  if (!noTty) {
101
101
  this.rl = readline.createInterface({
102
102
  input,
103
- // dissallow readline from prompting user, since we'll handle it!
104
103
  output
105
- // : new Writable({
106
- // write(chunk, encoding, callback) {
107
- // callback(); // Do nothing with the data
108
- // }
109
- // })
110
104
  });
111
105
  this.keypress = new TerminalKeypress(noTty, input);
112
106
  }
@@ -255,35 +249,32 @@ export class Inquirerer {
255
249
  // use local mutateArgs if defined, otherwise global mutateArgs
256
250
  const shouldMutate = options?.mutateArgs !== undefined ? options.mutateArgs : this.mutateArgs;
257
251
  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
- }
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);
281
265
  throw new Error('Missing required arguments. Please provide all required parameters.');
282
266
  }
283
267
  for (let index = 0; index < questions.length; index++) {
284
268
  const question = questions[index];
285
269
  const ctx = new PromptContext();
270
+ // obj is already either argv itself, or a clone, but let's check if it has the property
271
+ if (question.name in obj) {
272
+ this.handleOverrides(argv, obj, question);
273
+ ctx.nextQuestion();
274
+ continue;
275
+ }
286
276
  // Apply default value if applicable
277
+ // this is if useDefault is set, rare! not typical defaults which happen AFTER
287
278
  if ('default' in question && (this.useDefaults || question.useDefault)) {
288
279
  obj[question.name] = question.default;
289
280
  continue; // Skip to the next question since the default is applied
@@ -304,6 +295,60 @@ export class Inquirerer {
304
295
  }
305
296
  return obj;
306
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
+ }
307
352
  async handleQuestionType(question, ctx) {
308
353
  switch (question.type) {
309
354
  case 'confirm':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "1.6.1",
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": "25c70c84b496b546e74bd4268eadc0a49bc6d9e7"
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
@@ -106,13 +106,7 @@ class Inquirerer {
106
106
  if (!noTty) {
107
107
  this.rl = readline_1.default.createInterface({
108
108
  input,
109
- // dissallow readline from prompting user, since we'll handle it!
110
109
  output
111
- // : new Writable({
112
- // write(chunk, encoding, callback) {
113
- // callback(); // Do nothing with the data
114
- // }
115
- // })
116
110
  });
117
111
  this.keypress = new keypress_1.TerminalKeypress(noTty, input);
118
112
  }
@@ -261,35 +255,32 @@ class Inquirerer {
261
255
  // use local mutateArgs if defined, otherwise global mutateArgs
262
256
  const shouldMutate = options?.mutateArgs !== undefined ? options.mutateArgs : this.mutateArgs;
263
257
  let obj = shouldMutate ? argv : { ...argv };
264
- // Assuming questions is an array of Question objects and argv is the parsed command line arguments object.
265
- if (this.noTty && questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
266
- for (let index = 0; index < questions.length; index++) {
267
- const question = questions[index];
268
- // Apply default value if applicable
269
- if ('default' in question) {
270
- obj[question.name] = question.default;
271
- }
272
- }
273
- if (!questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
274
- return obj;
275
- }
276
- // If running in a non-interactive mode and any required argument is missing, handle the error.
277
- this.clearScreen();
278
- if (options?.usageText) {
279
- this.log(options.usageText);
280
- }
281
- else if (options?.manPageInfo) {
282
- this.log(this.generateManPage(options.manPageInfo));
283
- }
284
- else {
285
- this.log('Missing required arguments. Please provide all required parameters.');
286
- }
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);
287
271
  throw new Error('Missing required arguments. Please provide all required parameters.');
288
272
  }
289
273
  for (let index = 0; index < questions.length; index++) {
290
274
  const question = questions[index];
291
275
  const ctx = new PromptContext();
276
+ // obj is already either argv itself, or a clone, but let's check if it has the property
277
+ if (question.name in obj) {
278
+ this.handleOverrides(argv, obj, question);
279
+ ctx.nextQuestion();
280
+ continue;
281
+ }
292
282
  // Apply default value if applicable
283
+ // this is if useDefault is set, rare! not typical defaults which happen AFTER
293
284
  if ('default' in question && (this.useDefaults || question.useDefault)) {
294
285
  obj[question.name] = question.default;
295
286
  continue; // Skip to the next question since the default is applied
@@ -310,6 +301,60 @@ class Inquirerer {
310
301
  }
311
302
  return obj;
312
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
+ }
313
358
  async handleQuestionType(question, ctx) {
314
359
  switch (question.type) {
315
360
  case 'confirm':