inquirerer 1.7.0 → 1.9.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/keypress.js +6 -2
- package/esm/prompt.js +69 -23
- package/keypress.js +6 -2
- package/package.json +2 -2
- package/prompt.d.ts +7 -1
- package/prompt.js +69 -23
package/commander.js
CHANGED
package/esm/commander.js
CHANGED
package/esm/keypress.js
CHANGED
|
@@ -23,7 +23,9 @@ export class TerminalKeypress {
|
|
|
23
23
|
this.input = input;
|
|
24
24
|
this.proc = proc;
|
|
25
25
|
if (this.isTTY()) {
|
|
26
|
-
this.input.setRawMode
|
|
26
|
+
if (typeof this.input.setRawMode === 'function') {
|
|
27
|
+
this.input.setRawMode(true);
|
|
28
|
+
}
|
|
27
29
|
this.input.resume();
|
|
28
30
|
this.input.setEncoding('utf8');
|
|
29
31
|
}
|
|
@@ -64,7 +66,9 @@ export class TerminalKeypress {
|
|
|
64
66
|
this.active = true;
|
|
65
67
|
}
|
|
66
68
|
destroy() {
|
|
67
|
-
this.input.setRawMode
|
|
69
|
+
if (typeof this.input.setRawMode === 'function') {
|
|
70
|
+
this.input.setRawMode(false);
|
|
71
|
+
}
|
|
68
72
|
this.input.pause();
|
|
69
73
|
this.input.removeAllListeners('data');
|
|
70
74
|
}
|
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
|
-
//
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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/keypress.js
CHANGED
|
@@ -26,7 +26,9 @@ class TerminalKeypress {
|
|
|
26
26
|
this.input = input;
|
|
27
27
|
this.proc = proc;
|
|
28
28
|
if (this.isTTY()) {
|
|
29
|
-
this.input.setRawMode
|
|
29
|
+
if (typeof this.input.setRawMode === 'function') {
|
|
30
|
+
this.input.setRawMode(true);
|
|
31
|
+
}
|
|
30
32
|
this.input.resume();
|
|
31
33
|
this.input.setEncoding('utf8');
|
|
32
34
|
}
|
|
@@ -67,7 +69,9 @@ class TerminalKeypress {
|
|
|
67
69
|
this.active = true;
|
|
68
70
|
}
|
|
69
71
|
destroy() {
|
|
70
|
-
this.input.setRawMode
|
|
72
|
+
if (typeof this.input.setRawMode === 'function') {
|
|
73
|
+
this.input.setRawMode(false);
|
|
74
|
+
}
|
|
71
75
|
this.input.pause();
|
|
72
76
|
this.input.removeAllListeners('data');
|
|
73
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.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": "9cda01fdb660defddb159c9a6afee578e833e9c1"
|
|
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
|
@@ -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
|
-
//
|
|
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
|
-
}
|
|
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':
|