1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import { Interface as ReadlineInterface } from 'readline';
|
16 | import { Observable } from 'rxjs';
|
17 | import { ThroughStream } from 'through';
|
18 | import Choice from './lib/objects/choice.js';
|
19 | import Choices from './lib/objects/choices.js';
|
20 | import Separator from './lib/objects/separator.js';
|
21 | import './lib/prompts/base.js';
|
22 | import CheckboxPrompt from './lib/prompts/checkbox.js';
|
23 | import ConfirmPrompt from './lib/prompts/confirm.js';
|
24 | import EditorPrompt from './lib/prompts/editor.js';
|
25 | import ExpandPrompt from './lib/prompts/expand.js';
|
26 | import InputPrompt from './lib/prompts/input.js';
|
27 | import ListPrompt from './lib/prompts/list.js';
|
28 | import NumberPrompt from './lib/prompts/number.js';
|
29 | import PasswordPrompt from './lib/prompts/password.js';
|
30 | import RawListPrompt from './lib/prompts/rawlist.js';
|
31 | import UI from './lib/ui/baseUI.js';
|
32 | import './lib/ui/bottom-bar.js';
|
33 | import './lib/ui/prompt.js';
|
34 | import './lib/utils/events.js';
|
35 | import './lib/utils/paginator.js';
|
36 | import './lib/utils/readline.js';
|
37 | import './lib/utils/screen-manager.js';
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | type LiteralUnion<T extends F, F = string> = T | (F & {});
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export interface PromptFunction {
|
54 | |
55 |
|
56 |
|
57 | <T extends Answers = Answers>(questions: QuestionCollection<T>, initialAnswers?: Partial<T>): Promise<T>;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | export interface PromptModuleBase extends PromptFunction {
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | registerPrompt(name: string, prompt: inquirer.prompts.PromptConstructor): void;
|
74 |
|
75 | |
76 |
|
77 |
|
78 | restoreDefaultPrompts(): void;
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | type RegisterFunction = PromptModuleBase['registerPrompt'];
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | type RestoreFunction = PromptModuleBase['restoreDefaultPrompts'];
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | interface ListQuestionOptionsBase<T extends Answers, TChoiceMap> extends Question<T> {
|
101 | |
102 |
|
103 |
|
104 | choices?: AsyncDynamicQuestionProperty<ReadonlyArray<DistinctChoice<T, TChoiceMap>>, T> | undefined;
|
105 |
|
106 | |
107 |
|
108 |
|
109 | pageSize?: number | undefined;
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | export function createPromptModule(opt?: StreamOptions): PromptModule;
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | type PromptModuleCreator = typeof createPromptModule;
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | export type KeyUnion<T> = LiteralUnion<Extract<keyof T, string>>;
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | export interface Answers extends Record<string, any> { }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | export type Validator<T extends Answers = Answers> = Question<T>['validate'];
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | export type Transformer<T extends Answers = Answers> = InputQuestionOptions<T>['transformer'];
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | export type DynamicQuestionProperty<T, TAnswers extends Answers = Answers> = T | ((answers: TAnswers) => T);
|
175 |
|
176 | /**
|
177 | * Represents a dynamic property for a question which can be fetched asynchronously.
|
178 | *
|
179 | * @template T
|
180 | * The type of the property.
|
181 | *
|
182 | * @template TAnswers
|
183 | * The type of the answers.
|
184 | */
|
185 | export type AsyncDynamicQuestionProperty<T, TAnswers extends Answers = Answers> = DynamicQuestionProperty<
|
186 | T | Promise<T>,
|
187 | TAnswers
|
188 | >;
|
189 |
|
190 | /**
|
191 | * Represents a choice-item.
|
192 | */
|
193 | export interface ChoiceBase {
|
194 | /**
|
195 | * The type of the choice.
|
196 | */
|
197 | type?: string | undefined;
|
198 | }
|
199 |
|
200 | /**
|
201 | * Provides options for a choice of the {@link ListPrompt `ListPrompt<TQuestion>`}.
|
202 | *
|
203 | * @template T
|
204 | * The type of the answers.
|
205 | */
|
206 | export interface ListChoiceOptions<T extends Answers = Answers> extends ChoiceOptions {
|
207 | /**
|
208 | * A value indicating whether the choice is disabled.
|
209 | */
|
210 | disabled?: DynamicQuestionProperty<boolean | string, T> | undefined;
|
211 | }
|
212 |
|
213 | /**
|
214 | * Provides options for a choice of the {@link CheckboxPrompt `CheckboxPrompt<TQuestion>`}.
|
215 | *
|
216 | * @template T
|
217 | * The type of the answers.
|
218 | */
|
219 | export interface CheckboxChoiceOptions<T extends Answers = Answers> extends ListChoiceOptions<T> {
|
220 | /**
|
221 | * A value indicating whether the choice should be initially checked.
|
222 | */
|
223 | checked?: boolean | undefined;
|
224 | }
|
225 |
|
226 | /**
|
227 | * Provides options for a choice of the {@link ExpandPrompt `ExpandPrompt<TQuestion>`}.
|
228 | */
|
229 | export interface ExpandChoiceOptions extends ChoiceOptions {
|
230 | /**
|
231 | * The key to press for selecting the choice.
|
232 | */
|
233 | key?: string | undefined;
|
234 | }
|
235 |
|
236 | /**
|
237 | * Represents a separator.
|
238 | */
|
239 | export interface SeparatorOptions extends ChoiceBase {
|
240 | /**
|
241 | * Gets the type of the choice.
|
242 | */
|
243 | type: 'separator';
|
244 |
|
245 | /**
|
246 | * Gets or sets the text of the separator.
|
247 | */
|
248 | line?: string | undefined;
|
249 | }
|
250 |
|
251 | /**
|
252 | * Provides options for a choice.
|
253 | */
|
254 | export interface ChoiceOptions extends ChoiceBase {
|
255 | /**
|
256 | * @inheritdoc
|
257 | */
|
258 | type?: 'choice' | undefined;
|
259 |
|
260 | /**
|
261 | * The name of the choice to show to the user.
|
262 | */
|
263 | name?: string | undefined;
|
264 |
|
265 | /**
|
266 | * The value of the choice.
|
267 | */
|
268 | value?: any;
|
269 |
|
270 | /**
|
271 | * The short form of the name of the choice.
|
272 | */
|
273 | short?: string | undefined;
|
274 |
|
275 | /**
|
276 | * The extra properties of the choice.
|
277 | */
|
278 | extra?: any;
|
279 | }
|
280 |
|
281 | /**
|
282 | * Provides all valid choice-types for any kind of question.
|
283 | *
|
284 | * @template T
|
285 | * The type of the answers.
|
286 | */
|
287 | export interface BaseChoiceMap<T extends Answers = Answers> {
|
288 | Choice: Choice<T>;
|
289 | ChoiceOptions: ChoiceOptions;
|
290 | Separator: Separator;
|
291 | SeparatorOptions: SeparatorOptions;
|
292 | }
|
293 |
|
294 | /**
|
295 | * Provides all valid choice-types for the {@link ListQuestion `ListQuestion<T>`}.
|
296 | *
|
297 | * @template T
|
298 | * The type of the answers.
|
299 | */
|
300 | export interface ListChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
|
301 | ListChoiceOptions: ListChoiceOptions<T>;
|
302 | }
|
303 |
|
304 | /**
|
305 | * Provides all valid choice-types for the {@link CheckboxQuestion `CheckboxQuestion<T>`}.
|
306 | *
|
307 | * @template T
|
308 | * The type of the answers.
|
309 | */
|
310 | export interface CheckboxChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
|
311 | CheckboxChoiceOptions: CheckboxChoiceOptions<T>;
|
312 | }
|
313 |
|
314 | /**
|
315 | * Provides all valid choice-types for the {@link ExpandQuestion `ExpandQuestion<T>`}.
|
316 | *
|
317 | * @template T
|
318 | * The type of the answers.
|
319 | */
|
320 | export interface ExpandChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
|
321 | ExpandChoiceOptions: ExpandChoiceOptions;
|
322 | }
|
323 |
|
324 | /**
|
325 | * Provides all valid choice-types.
|
326 | *
|
327 | * @template T
|
328 | * The type of the answers.
|
329 | */
|
330 | export interface AllChoiceMap<T extends Answers = Answers> {
|
331 | BaseChoiceMap: BaseChoiceMap<T>[keyof BaseChoiceMap<T>];
|
332 | ListChoiceMap: ListChoiceMap<T>[keyof ListChoiceMap<T>];
|
333 | CheckboxChoiceMap: CheckboxChoiceMap<T>[keyof CheckboxChoiceMap<T>];
|
334 | ExpandChoiceMap: ExpandChoiceMap<T>[keyof ExpandChoiceMap<T>];
|
335 | }
|
336 |
|
337 | /**
|
338 | * Provides valid choices for the question of the {@link TChoiceMap `TChoiceMap`}.
|
339 | *
|
340 | * @template TAnswers
|
341 | * The type of the answers.
|
342 | *
|
343 | * @template TChoiceMap
|
344 | * The choice-types to provide.
|
345 | */
|
346 | export type DistinctChoice<TAnswers extends Answers = Answers, TChoiceMap = AllChoiceMap<TAnswers>> =
|
347 | | string
|
348 | | TChoiceMap[keyof TChoiceMap];
|
349 |
|
350 | /**
|
351 | * Represents a set of choices.
|
352 | *
|
353 | * @template T
|
354 | * The type of the answers.
|
355 | */
|
356 | export type ChoiceCollection<T extends Answers = Answers> = Array<DistinctChoice<T, AllChoiceMap<T>>>;
|
357 |
|
358 | /**
|
359 | * Provides options for a question.
|
360 | *
|
361 | * @template T
|
362 | * The type of the answers.
|
363 | */
|
364 | export interface Question<T extends Answers = Answers> {
|
365 | /**
|
366 | * The type of the question.
|
367 | */
|
368 | type?: string | undefined;
|
369 |
|
370 | /**
|
371 | * The key to save the answer to the answers-hash.
|
372 | */
|
373 | name?: KeyUnion<T> | undefined;
|
374 |
|
375 | /**
|
376 | * The message to show to the user.
|
377 | */
|
378 | message?: AsyncDynamicQuestionProperty<string, T> | undefined;
|
379 |
|
380 | /**
|
381 | * The default value of the question.
|
382 | */
|
383 | default?: AsyncDynamicQuestionProperty<any, T> | undefined;
|
384 |
|
385 | /**
|
386 | * The prefix of the {@link message `message`}.
|
387 | */
|
388 | prefix?: string | undefined;
|
389 |
|
390 | /**
|
391 | * The suffix of the {@link message `message`}.
|
392 | */
|
393 | suffix?: string | undefined;
|
394 |
|
395 | /**
|
396 | * Post-processes the answer.
|
397 | *
|
398 | * @param input
|
399 | * The answer provided by the user.
|
400 | *
|
401 | * @param answers
|
402 | * The answers provided by the user.
|
403 | */
|
404 | filter?(input: any, answers: T): any;
|
405 |
|
406 | /**
|
407 | * A value indicating whether the question should be prompted.
|
408 | */
|
409 | when?: AsyncDynamicQuestionProperty<boolean, T> | undefined;
|
410 |
|
411 | /**
|
412 | * Validates the integrity of the answer.
|
413 | *
|
414 | * @param input
|
415 | * The answer provided by the user.
|
416 | *
|
417 | * @param answers
|
418 | * The answers provided by the user.
|
419 | *
|
420 | * @returns
|
421 | * Either a value indicating whether the answer is valid or a {@link String `string`} which describes the error.
|
422 | */
|
423 | validate?(input: any, answers?: T): boolean | string | Promise<boolean | string>;
|
424 |
|
425 | /**
|
426 | * Force to prompt the question if the answer already exists.
|
427 | */
|
428 | askAnswered?: boolean;
|
429 | }
|
430 |
|
431 | /**
|
432 | * Represents the possible answers of each question in the prompt
|
433 | */
|
434 | export type QuestionAnswer<T extends Answers = Answers> = {
|
435 | [K in keyof T]: {
|
436 | name: K;
|
437 | answer: T[K]
|
438 | }
|
439 | }[keyof T];
|
440 |
|
441 | /**
|
442 | * Provides options for a question for the {@link InputPrompt `InputPrompt<TQuestion>`}.
|
443 | *
|
444 | * @template T
|
445 | * The type of the answers.
|
446 | */
|
447 | export interface InputQuestionOptions<T extends Answers = Answers> extends Question<T> {
|
448 | /**
|
449 | * Transforms the value to display to the user.
|
450 | *
|
451 | * @param input
|
452 | * The input provided by the user.
|
453 | *
|
454 | * @param answers
|
455 | * The answers provided by the users.
|
456 | *
|
457 | * @param flags
|
458 | * Additional information about the value.
|
459 | *
|
460 | * @returns
|
461 | * The value to display to the user.
|
462 | */
|
463 | transformer?(input: any, answers: T, flags: { isFinal?: boolean | undefined }): string | Promise<string>;
|
464 | }
|
465 |
|
466 | /**
|
467 | * Provides options for a question for the {@link InputPrompt `InputPrompt<TQuestion>`}.
|
468 | *
|
469 | * @template T
|
470 | * The type of the answers.
|
471 | */
|
472 | export interface InputQuestion<T extends Answers = Answers> extends InputQuestionOptions<T> {
|
473 | /**
|
474 | * @inheritdoc
|
475 | */
|
476 | type?: 'input' | undefined;
|
477 | }
|
478 |
|
479 | /**
|
480 | * Provides options for a question for the {@link NumberPrompt `NumberPrompt<TQuestion>`}.
|
481 | *
|
482 | * @template T
|
483 | * The type of the answers.
|
484 | */
|
485 | export interface NumberQuestionOptions<T extends Answers = Answers> extends InputQuestionOptions<T> { }
|
486 |
|
487 | /**
|
488 | * Provides options for a question for the {@link NumberPrompt `NumberPrompt<TQuestion>`}.
|
489 | *
|
490 | * @template T
|
491 | * The type of the answers.
|
492 | */
|
493 | export interface NumberQuestion<T extends Answers = Answers> extends NumberQuestionOptions<T> {
|
494 | /**
|
495 | * @inheritdoc
|
496 | */
|
497 | type: 'number';
|
498 | }
|
499 |
|
500 | /**
|
501 | * Provides options for a question for the {@link PasswordPrompt `PasswordPrompt<TQuestion>`}.
|
502 | *
|
503 | * @template T
|
504 | * The type of the answers.
|
505 | */
|
506 | export interface PasswordQuestionOptions<T extends Answers = Answers> extends InputQuestionOptions<T> {
|
507 | /**
|
508 | * The character to replace the user-input.
|
509 | */
|
510 | mask?: string | undefined;
|
511 | }
|
512 |
|
513 | /**
|
514 | * Provides options for a question for the {@link PasswordPrompt `PasswordPrompt<TQuestion>`}.
|
515 | *
|
516 | * @template T
|
517 | * The type of the answers.
|
518 | */
|
519 | export interface PasswordQuestion<T extends Answers = Answers> extends PasswordQuestionOptions<T> {
|
520 | /**
|
521 | * @inheritdoc
|
522 | */
|
523 | type: 'password';
|
524 | }
|
525 |
|
526 | /**
|
527 | * Represents a list-based question that can loop.
|
528 | *
|
529 | * @template T
|
530 | * The type of the answers.
|
531 | *
|
532 | * @template TChoiceMap
|
533 | * The valid choices for the question.
|
534 | */
|
535 | interface LoopableListQuestionOptionsBase<T extends Answers, TChoiceMap> extends ListQuestionOptionsBase<T, TChoiceMap> {
|
536 | /**
|
537 | * A value indicating whether choices in a list should be looped.
|
538 | */
|
539 | loop?: boolean | undefined;
|
540 | }
|
541 |
|
542 | /**
|
543 | * Provides options for a question for the {@link ListPrompt `ListPrompt<TQuestion>`}.
|
544 | *
|
545 | * @template T
|
546 | * The type of the answers.
|
547 | */
|
548 | export interface ListQuestionOptions<T extends Answers = Answers>
|
549 | extends LoopableListQuestionOptionsBase<T, ListChoiceMap<T>> { }
|
550 |
|
551 | /**
|
552 | * Provides options for a question for the {@link ListPrompt `ListPrompt<TQuestion>`}.
|
553 | *
|
554 | * @template T
|
555 | * The type of the answers.
|
556 | */
|
557 | export interface ListQuestion<T extends Answers = Answers> extends ListQuestionOptions<T> {
|
558 | /**
|
559 | * @inheritdoc
|
560 | */
|
561 | type: 'list';
|
562 | }
|
563 |
|
564 | /**
|
565 | * Provides options for a question for the {@link RawListPrompt `RawListPrompt<TQuestion>`}.
|
566 | *
|
567 | * @template T
|
568 | * The type of the answers.
|
569 | */
|
570 | export interface RawListQuestionOptions<T extends Answers = Answers> extends ListQuestionOptions<T> { }
|
571 |
|
572 | /**
|
573 | * Provides options for a question for the {@link RawListPrompt `RawListPrompt<TQuestion>`}.
|
574 | *
|
575 | * @template T
|
576 | * The type of the answers.
|
577 | */
|
578 | export interface RawListQuestion<T extends Answers = Answers> extends RawListQuestionOptions<T> {
|
579 | /**
|
580 | * @inheritdoc
|
581 | */
|
582 | type: 'rawlist';
|
583 | }
|
584 |
|
585 | /**
|
586 | * Provides options for a question for the {@link ExpandPrompt `ExpandPrompt<TQuestion>`}.
|
587 | *
|
588 | * @template T
|
589 | * The type of the answers.
|
590 | */
|
591 | export interface ExpandQuestionOptions<T extends Answers = Answers>
|
592 | extends ListQuestionOptionsBase<T, ExpandChoiceMap<T>> { }
|
593 |
|
594 | /**
|
595 | * Provides options for a question for the {@link ExpandPrompt `ExpandPrompt<TQuestion>`}.
|
596 | *
|
597 | * @template T
|
598 | * The type of the answers.
|
599 | */
|
600 | export interface ExpandQuestion<T extends Answers = Answers> extends ExpandQuestionOptions<T> {
|
601 | /**
|
602 | * @inheritdoc
|
603 | */
|
604 | type: 'expand';
|
605 | }
|
606 |
|
607 | /**
|
608 | * Provides options for a question for the {@link CheckboxPrompt `CheckboxPrompt<TQuestion>`}.
|
609 | *
|
610 | * @template T
|
611 | * The type of the answers.
|
612 | */
|
613 | export interface CheckboxQuestionOptions<T extends Answers = Answers>
|
614 | extends LoopableListQuestionOptionsBase<T, CheckboxChoiceMap<T>> { }
|
615 |
|
616 | /**
|
617 | * Provides options for a question for the {@link CheckboxPrompt `CheckboxPrompt<TQuestion>`}.
|
618 | *
|
619 | * @template T
|
620 | * The type of the answers.
|
621 | */
|
622 | export interface CheckboxQuestion<T extends Answers = Answers> extends CheckboxQuestionOptions<T> {
|
623 | /**
|
624 | * @inheritdoc
|
625 | */
|
626 | type: 'checkbox';
|
627 | }
|
628 |
|
629 | /**
|
630 | * Provides options for a question for the {@link ConfirmPrompt `ConfirmPrompt<TQuestion>`}.
|
631 | *
|
632 | * @template T
|
633 | * The type of the answers.
|
634 | */
|
635 | export interface ConfirmQuestionOptions<T extends Answers = Answers> extends Question<T> { }
|
636 |
|
637 | /**
|
638 | * Provides options for a question for the {@link ConfirmPrompt `ConfirmPrompt<TQuestion>`}.
|
639 | *
|
640 | * @template T
|
641 | * The type of the answers.
|
642 | */
|
643 | export interface ConfirmQuestion<T extends Answers = Answers> extends ConfirmQuestionOptions<T> {
|
644 | /**
|
645 | * @inheritdoc
|
646 | */
|
647 | type: 'confirm';
|
648 | }
|
649 |
|
650 | /**
|
651 | * Provides options for a question for the {@link EditorPrompt `EditorPrompt<TQuestion>`}.
|
652 | *
|
653 | * @template T
|
654 | * The type of the answers.
|
655 | */
|
656 | export interface EditorQuestionOptions<T extends Answers = Answers> extends Question<T> {
|
657 | /**
|
658 | * The postfix of the file being edited.
|
659 | *
|
660 | * Adding this will add color highlighting to the file content in most editors.
|
661 | */
|
662 | postfix?: string;
|
663 | }
|
664 |
|
665 | /**
|
666 | * Provides options for a question for the {@link EditorPrompt `EditorPrompt<TQuestion>`}.
|
667 | *
|
668 | * @template T
|
669 | * The type of the answers.
|
670 | */
|
671 | export interface EditorQuestion<T extends Answers = Answers> extends EditorQuestionOptions<T> {
|
672 | /**
|
673 | * @inheritdoc
|
674 | */
|
675 | type: 'editor';
|
676 | }
|
677 |
|
678 | /**
|
679 | * Provides the available question-types.
|
680 | *
|
681 | * @template T
|
682 | * The type of the answers.
|
683 | */
|
684 | export interface QuestionMap<T extends Answers = Answers> {
|
685 | /**
|
686 | * The {@link InputQuestion `InputQuestion<T>`} type.
|
687 | */
|
688 | input: InputQuestion<T>;
|
689 |
|
690 | /**
|
691 | * The {@link NumberQuestion `NumberQuestion<T>`} type.
|
692 | */
|
693 | number: NumberQuestion<T>;
|
694 |
|
695 | /**
|
696 | * The {@link PasswordQuestion `PasswordQuestion<T>`} type.
|
697 | */
|
698 | password: PasswordQuestion<T>;
|
699 |
|
700 | /**
|
701 | * The {@link ListQuestion `ListQuestion<T>`} type.
|
702 | */
|
703 | list: ListQuestion<T>;
|
704 |
|
705 | /**
|
706 | * The {@link RawListQuestion `RawListQuestion<T>`} type.
|
707 | */
|
708 | rawList: RawListQuestion<T>;
|
709 |
|
710 | /**
|
711 | * The {@link ExpandQuestion `ExpandQuestion<T>`} type.
|
712 | */
|
713 | expand: ExpandQuestion<T>;
|
714 |
|
715 | /**
|
716 | * The {@link CheckboxQuestion `CheckboxQuestion<T>`} type.
|
717 | */
|
718 | checkbox: CheckboxQuestion<T>;
|
719 |
|
720 | /**
|
721 | * The {@link ConfirmQuestion `ConfirmQuestion<T>`} type.
|
722 | */
|
723 | confirm: ConfirmQuestion<T>;
|
724 |
|
725 | /**
|
726 | * The {@link EditorQuestion `EditorQuestion<T>`} type.
|
727 | */
|
728 | editor: EditorQuestion<T>;
|
729 | }
|
730 |
|
731 | /**
|
732 | * Represents one of the available questions.
|
733 | *
|
734 | * @template T
|
735 | * The type of the answers.
|
736 | */
|
737 | export type DistinctQuestion<T extends Answers = Answers> = QuestionMap<T>[keyof QuestionMap<T>];
|
738 |
|
739 | /**
|
740 | * Indicates the type of a question
|
741 | */
|
742 | export type QuestionTypeName = DistinctQuestion['type'];
|
743 |
|
744 | /**
|
745 | * Represents a collection of questions.
|
746 | *
|
747 | * @template T
|
748 | * The type of the answers.
|
749 | */
|
750 | export type QuestionCollection<T extends Answers = Answers> =
|
751 | | DistinctQuestion<T>
|
752 | | ReadonlyArray<DistinctQuestion<T>>
|
753 | | Observable<DistinctQuestion<T>>
|
754 | | { [P in KeyUnion<T>]?: DistinctQuestion<T> & { name?: never } };
|
755 |
|
756 | /**
|
757 | * Provides an input and an output-stream.
|
758 | */
|
759 | export interface StreamOptions {
|
760 | /**
|
761 | * A stream to read the input from.
|
762 | */
|
763 | input?: NodeJS.ReadStream | undefined;
|
764 |
|
765 | /**
|
766 | * A stream to write the output to.
|
767 | */
|
768 | output?: NodeJS.WriteStream | undefined;
|
769 |
|
770 | /**
|
771 | * Whether to display prompts if input is not a TTY.
|
772 | */
|
773 | skipTTYChecks?: boolean | undefined;
|
774 | }
|
775 |
|
776 | /**
|
777 | * Provides the functionality to prompt questions to the user.
|
778 | */
|
779 | export interface PromptModule extends PromptModuleBase {
|
780 | /**
|
781 | * The prompts of the prompt-module.
|
782 | */
|
783 | prompts: inquirer.prompts.PromptCollection;
|
784 |
|
785 | /**
|
786 | * Prompts the questions to the user.
|
787 | */
|
788 | <T extends Answers = Answers>(questions: QuestionCollection<T>, initialAnswers?: Partial<T>): Promise<T> & { ui: inquirer.ui.Prompt<T> };
|
789 |
|
790 | /**
|
791 | * Registers a new prompt-type.
|
792 | *
|
793 | * @param name
|
794 | * The name of the prompt.
|
795 | *
|
796 | * @param prompt
|
797 | * The constructor of the prompt.
|
798 | */
|
799 | registerPrompt(name: string, prompt: inquirer.prompts.PromptConstructor): this;
|
800 | }
|
801 |
|
802 | /**
|
803 | * Provides the functionality to prompt questions.
|
804 | */
|
805 | declare namespace inquirer {
|
806 | /**
|
807 | * Provides components for the prompts.
|
808 | */
|
809 | namespace prompts {
|
810 | /**
|
811 | * Provides options for a prompt.
|
812 | *
|
813 | * @template T
|
814 | * The type of the answers.
|
815 | */
|
816 | type PromptOptions<T extends Question = Question> = T & {
|
817 | /**
|
818 | * The choices of the prompt.
|
819 | */
|
820 | choices: Choices;
|
821 | };
|
822 |
|
823 | /**
|
824 | * Represents the state of a prompt.
|
825 | */
|
826 | type PromptState = LiteralUnion<'pending' | 'idle' | 'loading' | 'answered' | 'done'>;
|
827 |
|
828 | /**
|
829 | * Represents a prompt.
|
830 | */
|
831 | interface PromptBase {
|
832 | /**
|
833 | * Gets or sets a string which represents the state of the prompt.
|
834 | */
|
835 | status: PromptState;
|
836 |
|
837 | /**
|
838 | * Runs the prompt.
|
839 | *
|
840 | * @returns
|
841 | * The result of the prompt.
|
842 | */
|
843 | run(): Promise<any>;
|
844 | }
|
845 |
|
846 | /**
|
847 | * Provides the functionality to initialize new prompts.
|
848 | */
|
849 | interface PromptConstructor {
|
850 | /**
|
851 | * Initializes a new instance of a prompt.
|
852 | *
|
853 | * @param question
|
854 | * The question to prompt.
|
855 | *
|
856 | * @param readLine
|
857 | * An object for reading from the command-line.
|
858 | *
|
859 | * @param answers
|
860 | * The answers provided by the user.
|
861 | */
|
862 | new(question: any, readLine: ReadlineInterface, answers: Answers): PromptBase;
|
863 | }
|
864 |
|
865 | /**
|
866 | * Provides a set of prompt-constructors.
|
867 | */
|
868 | type PromptCollection = Record<string, PromptConstructor>;
|
869 |
|
870 | /**
|
871 | * Provides data about the state of a prompt.
|
872 | */
|
873 | interface PromptStateData {
|
874 | /**
|
875 | * Either a {@link String `string`} which describes the error of the prompt or a {@link Boolean `boolean`} indicating whether the prompt-value is valid.
|
876 | */
|
877 | isValid: string | boolean;
|
878 | }
|
879 |
|
880 | /**
|
881 | * Provides data about the successful state of a prompt.
|
882 | *
|
883 | * @param T
|
884 | * The type of the answer.
|
885 | */
|
886 | interface SuccessfulPromptStateData<T = any> extends PromptStateData {
|
887 | /**
|
888 | * @inheritdoc
|
889 | */
|
890 | isValid: true;
|
891 |
|
892 | /**
|
893 | * The value of the prompt.
|
894 | */
|
895 | value: T;
|
896 | }
|
897 |
|
898 | /**
|
899 | * Provides data about the failed state of a prompt.
|
900 | */
|
901 | interface FailedPromptStateData extends PromptStateData {
|
902 | /**
|
903 | * @inheritdoc
|
904 | */
|
905 | isValid: false | string;
|
906 | }
|
907 |
|
908 | /**
|
909 | * Provides pipes for handling events of a prompt.
|
910 | *
|
911 | * @param T
|
912 | * The type of the answer.
|
913 | */
|
914 | interface PromptEventPipes<T = any> {
|
915 | /**
|
916 | * A pipeline for successful inputs.
|
917 | */
|
918 | success: Observable<SuccessfulPromptStateData<T>>;
|
919 |
|
920 | /**
|
921 | * An object representing an error.
|
922 | */
|
923 | error: Observable<FailedPromptStateData>;
|
924 | }
|
925 | }
|
926 |
|
927 | /**
|
928 | * Provides components for the ui.
|
929 | */
|
930 | namespace ui {
|
931 | /**
|
932 | * Represents the bottom-bar UI.
|
933 | */
|
934 | class BottomBar extends UI {
|
935 | /**
|
936 | * Gets or sets a stream to write logs to.
|
937 | */
|
938 | log: ThroughStream;
|
939 |
|
940 | /**
|
941 | * Initializes a new instance of the {@link BottomBar `BottomBar`} class.
|
942 | *
|
943 | * @param options
|
944 | * Provides options for the bottom-bar ui.
|
945 | */
|
946 | constructor(options?: BottomBarOptions);
|
947 |
|
948 | /**
|
949 | * Renders the specified {@link text `text`} to the bottom bar.
|
950 | *
|
951 | * @param text
|
952 | * The text to print to the bottom bar.
|
953 | */
|
954 | updateBottomBar(text: string): this;
|
955 |
|
956 | /**
|
957 | * Renders the bottom bar.
|
958 | */
|
959 | protected render(): this;
|
960 |
|
961 | /**
|
962 | * Cleans the bottom bar.
|
963 | */
|
964 | protected clean(): this;
|
965 |
|
966 | /**
|
967 | * Writes a message to the bottom bar.
|
968 | *
|
969 | * @param message
|
970 | * The message to write.
|
971 | */
|
972 | protected write(message: string): void;
|
973 |
|
974 | /**
|
975 | * Writes the specified {@link data `data`} to the log-zone.
|
976 | *
|
977 | * @param data
|
978 | * The data to write to the log-zone.
|
979 | */
|
980 | protected writeLog(data: any): this;
|
981 |
|
982 | /**
|
983 | * Fixes the new-line characters of the specified {@link text `text`}.
|
984 | *
|
985 | * @param text
|
986 | * The text to process.
|
987 | */
|
988 | protected enforceLF(text: string): string;
|
989 | }
|
990 |
|
991 | /**
|
992 | * Represents the prompt ui.
|
993 | */
|
994 | class Prompt<T extends Answers = Answers> extends UI {
|
995 | /**
|
996 | * Gets or sets the prompts of the ui.
|
997 | */
|
998 | prompts: prompts.PromptCollection;
|
999 |
|
1000 | /**
|
1001 | * Gets or sets the answers provided by the user.
|
1002 | */
|
1003 | answers: T;
|
1004 |
|
1005 | /**
|
1006 | * Gets or sets the event-flow of the process.
|
1007 | */
|
1008 | process: Observable<QuestionAnswer<T>>;
|
1009 |
|
1010 | /**
|
1011 | * Initializes a new instance of the {@link Prompt `Prompt`} class.
|
1012 | *
|
1013 | * @param prompts
|
1014 | * The prompts for the ui.
|
1015 | *
|
1016 | * @param options
|
1017 | * The input- and output-stream of the ui.
|
1018 | */
|
1019 | constructor(prompts: prompts.PromptCollection, options?: StreamOptions);
|
1020 |
|
1021 | /**
|
1022 | * Runs the prompt-UI.
|
1023 | *
|
1024 | * @param questions
|
1025 | * The questions to prompt the user to answer.
|
1026 | *
|
1027 | * @returns
|
1028 | * The answers provided by the user.
|
1029 | */
|
1030 | run(questions: Array<DistinctQuestion<T>>): Promise<T>;
|
1031 |
|
1032 | /**
|
1033 | * Finishes the process.
|
1034 | */
|
1035 | protected onCompletion(): T;
|
1036 |
|
1037 | /**
|
1038 | * Processes a question.
|
1039 | *
|
1040 | * @param question
|
1041 | * The question to process.
|
1042 | *
|
1043 | * @returns
|
1044 | * The answer to the question.
|
1045 | */
|
1046 | protected processQuestion(
|
1047 | question: DistinctQuestion<T>,
|
1048 | ): Observable<FetchedAnswer>;
|
1049 |
|
1050 | /**
|
1051 | * Fetches the answer to a question.
|
1052 | *
|
1053 | * @param question
|
1054 | * The question to fetch the answer for.
|
1055 | *
|
1056 | * @returns
|
1057 | * The answer to the question.
|
1058 | */
|
1059 | protected fetchAnswer(
|
1060 | question: FetchedQuestion<T>,
|
1061 | ): Observable<FetchedAnswer>;
|
1062 |
|
1063 | /**
|
1064 | * Sets the type of the question if no question-type is specified.
|
1065 | *
|
1066 | * @param question
|
1067 | * The question to set the default type for.
|
1068 | *
|
1069 | * @returns
|
1070 | * The processed question.
|
1071 | */
|
1072 | protected setDefaultType(
|
1073 | question: DistinctQuestion<T>,
|
1074 | ): Observable<DistinctQuestion<T>>;
|
1075 |
|
1076 | /**
|
1077 | * Filters the question if it is runnable.
|
1078 | *
|
1079 | * @param question
|
1080 | * The question to filter.
|
1081 | *
|
1082 | * @returns
|
1083 | * Either the event-flow of the question if it is runnable or an empty event-flow.
|
1084 | */
|
1085 | protected filterIfRunnable(
|
1086 | question: DistinctQuestion<T>,
|
1087 | ): Observable<DistinctQuestion<T>>;
|
1088 | }
|
1089 |
|
1090 | /**
|
1091 | * Provides options for the bottom-bar UI.
|
1092 | */
|
1093 | interface BottomBarOptions extends StreamOptions {
|
1094 | /**
|
1095 | * The initial text to display.
|
1096 | */
|
1097 | bottomBar?: string | undefined;
|
1098 | }
|
1099 |
|
1100 | /**
|
1101 | * Represents a fetched answer.
|
1102 | *
|
1103 | * @template T
|
1104 | * The type of the answers.
|
1105 | */
|
1106 | type FetchedQuestion<T extends Answers = Answers> = DistinctQuestion<T> & {
|
1107 | /**
|
1108 | * The type of the question.
|
1109 | */
|
1110 | type: string;
|
1111 |
|
1112 | /**
|
1113 | * The message to show to the user.
|
1114 | */
|
1115 | message: string;
|
1116 |
|
1117 | /**
|
1118 | * The default value of the question.
|
1119 | */
|
1120 | default: any;
|
1121 |
|
1122 | /**
|
1123 | * The choices of the question.
|
1124 | */
|
1125 | choices: ChoiceCollection<T>;
|
1126 | };
|
1127 |
|
1128 | /**
|
1129 | * Represents a fetched answer.
|
1130 | */
|
1131 | interface FetchedAnswer {
|
1132 | /**
|
1133 | * The name of the answer.
|
1134 | */
|
1135 | name: string;
|
1136 |
|
1137 | /**
|
1138 | * The value of the answer.
|
1139 | */
|
1140 | answer: any;
|
1141 | }
|
1142 | }
|
1143 |
|
1144 | /**
|
1145 | * Represents a choice-item separator.
|
1146 | */
|
1147 | class Separator implements SeparatorOptions {
|
1148 | /**
|
1149 | * @inheritdoc
|
1150 | */
|
1151 | readonly type: 'separator';
|
1152 |
|
1153 | /**
|
1154 | * @inheritdoc
|
1155 | */
|
1156 | line: string;
|
1157 |
|
1158 | /**
|
1159 | * Initializes a new instance of the {@link Separator `Separator`} class.
|
1160 | *
|
1161 | * @param line
|
1162 | * The text of the separator.
|
1163 | */
|
1164 | constructor(line?: string);
|
1165 |
|
1166 | /**
|
1167 | * Checks whether the specified {@link item `item`} is not a separator.
|
1168 | *
|
1169 | * @param item
|
1170 | * The item to check.
|
1171 | *
|
1172 | * @returns
|
1173 | * A value indicating whether the item is not a separator.
|
1174 | */
|
1175 | static exclude(item: any): boolean;
|
1176 | }
|
1177 |
|
1178 | /**
|
1179 | * Registers a new prompt-type.
|
1180 | *
|
1181 | * @param name
|
1182 | * The name of the prompt.
|
1183 | *
|
1184 | * @param prompt
|
1185 | * The constructor of the prompt.
|
1186 | */
|
1187 | let registerPrompt: RegisterFunction;
|
1188 |
|
1189 | /**
|
1190 | * Registers the default prompts.
|
1191 | */
|
1192 | let restoreDefaultPrompts: RestoreFunction;
|
1193 |
|
1194 | /**
|
1195 | * Creates a prompt-module.
|
1196 | *
|
1197 | * @param opt
|
1198 | * The streams for the prompt-module.
|
1199 | *
|
1200 | * @returns
|
1201 | * The new prompt-module.
|
1202 | */
|
1203 | let createPromptModule: PromptModuleCreator;
|
1204 |
|
1205 | /**
|
1206 | * The default prompt-module.
|
1207 | */
|
1208 | let prompt: PromptModule;
|
1209 | }
|
1210 |
|
1211 | export default inquirer;
|
1212 |
|
\ | No newline at end of file |