UNPKG

24.4 kBTypeScriptView Raw
1// Type definitions for inquirer 6.5
2// Project: https://github.com/SBoudrias/Inquirer.js
3// Definitions by: Qubo <https://github.com/tkQubo>
4// Parvez <https://github.com/ppathan>
5// Jouderian <https://github.com/jouderianjr>
6// Qibang <https://github.com/bang88>
7// Jason Dreyzehner <https://github.com/bitjson>
8// Synarque <https://github.com/synarque>
9// Justin Rockwood <https://github.com/jrockwood>
10// Keith Kelly <https://github.com/kwkelly>
11// Richard Lea <https://github.com/chigix>
12// Jed Mao <https://github.com/jedmao>
13// Manuel Thalmann <https://github.com/manuth>
14// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
15// TypeScript Version: 3.3
16import { Interface as ReadlineInterface } from "readline";
17import { Observable } from "rxjs";
18import { ThroughStream } from "through";
19import Choice = require("./lib/objects/choice");
20import Choices = require("./lib/objects/choices");
21import Separator = require("./lib/objects/separator");
22import Prompt = require("./lib/prompts/base");
23import "./lib/prompts/checkbox";
24import "./lib/prompts/confirm";
25import "./lib/prompts/editor";
26import "./lib/prompts/expand";
27import "./lib/prompts/input";
28import "./lib/prompts/list";
29import "./lib/prompts/number";
30import "./lib/prompts/password";
31import "./lib/prompts/rawlist";
32import BottomBar = require("./lib/ui/bottom-bar");
33import PromptUI = require("./lib/ui/prompt");
34import "./lib/utils/events";
35import Paginator = require("./lib/utils/paginator");
36import "./lib/utils/readline";
37import ScreenManager = require("./lib/utils/screen-manager");
38import "./lib/utils/utils";
39
40/**
41 * Represents a union which preserves autocompletion.
42 *
43 * @template T
44 * The keys which are available for autocompletion.
45 *
46 * @template F
47 * The fallback-type.
48 */
49type LiteralUnion<T extends F, F = string> = T | (F & {});
50
51/**
52 * Provides prompts for answering questions.
53 */
54interface PromptModuleBase {
55 /**
56 * Registers a new prompt-type.
57 *
58 * @param name
59 * The name of the prompt.
60 *
61 * @param prompt
62 * The constructor of the prompt.
63 */
64 registerPrompt(name: string, prompt: inquirer.prompts.PromptConstructor): void;
65
66 /**
67 * Registers the default prompts.
68 */
69 restoreDefaultPrompts(): void;
70}
71
72/**
73 * Represents a list-based question.
74 *
75 * @template T
76 * The type of the answers.
77 *
78 * @template TChoiceMap
79 * The valid choices for the question.
80 */
81interface ListQuestionOptionsBase<T, TChoiceMap> extends inquirer.Question<T> {
82 /**
83 * The choices of the prompt.
84 */
85 choices?: inquirer.AsyncDynamicQuestionProperty<ReadonlyArray<inquirer.DistinctChoice<TChoiceMap>>, T>;
86
87 /**
88 * The number of elements to show on each page.
89 */
90 pageSize?: number;
91}
92
93/**
94 * Provides components for the module.
95 */
96declare namespace inquirer {
97 /**
98 * Represents either a key of `T` or a `string`.
99 *
100 * @template T
101 * The type of the keys to suggest.
102 */
103 type KeyUnion<T> = LiteralUnion<Extract<keyof T, string>>;
104
105 /**
106 * Converts the specified union-type `U` to an intersection-type.
107 *
108 * @template U
109 * The union to convert to an intersection.
110 */
111 type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
112
113 /**
114 * Provides an input and an output-stream.
115 */
116 interface StreamOptions {
117 /**
118 * A stream to read the input from.
119 */
120 input?: NodeJS.ReadStream;
121
122 /**
123 * A stream to write the output to.
124 */
125 output?: NodeJS.WriteStream;
126 }
127
128 /**
129 * Provides the functionality to prompt questions to the user.
130 */
131 interface PromptModule extends PromptModuleBase {
132 /**
133 * The prompts of the prompt-module.
134 */
135 prompts: prompts.PromptCollection;
136
137 /**
138 * Prompts the questions to the user.
139 */
140 <T>(questions: QuestionCollection<T>): Promise<T> & { ui: PromptUI };
141
142 /**
143 * Registers a new prompt-type.
144 *
145 * @param name
146 * The name of the prompt.
147 *
148 * @param prompt
149 * The constructor of the prompt.
150 */
151 registerPrompt(name: string, prompt: prompts.PromptConstructor): this;
152 }
153
154 interface Inquirer extends PromptModuleBase {
155 /**
156 * Registers a new prompt-type.
157 *
158 * @param name
159 * The name of the prompt.
160 *
161 * @param prompt
162 * The constructor of the prompt.
163 */
164 registerPrompt(name: string, prompt: prompts.PromptConstructor): void;
165
166 /**
167 * Creates a prompt-module.
168 *
169 * @param opt
170 * The streams for the prompt-module.
171 *
172 * @returns
173 * The new prompt-module.
174 */
175 createPromptModule(opt?: StreamOptions): PromptModule;
176
177 /**
178 * The default prompt-module.
179 */
180 prompt: PromptModule;
181
182 /**
183 * The prompts of the default prompt-module.
184 *
185 * @deprecated
186 */
187 prompts: {};
188
189 /**
190 * Represents a choice-item separator.
191 */
192 Separator: typeof Separator;
193
194 /**
195 * Provides ui-components.
196 */
197 ui: {
198 /**
199 * Represents the bottom-bar UI.
200 */
201 BottomBar: typeof BottomBar;
202
203 /**
204 * Represents the prompt ui.
205 */
206 Prompt: typeof PromptUI;
207 };
208 }
209
210 /**
211 * A set of answers.
212 */
213 interface Answers extends Record<string, any> { }
214
215 /**
216 * Provides the functionality to validate answers.
217 *
218 * @template T
219 * The type of the answers.
220 */
221 type Validator<T extends Answers = Answers> = Question<T>["validate"];
222
223 /**
224 * Provides the functionality to transform an answer.
225 *
226 * @template T
227 * The type of the answers.
228 */
229 type Transformer<T extends Answers = Answers> = InputQuestionOptions<T>["transformer"];
230
231 /**
232 * Represents a dynamic property for a question.
233 */
234 type DynamicQuestionProperty<T, TAnswers extends Answers = Answers> = T | ((answers: TAnswers) => T);
235
236 /**
237 * Represents a dynamic property for a question which can be fetched asynchronously.
238 */
239 type AsyncDynamicQuestionProperty<T, TAnswers extends Answers = Answers> = DynamicQuestionProperty<T | Promise<T>, TAnswers>;
240
241 /**
242 * Provides options for a question.
243 *
244 * @template T
245 * The type of the answers.
246 */
247 interface Question<T extends Answers = Answers> {
248 /**
249 * The type of the question.
250 */
251 type?: string;
252
253 /**
254 * The key to save the answer to the answers-hash.
255 */
256 name?: KeyUnion<T>;
257
258 /**
259 * The message to show to the user.
260 */
261 message?: AsyncDynamicQuestionProperty<string, T>;
262
263 /**
264 * The default value of the question.
265 */
266 default?: AsyncDynamicQuestionProperty<any, T>;
267
268 /**
269 * The prefix of the `message`.
270 */
271 prefix?: string;
272
273 /**
274 * The suffix of the `message`.
275 */
276 suffix?: string;
277
278 /**
279 * Post-processes the answer.
280 *
281 * @param input
282 * The answer provided by the user.
283 */
284 filter?(input: any): any;
285
286 /**
287 * A value indicating whether the question should be prompted.
288 */
289 when?: AsyncDynamicQuestionProperty<boolean, T>;
290
291 /**
292 * Validates the integrity of the answer.
293 *
294 * @param input
295 * The answer provided by the user.
296 *
297 * @param answers
298 * The answers provided by the user.
299 *
300 * @returns
301 * Either a value indicating whether the answer is valid or a `string` which describes the error.
302 */
303 validate?(input: any, answers?: T): boolean | string | Promise<boolean | string>;
304 }
305
306 /**
307 * Represents a choice-item.
308 */
309 interface ChoiceBase {
310 /**
311 * The type of the choice.
312 */
313 type?: string;
314 }
315
316 /**
317 * Provides options for a choice.
318 *
319 * @template T
320 * The type of the answers.
321 */
322 interface ChoiceOptions<T extends Answers = Answers> extends ChoiceBase {
323 /**
324 * @inheritdoc
325 */
326 type?: "choice";
327
328 /**
329 * The name of the choice to show to the user.
330 */
331 name?: string;
332
333 /**
334 * The value of the choice.
335 */
336 value?: any;
337
338 /**
339 * The short form of the name of the choice.
340 */
341 short?: string;
342
343 /**
344 * The extra properties of the choice.
345 */
346 extra?: any;
347 }
348
349 /**
350 * Provides options for a choice of the `ListPrompt`.
351 *
352 * @template T
353 * The type of the answers.
354 */
355 interface ListChoiceOptions<T extends Answers = Answers> extends ChoiceOptions<T> {
356 /**
357 * A value indicating whether the choice is disabled.
358 */
359 disabled?: DynamicQuestionProperty<boolean | string, T>;
360 }
361
362 /**
363 * Provides options for a choice of the `CheckboxPrompt`.
364 *
365 * @template T
366 * The type of the answers.
367 */
368 interface CheckboxChoiceOptions<T extends Answers = Answers> extends ListChoiceOptions<T> {
369 /**
370 * A value indicating whether the choice should be initially checked.
371 */
372 checked?: boolean;
373 }
374
375 /**
376 * Provides options for a choice of the `ExpandPrompt`.
377 *
378 * @template T
379 * The type of the answers.
380 */
381 interface ExpandChoiceOptions<T extends Answers = Answers> extends ChoiceOptions<T> {
382 /**
383 * The key to press for selecting the choice.
384 */
385 key?: string;
386 }
387
388 /**
389 * Represents a separator.
390 */
391 interface SeparatorOptions {
392 /**
393 * Gets the type of the choice.
394 */
395 type: "separator";
396
397 /**
398 * Gets or sets the text of the separator.
399 */
400 line?: string;
401 }
402
403 /**
404 * Provides all valid choice-types for any kind of question.
405 *
406 * @template T
407 * The type of the answers.
408 */
409 interface BaseChoiceMap<T extends Answers = Answers> {
410 Choice: Choice<T>;
411 ChoiceOptions: ChoiceOptions<T>;
412 SeparatorOptions: SeparatorOptions;
413 Separator: Separator;
414 }
415
416 /**
417 * Provides all valid choice-types for the `ListQuestion`.
418 *
419 * @template T
420 * The type of the answers.
421 */
422 interface ListChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
423 ListChoiceOptions: ListChoiceOptions<T>;
424 }
425
426 /**
427 * Provides all valid choice-types for the `CheckboxQuestion`.
428 *
429 * @template T
430 * The type of the answers.
431 */
432 interface CheckboxChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
433 CheckboxChoiceOptions: CheckboxChoiceOptions<T>;
434 }
435
436 /**
437 * Provides all valid choice-types for the `ExpandQuestion`.
438 *
439 * @template T
440 * The type of the answers.
441 */
442 interface ExpandChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
443 ExpandChoiceOptions: ExpandChoiceOptions<T>;
444 }
445
446 /**
447 * Provides all valid choice-types.
448 *
449 * @template T
450 * The type of the answers.
451 */
452 interface AllChoiceMap<T extends Answers = Answers> {
453 BaseChoiceMap: BaseChoiceMap<T>[keyof BaseChoiceMap<T>];
454 ListChoiceMap: ListChoiceMap<T>[keyof ListChoiceMap<T>];
455 CheckboxChoiceMap: CheckboxChoiceMap<T>[keyof CheckboxChoiceMap<T>];
456 ExpandChoiceMap: ExpandChoiceMap<T>[keyof ExpandChoiceMap<T>];
457 }
458
459 /**
460 * Provides valid choices for the question of the `TChoiceMap`.
461 *
462 * @template TChoiceMap
463 * The choice-types to provide.
464 */
465 type DistinctChoice<TChoiceMap> =
466 string |
467 TChoiceMap[keyof TChoiceMap];
468
469 /**
470 * Represents a set of choices.
471 */
472 type ChoiceCollection<T extends Answers = Answers> = Array<DistinctChoice<AllChoiceMap>>;
473
474 /**
475 * Provides options for a question for the `InputPrompt`.
476 *
477 * @template T
478 * The type of the answers.
479 */
480 interface InputQuestionOptions<T extends Answers = Answers> extends Question<T> {
481 /**
482 * Transforms the value to display to the user.
483 *
484 * @param input
485 * The input provided by the user.
486 *
487 * @param answers
488 * The answers provided by the users.
489 *
490 * @param flags
491 * Additional information about the value.
492 *
493 * @returns
494 * The value to display to the user.
495 */
496 transformer?(input: any, answers: T, flags: { isFinal?: boolean }): string | Promise<string>;
497 }
498
499 /**
500 * Provides options for a question for the `InputPrompt`.
501 *
502 * @template T
503 * The type of the answers.
504 */
505 interface InputQuestion<T extends Answers = Answers> extends InputQuestionOptions<T> {
506 /**
507 * @inheritdoc
508 */
509 type?: "input";
510 }
511
512 /**
513 * Provides options for a question for the `NumberPrompt`.
514 *
515 * @template T
516 * The type of the answers.
517 */
518 interface NumberQuestionOptions<T extends Answers = Answers> extends InputQuestionOptions<T> { }
519
520 /**
521 * Provides options for a question for the `NumberPrompt`.
522 *
523 * @template T
524 * The type of the answers.
525 */
526 interface NumberQuestion<T extends Answers = Answers> extends NumberQuestionOptions<T> {
527 /**
528 * @inheritdoc
529 */
530 type: "number";
531 }
532
533 /**
534 * Provides options for a question for the `PasswordPrompt`.
535 *
536 * @template T
537 * The type of the answers.
538 */
539 interface PasswordQuestionOptions<T extends Answers = Answers> extends InputQuestionOptions<T> {
540 /**
541 * The character to replace the user-input.
542 */
543 mask?: string;
544 }
545
546 /**
547 * Provides options for a question for the `PasswordPrompt`.
548 *
549 * @template T
550 * The type of the answers.
551 */
552 interface PasswordQuestion<T extends Answers = Answers> extends PasswordQuestionOptions<T> {
553 /**
554 * @inheritdoc
555 */
556 type: "password";
557 }
558
559 /**
560 * Provides options for a question for the `ListPrompt`.
561 *
562 * @template T
563 * The type of the answers.
564 */
565 interface ListQuestionOptions<T extends Answers = Answers> extends ListQuestionOptionsBase<T, ListChoiceMap<T>> { }
566
567 /**
568 * Provides options for a question for the `ListPrompt`.
569 *
570 * @template T
571 * The type of the answers.
572 */
573 interface ListQuestion<T extends Answers = Answers> extends ListQuestionOptions<T> {
574 /**
575 * @inheritdoc
576 */
577 type: "list";
578 }
579
580 /**
581 * Provides options for a question for the `RawListPrompt`.
582 *
583 * @template T
584 * The type of the answers.
585 */
586 interface RawListQuestionOptions<T extends Answers = Answers> extends ListQuestionOptions<T> { }
587
588 /**
589 * Provides options for a question for the `RawListPrompt`.
590 *
591 * @template T
592 * The type of the answers.
593 */
594 interface RawListQuestion<T extends Answers = Answers> extends ListQuestionOptionsBase<T, ListChoiceMap<T>> {
595 /**
596 * @inheritdoc
597 */
598 type: "rawlist";
599 }
600
601 /**
602 * Provides options for a question for the `ExpandPrompt`.
603 *
604 * @template T
605 * The type of the answers.
606 */
607 interface ExpandQuestionOptions<T extends Answers = Answers> extends ListQuestionOptionsBase<T, ExpandChoiceMap<T>> { }
608
609 /**
610 * Provides options for a question for the `ExpandPrompt`.
611 *
612 * @template T
613 * The type of the answers.
614 */
615 interface ExpandQuestion<T extends Answers = Answers> extends ExpandQuestionOptions<T> {
616 /**
617 * @inheritdoc
618 */
619 type: "expand";
620 }
621
622 /**
623 * Provides options for a question for the `CheckboxPrompt`.
624 *
625 * @template T
626 * The type of the answers.
627 */
628 interface CheckboxQuestionOptions<T extends Answers = Answers> extends ListQuestionOptionsBase<T, CheckboxChoiceMap<T>> { }
629
630 /**
631 * Provides options for a question for the `CheckboxPrompt`.
632 *
633 * @template T
634 * The type of the answers.
635 */
636 interface CheckboxQuestion<T extends Answers = Answers> extends CheckboxQuestionOptions<T> {
637 /**
638 * @inheritdoc
639 */
640 type: "checkbox";
641 }
642
643 /**
644 * Provides options for a question for the `ConfirmPrompt`.
645 *
646 * @template T
647 * The type of the answers.
648 */
649 interface ConfirmQuestionOptions<T extends Answers = Answers> extends Question<T> { }
650
651 /**
652 * Provides options for a question for the `ConfirmPrompt`.
653 *
654 * @template T
655 * The type of the answers.
656 */
657 interface ConfirmQuestion<T extends Answers = Answers> extends ConfirmQuestionOptions<T> {
658 /**
659 * @inheritdoc
660 */
661 type: "confirm";
662 }
663
664 /**
665 * Provides options for a question for the `EditorPrompt`.
666 *
667 * @template T
668 * The type of the answers.
669 */
670 interface EditorQuestionOptions<T extends Answers = Answers> extends Question<T> { }
671
672 /**
673 * Provides options for a question for the `EditorPrompt`.
674 *
675 * @template T
676 * The type of the answers.
677 */
678 interface EditorQuestion<T extends Answers = Answers> extends EditorQuestionOptions<T> {
679 /**
680 * @inheritdoc
681 */
682 type: "editor";
683 }
684
685 /**
686 * Provides the available question-types.
687 *
688 * @template T
689 * The type of the answers.
690 */
691 interface QuestionMap<T extends Answers = Answers> {
692 /**
693 * The `InputQuestion` type.
694 */
695 input: InputQuestion<T>;
696
697 /**
698 * The `NumberQuestion` type.
699 */
700 number: NumberQuestion<T>;
701
702 /**
703 * The `PasswordQuestion` type.
704 */
705 password: PasswordQuestion<T>;
706
707 /**
708 * The `ListQuestion` type.
709 */
710 list: ListQuestion<T>;
711
712 /**
713 * The `RawListQuestion` type.
714 */
715 rawList: RawListQuestion<T>;
716
717 /**
718 * The `ExpandQuestion` type.
719 */
720 expand: ExpandQuestion<T>;
721
722 /**
723 * The `CheckboxQuestion` type.
724 */
725 checkbox: CheckboxQuestion<T>;
726
727 /**
728 * The `ConfirmQuestion` type.
729 */
730 confirm: ConfirmQuestion<T>;
731
732 /**
733 * The `EditorQuestion` type.
734 */
735 editor: EditorQuestion<T>;
736 }
737
738 /**
739 * Represents one of the available questions.
740 *
741 * @template T
742 * The type of the answers.
743 */
744 type DistinctQuestion<T extends Answers = Answers> = QuestionMap<T>[keyof QuestionMap<T>];
745
746 /**
747 * Represents a collection of questions.
748 *
749 * @template T
750 * The type of the answers.
751 */
752 type QuestionCollection<T extends Answers = Answers> =
753 | DistinctQuestion<T>
754 | ReadonlyArray<DistinctQuestion<T>>
755 | Observable<DistinctQuestion<T>>;
756
757 /**
758 * Provides components for the prompts.
759 */
760 namespace prompts {
761 /**
762 * Provides a base for and prompt-options.
763 *
764 * @template T
765 * The type of the answers.
766 */
767 type PromptOptions<T extends Question = Question> = T & {
768 /**
769 * The choices of the prompt.
770 */
771 choices: Choices;
772 };
773
774 /**
775 * Represents the state of a prompt.
776 */
777 type PromptState = LiteralUnion<"pending" | "idle" | "loading" | "answered" | "done">;
778
779 /**
780 * Represents a prompt.
781 */
782 interface PromptBase {
783 /**
784 * Gets or sets a string which represents the state of the prompt.
785 */
786 status: PromptState;
787
788 /**
789 * Runs the prompt.
790 *
791 * @returns
792 * The result of the prompt.
793 */
794 run(): Promise<any>;
795 }
796
797 /**
798 * Provides the functionality to initialize new prompts.
799 */
800 interface PromptConstructor {
801 /**
802 * Initializes a new instance of a prompt.
803 *
804 * @param question
805 * The question to prompt.
806 *
807 * @param readLine
808 * An object for reading from the command-line.
809 *
810 * @param answers
811 * The answers provided by the user.
812 */
813 new(question: any, readLine: ReadlineInterface, answers: Answers): PromptBase;
814 }
815
816 /**
817 * Provides a set of prompt-constructors.
818 */
819 type PromptCollection = Record<string, PromptConstructor>;
820
821 /**
822 * Provides data about the state of a prompt.
823 */
824 interface PromptStateData {
825 /**
826 * Either a string which describes the error of the prompt or a boolean indicating whether the prompt-value is valid.
827 */
828 isValid: string | boolean;
829 }
830
831 /**
832 * Provides data about the successful state of a prompt.
833 *
834 * @param T
835 * The type of the answer.
836 */
837 interface SuccessfulPromptStateData<T = any> extends PromptStateData {
838 /**
839 * @inheritdoc
840 */
841 isValid: true;
842
843 /**
844 * The value of the prompt.
845 */
846 value: T;
847 }
848
849 /**
850 * Provides data about the failed state of a prompt.
851 */
852 interface FailedPromptStateData extends PromptStateData {
853 /**
854 * @inheritdoc
855 */
856 isValid: false | string;
857 }
858
859 /**
860 * Provides pipes for handling events of a prompt.
861 *
862 * @param T
863 * The type of the answer.
864 */
865 interface PromptEventPipes<T = any> {
866 /**
867 * A pypeline for succesful inputs.
868 */
869 success: Observable<SuccessfulPromptStateData<T>>;
870
871 /**
872 * An object representing an error.
873 */
874 error: Observable<FailedPromptStateData>;
875 }
876 }
877
878 /**
879 * Provides components for the ui.
880 */
881 namespace ui {
882 /**
883 * Provides options for the bottom-bar UI.
884 */
885 interface BottomBarOptions extends StreamOptions {
886 /**
887 * The initial text to display.
888 */
889 bottomBar?: string;
890 }
891
892 /**
893 * Represents a fetched answer.
894 *
895 * @template T
896 * The type of the answers.
897 */
898 type FetchedQuestion<T extends Answers = Answers> = DistinctQuestion<T> & {
899 /**
900 * The type of the question.
901 */
902 type: string;
903
904 /**
905 * The message to show to the user.
906 */
907 message: string;
908
909 /**
910 * The default value of the question.
911 */
912 default: any;
913
914 /**
915 * The choices of the question.
916 */
917 choices: ChoiceCollection<T>;
918 };
919
920 /**
921 * Represents a fetched answer.
922 */
923 interface FetchedAnswer {
924 /**
925 * The name of the answer.
926 */
927 name: string;
928
929 /**
930 * The value of the answer.
931 */
932 answer: any;
933 }
934 }
935}
936
937/**
938 * Provides the functionality to prompt questions.
939 */
940declare var inquirer: inquirer.Inquirer;
941export = inquirer;
942
\No newline at end of file