inquirerer 0.12.0 → 1.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/commander.js +1 -1
- package/esm/commander.js +1 -1
- package/esm/prompt.js +228 -58
- package/package.json +2 -2
- package/prompt.d.ts +26 -3
- package/prompt.js +228 -58
- package/question/types.d.ts +9 -0
package/commander.js
CHANGED
|
@@ -33,7 +33,7 @@ class CLI {
|
|
|
33
33
|
this.options = mergedOptions;
|
|
34
34
|
this.argv = argv ? argv : (0, minimist_1.default)(process.argv.slice(2), this.options.minimistOpts);
|
|
35
35
|
this.prompter = new prompt_1.Inquirerer({
|
|
36
|
-
noTty: this.options.noTty,
|
|
36
|
+
noTty: !this.argv.tty ? true : this.options.noTty,
|
|
37
37
|
input: this.options.input,
|
|
38
38
|
output: this.options.output
|
|
39
39
|
});
|
package/esm/commander.js
CHANGED
|
@@ -27,7 +27,7 @@ export class CLI {
|
|
|
27
27
|
this.options = mergedOptions;
|
|
28
28
|
this.argv = argv ? argv : minimist(process.argv.slice(2), this.options.minimistOpts);
|
|
29
29
|
this.prompter = new Inquirerer({
|
|
30
|
-
noTty: this.options.noTty,
|
|
30
|
+
noTty: !this.argv.tty ? true : this.options.noTty,
|
|
31
31
|
input: this.options.input,
|
|
32
32
|
output: this.options.output
|
|
33
33
|
});
|
package/esm/prompt.js
CHANGED
|
@@ -1,16 +1,65 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import readline from 'readline';
|
|
3
3
|
import { KEY_CODES, TerminalKeypress } from './keypress';
|
|
4
|
-
const
|
|
4
|
+
const validationMessage = (question, ctx) => {
|
|
5
|
+
if (ctx.numTries === 0 || ctx.validation.success) {
|
|
6
|
+
return ''; // No message if first attempt or validation was successful
|
|
7
|
+
}
|
|
8
|
+
if (ctx.validation.reason) {
|
|
9
|
+
return chalk.red(`The field "${question.name}" is invalid: ${ctx.validation.reason}\n`);
|
|
10
|
+
}
|
|
11
|
+
switch (ctx.validation.type) {
|
|
12
|
+
case 'required':
|
|
13
|
+
return chalk.red(`The field "${question.name}" is required. Please provide a value.\n`);
|
|
14
|
+
case 'pattern':
|
|
15
|
+
return chalk.red(`The field "${question.name}" does not match the pattern: ${question.pattern}.\n`);
|
|
16
|
+
default:
|
|
17
|
+
return chalk.red(`The field "${question.name}" is invalid. Please try again.\n`);
|
|
18
|
+
}
|
|
19
|
+
return ''; // Return empty string if no specific conditions are met
|
|
20
|
+
};
|
|
21
|
+
class PromptContext {
|
|
22
|
+
numTries = 0;
|
|
23
|
+
needsInput = true;
|
|
24
|
+
validation = { success: false };
|
|
25
|
+
constructor() { }
|
|
26
|
+
tryAgain(validation) {
|
|
27
|
+
this.numTries++;
|
|
28
|
+
this.needsInput = true;
|
|
29
|
+
this.validation = { ...this.validation, ...validation, success: false };
|
|
30
|
+
}
|
|
31
|
+
nextQuestion() {
|
|
32
|
+
this.numTries = 0;
|
|
33
|
+
this.needsInput = false;
|
|
34
|
+
this.validation = { success: true };
|
|
35
|
+
}
|
|
36
|
+
process(validation) {
|
|
37
|
+
if (typeof validation === 'boolean') {
|
|
38
|
+
if (validation) {
|
|
39
|
+
this.nextQuestion();
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.tryAgain({ type: 'validation' });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
if (validation.success) {
|
|
47
|
+
this.nextQuestion();
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.tryAgain(validation);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return this.validation;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
5
56
|
function generatePromptMessage(question, ctx) {
|
|
6
57
|
let promptMessage = '';
|
|
7
58
|
if (question.message) {
|
|
8
59
|
promptMessage = chalk.whiteBright(question.message) + '\n';
|
|
9
60
|
}
|
|
10
61
|
promptMessage += `${chalk.white('[')}${chalk.green('--' + question.name)}${chalk.white(']:')}\n`;
|
|
11
|
-
|
|
12
|
-
promptMessage = requiredMessage(question) + promptMessage;
|
|
13
|
-
}
|
|
62
|
+
promptMessage = validationMessage(question, ctx) + promptMessage;
|
|
14
63
|
switch (question.type) {
|
|
15
64
|
case 'confirm':
|
|
16
65
|
promptMessage += `(y/n)${question.default !== undefined ? ` [${question.default ? 'y' : 'n'}]` : ''}`;
|
|
@@ -85,65 +134,178 @@ export class Inquirerer {
|
|
|
85
134
|
const prompt = this.getPrompt(question, ctx, input);
|
|
86
135
|
this.log(prompt);
|
|
87
136
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
//
|
|
91
|
-
|
|
92
|
-
|
|
137
|
+
generateManPage(opts) {
|
|
138
|
+
let manPage = `NAME\n\t${opts.commandName} ${opts.description ?? ''}\n\n`;
|
|
139
|
+
// Constructing the SYNOPSIS section with required and optional arguments
|
|
140
|
+
let requiredArgs = '';
|
|
141
|
+
let optionalArgs = '';
|
|
142
|
+
opts.questions.forEach(question => {
|
|
143
|
+
if (question.required) {
|
|
144
|
+
requiredArgs += ` --${question.name} <${question.name}>`;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
optionalArgs += ` [--${question.name}${question.default ? `=${question.default}` : ''}]`;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
manPage += `SYNOPSIS\n\t${opts.commandName}${requiredArgs}${optionalArgs}\n\n`;
|
|
151
|
+
manPage += `DESCRIPTION\n\tUse this command to interact with the application. It supports the following options:\n\n`;
|
|
152
|
+
opts.questions.forEach(question => {
|
|
153
|
+
manPage += `${question.name.toUpperCase()}\n`;
|
|
154
|
+
manPage += `\tType: ${question.type}\n`;
|
|
155
|
+
if (question.message) {
|
|
156
|
+
manPage += `\tSummary: ${question.message}\n`;
|
|
157
|
+
}
|
|
158
|
+
if (question.description) {
|
|
159
|
+
manPage += `\tDescription: ${question.description}\n`;
|
|
160
|
+
}
|
|
161
|
+
if ('options' in question) {
|
|
162
|
+
const optionsList = Array.isArray(question.options)
|
|
163
|
+
? question.options.map(opt => typeof opt === 'string' ? opt : `${opt.name} (${opt.value})`).join(', ')
|
|
164
|
+
: '';
|
|
165
|
+
manPage += `\tOptions: ${optionsList}\n`;
|
|
166
|
+
}
|
|
167
|
+
if (question.default !== undefined) {
|
|
168
|
+
manPage += `\tDefault: ${JSON.stringify(question.default)}\n`;
|
|
169
|
+
}
|
|
170
|
+
if (question.required) {
|
|
171
|
+
manPage += `\tRequired: Yes\n`;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
manPage += `\tRequired: No\n`;
|
|
175
|
+
}
|
|
176
|
+
manPage += '\n';
|
|
177
|
+
});
|
|
178
|
+
manPage += `EXAMPLES\n\tExample usage of \`${opts.commandName}\`.\n\t$ ${opts.commandName}${requiredArgs}${optionalArgs}\n\n`;
|
|
179
|
+
manPage += opts.author ? `AUTHOR\n\t${opts.author}\n` : '';
|
|
180
|
+
return manPage;
|
|
181
|
+
}
|
|
182
|
+
isValidatableAnswer(answer) {
|
|
183
|
+
return answer !== undefined;
|
|
184
|
+
}
|
|
185
|
+
validateAnswer(question, answer, obj, ctx) {
|
|
186
|
+
const validation = this.validateAnswerPattern(question, answer);
|
|
187
|
+
if (!validation.success) {
|
|
188
|
+
return ctx.process(validation);
|
|
189
|
+
}
|
|
190
|
+
if (question.validate) {
|
|
191
|
+
const customValidation = question.validate(answer, obj);
|
|
192
|
+
return ctx.process(customValidation);
|
|
193
|
+
}
|
|
194
|
+
return ctx.process({
|
|
195
|
+
success: true
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
isValid(question, obj, ctx) {
|
|
199
|
+
if (this.isValidatableAnswer(obj[question.name])) {
|
|
200
|
+
obj[question.name] = this.sanitizeAnswer(question, obj[question.name], obj);
|
|
201
|
+
const validationResult = this.validateAnswer(question, obj[question.name], obj, ctx);
|
|
202
|
+
if (!validationResult.success) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (question.required && this.isEmptyAnswer(obj[question.name])) {
|
|
207
|
+
ctx.tryAgain({ type: 'required' });
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
validateAnswerPattern(question, answer) {
|
|
213
|
+
if (question.pattern && typeof answer === 'string') {
|
|
214
|
+
const regex = new RegExp(question.pattern);
|
|
215
|
+
const success = regex.test(answer);
|
|
216
|
+
if (success) {
|
|
217
|
+
return {
|
|
218
|
+
success
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
return {
|
|
223
|
+
type: 'pattern',
|
|
224
|
+
success: false,
|
|
225
|
+
reason: question.pattern
|
|
226
|
+
};
|
|
227
|
+
}
|
|
93
228
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return (value === undefined ||
|
|
97
|
-
value === null ||
|
|
98
|
-
(typeof value === 'string' && value.trim().length === 0) // Check for empty string, safely trimming it
|
|
99
|
-
);
|
|
229
|
+
return {
|
|
230
|
+
success: true
|
|
100
231
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
232
|
+
}
|
|
233
|
+
isEmptyAnswer(answer) {
|
|
234
|
+
switch (true) {
|
|
235
|
+
case answer === undefined:
|
|
236
|
+
case answer === null:
|
|
237
|
+
case answer === '':
|
|
238
|
+
case Array.isArray(answer) && answer.length === 0:
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
sanitizeAnswer(question, answer, obj) {
|
|
244
|
+
if (question.sanitize) {
|
|
245
|
+
return question.sanitize(answer, obj);
|
|
246
|
+
}
|
|
247
|
+
return answer;
|
|
248
|
+
}
|
|
249
|
+
async prompt(argv, questions, docs) {
|
|
250
|
+
const obj = { ...argv };
|
|
251
|
+
// Assuming questions is an array of Question objects and argv is the parsed command line arguments object.
|
|
252
|
+
// @ts-ignore
|
|
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
|
+
// @ts-ignore
|
|
262
|
+
if (!questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
|
|
263
|
+
return obj;
|
|
264
|
+
}
|
|
265
|
+
// If running in a non-interactive mode and any required argument is missing, handle the error.
|
|
266
|
+
if (docs?.usageText) {
|
|
267
|
+
this.log(docs.usageText);
|
|
268
|
+
}
|
|
269
|
+
else if (docs?.manPageInfo) {
|
|
270
|
+
this.log(this.generateManPage(docs.manPageInfo));
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
this.log('Missing required arguments. Please provide all required parameters.');
|
|
274
|
+
}
|
|
275
|
+
process.exit(1);
|
|
276
|
+
}
|
|
277
|
+
for (let index = 0; index < questions.length; index++) {
|
|
104
278
|
const question = questions[index];
|
|
279
|
+
const ctx = new PromptContext();
|
|
280
|
+
// Apply default value if applicable
|
|
105
281
|
if ('default' in question && (this.useDefaults || question.useDefault)) {
|
|
106
282
|
obj[question.name] = question.default;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (needsContinue(question)) {
|
|
114
|
-
switch (question.type) {
|
|
115
|
-
case 'confirm':
|
|
116
|
-
obj[question.name] = await this.confirm(question, ctx);
|
|
117
|
-
break;
|
|
118
|
-
case 'checkbox':
|
|
119
|
-
obj[question.name] = await this.checkbox(question, ctx);
|
|
120
|
-
break;
|
|
121
|
-
case 'autocomplete':
|
|
122
|
-
obj[question.name] = await this.autocomplete(question, ctx);
|
|
123
|
-
break;
|
|
124
|
-
case 'text':
|
|
125
|
-
default:
|
|
126
|
-
obj[question.name] = await this.text(question, ctx); // Use promptText instead of text
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
// Check if the question is required and the response is not adequate
|
|
130
|
-
if (question.required && needsContinue(question)) {
|
|
131
|
-
// Reset the property to undefined to re-trigger the prompt
|
|
132
|
-
numTries++;
|
|
133
|
-
obj[question.name] = undefined;
|
|
134
|
-
continue; // Stay on the same question
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
if ('default' in question) {
|
|
138
|
-
obj[question.name] = question.default;
|
|
139
|
-
}
|
|
283
|
+
continue; // Skip to the next question since the default is applied
|
|
284
|
+
}
|
|
285
|
+
while (ctx.needsInput) {
|
|
286
|
+
obj[question.name] = await this.handleQuestionType(question, ctx);
|
|
287
|
+
if (!this.isValid(question, obj, ctx)) {
|
|
288
|
+
continue;
|
|
140
289
|
}
|
|
290
|
+
// If input passes validation and is not empty, or not required, move to the next question
|
|
291
|
+
ctx.nextQuestion();
|
|
141
292
|
}
|
|
142
|
-
index++; // Move to the next question
|
|
143
|
-
numTries = 0;
|
|
144
293
|
}
|
|
145
294
|
return obj;
|
|
146
295
|
}
|
|
296
|
+
async handleQuestionType(question, ctx) {
|
|
297
|
+
switch (question.type) {
|
|
298
|
+
case 'confirm':
|
|
299
|
+
return this.confirm(question, ctx);
|
|
300
|
+
case 'checkbox':
|
|
301
|
+
return this.checkbox(question, ctx);
|
|
302
|
+
case 'autocomplete':
|
|
303
|
+
return this.autocomplete(question, ctx);
|
|
304
|
+
case 'text':
|
|
305
|
+
default:
|
|
306
|
+
return this.text(question, ctx);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
147
309
|
async confirm(question, ctx) {
|
|
148
310
|
if (this.noTty || !this.rl)
|
|
149
311
|
return question.default ?? false; // Return default if non-interactive
|
|
@@ -164,8 +326,12 @@ export class Inquirerer {
|
|
|
164
326
|
});
|
|
165
327
|
}
|
|
166
328
|
async text(question, ctx) {
|
|
167
|
-
if (this.noTty || !this.rl)
|
|
168
|
-
|
|
329
|
+
if (this.noTty || !this.rl) {
|
|
330
|
+
if ('default' in question) {
|
|
331
|
+
return question.default;
|
|
332
|
+
}
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
169
335
|
let input = '';
|
|
170
336
|
return new Promise((resolve) => {
|
|
171
337
|
this.clearScreen();
|
|
@@ -282,8 +448,12 @@ export class Inquirerer {
|
|
|
282
448
|
});
|
|
283
449
|
}
|
|
284
450
|
async autocomplete(question, ctx) {
|
|
285
|
-
if (this.noTty || !this.rl)
|
|
286
|
-
|
|
451
|
+
if (this.noTty || !this.rl) {
|
|
452
|
+
if ('default' in question) {
|
|
453
|
+
return question.default;
|
|
454
|
+
}
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
287
457
|
this.keypress.resume();
|
|
288
458
|
const options = this.sanitizeOptions(question);
|
|
289
459
|
let input = '';
|
|
@@ -369,7 +539,7 @@ export class Inquirerer {
|
|
|
369
539
|
if (typeof option === 'string') {
|
|
370
540
|
return { name: option, value: option };
|
|
371
541
|
}
|
|
372
|
-
else if (typeof option === 'object' && 'name' in option) {
|
|
542
|
+
else if (typeof option === 'object' && option && 'name' in option) {
|
|
373
543
|
return { name: option.name, value: option.value };
|
|
374
544
|
}
|
|
375
545
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.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": "
|
|
38
|
+
"gitHead": "df754ef4059b18c9a98378134f365fd0b8063d4c"
|
|
39
39
|
}
|
package/prompt.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Readable, Writable } from 'stream';
|
|
3
|
-
import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, OptionValue, Question, TextQuestion } from './question';
|
|
4
|
-
interface
|
|
3
|
+
import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, OptionValue, Question, TextQuestion, Validation } from './question';
|
|
4
|
+
interface ManPageInfo {
|
|
5
|
+
commandName: string;
|
|
6
|
+
questions: Question[];
|
|
7
|
+
author?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
declare class PromptContext {
|
|
5
11
|
numTries: number;
|
|
12
|
+
needsInput: boolean;
|
|
13
|
+
validation: Validation;
|
|
14
|
+
constructor();
|
|
15
|
+
tryAgain(validation: Partial<Validation>): void;
|
|
16
|
+
nextQuestion(): void;
|
|
17
|
+
process(validation: Validation | boolean): Validation;
|
|
6
18
|
}
|
|
7
19
|
export interface InquirererOptions {
|
|
8
20
|
noTty?: boolean;
|
|
@@ -26,7 +38,18 @@ export declare class Inquirerer {
|
|
|
26
38
|
getInput(input: string): string;
|
|
27
39
|
getPrompt(question: Question, ctx: PromptContext, input: string): string;
|
|
28
40
|
displayPrompt(question: Question, ctx: PromptContext, input: string): void;
|
|
29
|
-
|
|
41
|
+
generateManPage(opts: ManPageInfo): string;
|
|
42
|
+
private isValidatableAnswer;
|
|
43
|
+
private validateAnswer;
|
|
44
|
+
private isValid;
|
|
45
|
+
private validateAnswerPattern;
|
|
46
|
+
private isEmptyAnswer;
|
|
47
|
+
private sanitizeAnswer;
|
|
48
|
+
prompt<T extends object>(argv: T, questions: Question[], docs?: {
|
|
49
|
+
usageText?: string;
|
|
50
|
+
manPageInfo?: ManPageInfo;
|
|
51
|
+
}): Promise<T>;
|
|
52
|
+
private handleQuestionType;
|
|
30
53
|
confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
|
|
31
54
|
text(question: TextQuestion, ctx: PromptContext): Promise<string | null>;
|
|
32
55
|
checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<OptionValue[]>;
|
package/prompt.js
CHANGED
|
@@ -7,16 +7,65 @@ exports.Inquirerer = 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
|
-
const
|
|
10
|
+
const validationMessage = (question, ctx) => {
|
|
11
|
+
if (ctx.numTries === 0 || ctx.validation.success) {
|
|
12
|
+
return ''; // No message if first attempt or validation was successful
|
|
13
|
+
}
|
|
14
|
+
if (ctx.validation.reason) {
|
|
15
|
+
return chalk_1.default.red(`The field "${question.name}" is invalid: ${ctx.validation.reason}\n`);
|
|
16
|
+
}
|
|
17
|
+
switch (ctx.validation.type) {
|
|
18
|
+
case 'required':
|
|
19
|
+
return chalk_1.default.red(`The field "${question.name}" is required. Please provide a value.\n`);
|
|
20
|
+
case 'pattern':
|
|
21
|
+
return chalk_1.default.red(`The field "${question.name}" does not match the pattern: ${question.pattern}.\n`);
|
|
22
|
+
default:
|
|
23
|
+
return chalk_1.default.red(`The field "${question.name}" is invalid. Please try again.\n`);
|
|
24
|
+
}
|
|
25
|
+
return ''; // Return empty string if no specific conditions are met
|
|
26
|
+
};
|
|
27
|
+
class PromptContext {
|
|
28
|
+
numTries = 0;
|
|
29
|
+
needsInput = true;
|
|
30
|
+
validation = { success: false };
|
|
31
|
+
constructor() { }
|
|
32
|
+
tryAgain(validation) {
|
|
33
|
+
this.numTries++;
|
|
34
|
+
this.needsInput = true;
|
|
35
|
+
this.validation = { ...this.validation, ...validation, success: false };
|
|
36
|
+
}
|
|
37
|
+
nextQuestion() {
|
|
38
|
+
this.numTries = 0;
|
|
39
|
+
this.needsInput = false;
|
|
40
|
+
this.validation = { success: true };
|
|
41
|
+
}
|
|
42
|
+
process(validation) {
|
|
43
|
+
if (typeof validation === 'boolean') {
|
|
44
|
+
if (validation) {
|
|
45
|
+
this.nextQuestion();
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.tryAgain({ type: 'validation' });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
if (validation.success) {
|
|
53
|
+
this.nextQuestion();
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
this.tryAgain(validation);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return this.validation;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
11
62
|
function generatePromptMessage(question, ctx) {
|
|
12
63
|
let promptMessage = '';
|
|
13
64
|
if (question.message) {
|
|
14
65
|
promptMessage = chalk_1.default.whiteBright(question.message) + '\n';
|
|
15
66
|
}
|
|
16
67
|
promptMessage += `${chalk_1.default.white('[')}${chalk_1.default.green('--' + question.name)}${chalk_1.default.white(']:')}\n`;
|
|
17
|
-
|
|
18
|
-
promptMessage = requiredMessage(question) + promptMessage;
|
|
19
|
-
}
|
|
68
|
+
promptMessage = validationMessage(question, ctx) + promptMessage;
|
|
20
69
|
switch (question.type) {
|
|
21
70
|
case 'confirm':
|
|
22
71
|
promptMessage += `(y/n)${question.default !== undefined ? ` [${question.default ? 'y' : 'n'}]` : ''}`;
|
|
@@ -91,65 +140,178 @@ class Inquirerer {
|
|
|
91
140
|
const prompt = this.getPrompt(question, ctx, input);
|
|
92
141
|
this.log(prompt);
|
|
93
142
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
143
|
+
generateManPage(opts) {
|
|
144
|
+
let manPage = `NAME\n\t${opts.commandName} ${opts.description ?? ''}\n\n`;
|
|
145
|
+
// Constructing the SYNOPSIS section with required and optional arguments
|
|
146
|
+
let requiredArgs = '';
|
|
147
|
+
let optionalArgs = '';
|
|
148
|
+
opts.questions.forEach(question => {
|
|
149
|
+
if (question.required) {
|
|
150
|
+
requiredArgs += ` --${question.name} <${question.name}>`;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
optionalArgs += ` [--${question.name}${question.default ? `=${question.default}` : ''}]`;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
manPage += `SYNOPSIS\n\t${opts.commandName}${requiredArgs}${optionalArgs}\n\n`;
|
|
157
|
+
manPage += `DESCRIPTION\n\tUse this command to interact with the application. It supports the following options:\n\n`;
|
|
158
|
+
opts.questions.forEach(question => {
|
|
159
|
+
manPage += `${question.name.toUpperCase()}\n`;
|
|
160
|
+
manPage += `\tType: ${question.type}\n`;
|
|
161
|
+
if (question.message) {
|
|
162
|
+
manPage += `\tSummary: ${question.message}\n`;
|
|
163
|
+
}
|
|
164
|
+
if (question.description) {
|
|
165
|
+
manPage += `\tDescription: ${question.description}\n`;
|
|
166
|
+
}
|
|
167
|
+
if ('options' in question) {
|
|
168
|
+
const optionsList = Array.isArray(question.options)
|
|
169
|
+
? question.options.map(opt => typeof opt === 'string' ? opt : `${opt.name} (${opt.value})`).join(', ')
|
|
170
|
+
: '';
|
|
171
|
+
manPage += `\tOptions: ${optionsList}\n`;
|
|
172
|
+
}
|
|
173
|
+
if (question.default !== undefined) {
|
|
174
|
+
manPage += `\tDefault: ${JSON.stringify(question.default)}\n`;
|
|
175
|
+
}
|
|
176
|
+
if (question.required) {
|
|
177
|
+
manPage += `\tRequired: Yes\n`;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
manPage += `\tRequired: No\n`;
|
|
181
|
+
}
|
|
182
|
+
manPage += '\n';
|
|
183
|
+
});
|
|
184
|
+
manPage += `EXAMPLES\n\tExample usage of \`${opts.commandName}\`.\n\t$ ${opts.commandName}${requiredArgs}${optionalArgs}\n\n`;
|
|
185
|
+
manPage += opts.author ? `AUTHOR\n\t${opts.author}\n` : '';
|
|
186
|
+
return manPage;
|
|
187
|
+
}
|
|
188
|
+
isValidatableAnswer(answer) {
|
|
189
|
+
return answer !== undefined;
|
|
190
|
+
}
|
|
191
|
+
validateAnswer(question, answer, obj, ctx) {
|
|
192
|
+
const validation = this.validateAnswerPattern(question, answer);
|
|
193
|
+
if (!validation.success) {
|
|
194
|
+
return ctx.process(validation);
|
|
195
|
+
}
|
|
196
|
+
if (question.validate) {
|
|
197
|
+
const customValidation = question.validate(answer, obj);
|
|
198
|
+
return ctx.process(customValidation);
|
|
199
|
+
}
|
|
200
|
+
return ctx.process({
|
|
201
|
+
success: true
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
isValid(question, obj, ctx) {
|
|
205
|
+
if (this.isValidatableAnswer(obj[question.name])) {
|
|
206
|
+
obj[question.name] = this.sanitizeAnswer(question, obj[question.name], obj);
|
|
207
|
+
const validationResult = this.validateAnswer(question, obj[question.name], obj, ctx);
|
|
208
|
+
if (!validationResult.success) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
99
211
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
212
|
+
if (question.required && this.isEmptyAnswer(obj[question.name])) {
|
|
213
|
+
ctx.tryAgain({ type: 'required' });
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
validateAnswerPattern(question, answer) {
|
|
219
|
+
if (question.pattern && typeof answer === 'string') {
|
|
220
|
+
const regex = new RegExp(question.pattern);
|
|
221
|
+
const success = regex.test(answer);
|
|
222
|
+
if (success) {
|
|
223
|
+
return {
|
|
224
|
+
success
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
return {
|
|
229
|
+
type: 'pattern',
|
|
230
|
+
success: false,
|
|
231
|
+
reason: question.pattern
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
success: true
|
|
106
237
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
238
|
+
}
|
|
239
|
+
isEmptyAnswer(answer) {
|
|
240
|
+
switch (true) {
|
|
241
|
+
case answer === undefined:
|
|
242
|
+
case answer === null:
|
|
243
|
+
case answer === '':
|
|
244
|
+
case Array.isArray(answer) && answer.length === 0:
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
sanitizeAnswer(question, answer, obj) {
|
|
250
|
+
if (question.sanitize) {
|
|
251
|
+
return question.sanitize(answer, obj);
|
|
252
|
+
}
|
|
253
|
+
return answer;
|
|
254
|
+
}
|
|
255
|
+
async prompt(argv, questions, docs) {
|
|
256
|
+
const obj = { ...argv };
|
|
257
|
+
// Assuming questions is an array of Question objects and argv is the parsed command line arguments object.
|
|
258
|
+
// @ts-ignore
|
|
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
|
+
// @ts-ignore
|
|
268
|
+
if (!questions.some(question => question.required && this.isEmptyAnswer(argv[question.name]))) {
|
|
269
|
+
return obj;
|
|
270
|
+
}
|
|
271
|
+
// If running in a non-interactive mode and any required argument is missing, handle the error.
|
|
272
|
+
if (docs?.usageText) {
|
|
273
|
+
this.log(docs.usageText);
|
|
274
|
+
}
|
|
275
|
+
else if (docs?.manPageInfo) {
|
|
276
|
+
this.log(this.generateManPage(docs.manPageInfo));
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
this.log('Missing required arguments. Please provide all required parameters.');
|
|
280
|
+
}
|
|
281
|
+
process.exit(1);
|
|
282
|
+
}
|
|
283
|
+
for (let index = 0; index < questions.length; index++) {
|
|
110
284
|
const question = questions[index];
|
|
285
|
+
const ctx = new PromptContext();
|
|
286
|
+
// Apply default value if applicable
|
|
111
287
|
if ('default' in question && (this.useDefaults || question.useDefault)) {
|
|
112
288
|
obj[question.name] = question.default;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (needsContinue(question)) {
|
|
120
|
-
switch (question.type) {
|
|
121
|
-
case 'confirm':
|
|
122
|
-
obj[question.name] = await this.confirm(question, ctx);
|
|
123
|
-
break;
|
|
124
|
-
case 'checkbox':
|
|
125
|
-
obj[question.name] = await this.checkbox(question, ctx);
|
|
126
|
-
break;
|
|
127
|
-
case 'autocomplete':
|
|
128
|
-
obj[question.name] = await this.autocomplete(question, ctx);
|
|
129
|
-
break;
|
|
130
|
-
case 'text':
|
|
131
|
-
default:
|
|
132
|
-
obj[question.name] = await this.text(question, ctx); // Use promptText instead of text
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
// Check if the question is required and the response is not adequate
|
|
136
|
-
if (question.required && needsContinue(question)) {
|
|
137
|
-
// Reset the property to undefined to re-trigger the prompt
|
|
138
|
-
numTries++;
|
|
139
|
-
obj[question.name] = undefined;
|
|
140
|
-
continue; // Stay on the same question
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
if ('default' in question) {
|
|
144
|
-
obj[question.name] = question.default;
|
|
145
|
-
}
|
|
289
|
+
continue; // Skip to the next question since the default is applied
|
|
290
|
+
}
|
|
291
|
+
while (ctx.needsInput) {
|
|
292
|
+
obj[question.name] = await this.handleQuestionType(question, ctx);
|
|
293
|
+
if (!this.isValid(question, obj, ctx)) {
|
|
294
|
+
continue;
|
|
146
295
|
}
|
|
296
|
+
// If input passes validation and is not empty, or not required, move to the next question
|
|
297
|
+
ctx.nextQuestion();
|
|
147
298
|
}
|
|
148
|
-
index++; // Move to the next question
|
|
149
|
-
numTries = 0;
|
|
150
299
|
}
|
|
151
300
|
return obj;
|
|
152
301
|
}
|
|
302
|
+
async handleQuestionType(question, ctx) {
|
|
303
|
+
switch (question.type) {
|
|
304
|
+
case 'confirm':
|
|
305
|
+
return this.confirm(question, ctx);
|
|
306
|
+
case 'checkbox':
|
|
307
|
+
return this.checkbox(question, ctx);
|
|
308
|
+
case 'autocomplete':
|
|
309
|
+
return this.autocomplete(question, ctx);
|
|
310
|
+
case 'text':
|
|
311
|
+
default:
|
|
312
|
+
return this.text(question, ctx);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
153
315
|
async confirm(question, ctx) {
|
|
154
316
|
if (this.noTty || !this.rl)
|
|
155
317
|
return question.default ?? false; // Return default if non-interactive
|
|
@@ -170,8 +332,12 @@ class Inquirerer {
|
|
|
170
332
|
});
|
|
171
333
|
}
|
|
172
334
|
async text(question, ctx) {
|
|
173
|
-
if (this.noTty || !this.rl)
|
|
174
|
-
|
|
335
|
+
if (this.noTty || !this.rl) {
|
|
336
|
+
if ('default' in question) {
|
|
337
|
+
return question.default;
|
|
338
|
+
}
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
175
341
|
let input = '';
|
|
176
342
|
return new Promise((resolve) => {
|
|
177
343
|
this.clearScreen();
|
|
@@ -288,8 +454,12 @@ class Inquirerer {
|
|
|
288
454
|
});
|
|
289
455
|
}
|
|
290
456
|
async autocomplete(question, ctx) {
|
|
291
|
-
if (this.noTty || !this.rl)
|
|
292
|
-
|
|
457
|
+
if (this.noTty || !this.rl) {
|
|
458
|
+
if ('default' in question) {
|
|
459
|
+
return question.default;
|
|
460
|
+
}
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
293
463
|
this.keypress.resume();
|
|
294
464
|
const options = this.sanitizeOptions(question);
|
|
295
465
|
let input = '';
|
|
@@ -375,7 +545,7 @@ class Inquirerer {
|
|
|
375
545
|
if (typeof option === 'string') {
|
|
376
546
|
return { name: option, value: option };
|
|
377
547
|
}
|
|
378
|
-
else if (typeof option === 'object' && 'name' in option) {
|
|
548
|
+
else if (typeof option === 'object' && option && 'name' in option) {
|
|
379
549
|
return { name: option.name, value: option.value };
|
|
380
550
|
}
|
|
381
551
|
else {
|
package/question/types.d.ts
CHANGED
|
@@ -6,6 +6,11 @@ export interface OptionValue {
|
|
|
6
6
|
name: string;
|
|
7
7
|
value: any;
|
|
8
8
|
}
|
|
9
|
+
export interface Validation {
|
|
10
|
+
type?: string;
|
|
11
|
+
success: boolean;
|
|
12
|
+
reason?: string;
|
|
13
|
+
}
|
|
9
14
|
export interface BaseQuestion {
|
|
10
15
|
name: string;
|
|
11
16
|
type: string;
|
|
@@ -13,6 +18,10 @@ export interface BaseQuestion {
|
|
|
13
18
|
useDefault?: boolean;
|
|
14
19
|
required?: boolean;
|
|
15
20
|
message?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
validate?: (input: any, obj: any) => Validation | boolean;
|
|
23
|
+
sanitize?: (input: any, obj: any) => any;
|
|
24
|
+
pattern?: string;
|
|
16
25
|
}
|
|
17
26
|
export interface ConfirmQuestion extends BaseQuestion {
|
|
18
27
|
type: 'confirm';
|