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 +2 -1
- package/esm/commander.js +2 -1
- package/esm/prompt.js +74 -29
- package/package.json +2 -2
- package/prompt.d.ts +7 -1
- package/prompt.js +74 -29
package/commander.js
CHANGED
package/esm/commander.js
CHANGED
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
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
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.
|
|
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": "
|
|
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
|
-
|
|
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
|
-
//
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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':
|