UNPKG

3.67 kBTypeScriptView Raw
1import { EventEmitter } from "events";
2
3interface BasePromptOptions {
4 name: string | (() => string)
5 type: string | (() => string)
6 message: string | (() => string) | (() => Promise<string>)
7 initial?: any
8 required?: boolean
9 format?(value: string): string | Promise<string>
10 result?(value: string): string | Promise<string>
11 skip?: ((state: object) => boolean | Promise<boolean>) | boolean
12 validate?(value: string): boolean | Promise<boolean> | string | Promise<string>
13 onSubmit?(name: string, value: any, prompt: Enquirer.Prompt): boolean | Promise<boolean>
14 onCancel?(name: string, value: any, prompt: Enquirer.Prompt): boolean | Promise<boolean>
15 stdin?: NodeJS.ReadStream
16 stdout?: NodeJS.WriteStream
17}
18
19interface Choice {
20 name: string
21 message?: string
22 value?: string
23 hint?: string
24 disabled?: boolean | string
25}
26
27interface ArrayPromptOptions extends BasePromptOptions {
28 type:
29 | 'autocomplete'
30 | 'editable'
31 | 'form'
32 | 'multiselect'
33 | 'select'
34 | 'survey'
35 | 'list'
36 | 'scale'
37 choices: string[] | Choice[]
38 maxChoices?: number
39 muliple?: boolean
40 initial?: number
41 delay?: number
42 separator?: boolean
43 sort?: boolean
44 linebreak?: boolean
45 edgeLength?: number
46 align?: 'left' | 'right'
47 scroll?: boolean
48}
49
50interface BooleanPromptOptions extends BasePromptOptions {
51 type: 'confirm'
52 initial?: boolean
53}
54
55interface StringPromptOptions extends BasePromptOptions {
56 type: 'input' | 'invisible' | 'list' | 'password' | 'text'
57 initial?: string
58 multiline?: boolean
59}
60
61interface NumberPromptOptions extends BasePromptOptions {
62 type: 'numeral'
63 min?: number
64 max?: number
65 delay?: number
66 float?: boolean
67 round?: boolean
68 major?: number
69 minor?: number
70 initial?: number
71}
72
73interface SnippetPromptOptions extends BasePromptOptions {
74 type: 'snippet'
75 newline?: string
76 template?: string
77}
78
79interface SortPromptOptions extends BasePromptOptions {
80 type: 'sort'
81 hint?: string
82 drag?: boolean
83 numbered?: boolean
84}
85
86type PromptOptions =
87 | BasePromptOptions
88 | ArrayPromptOptions
89 | BooleanPromptOptions
90 | StringPromptOptions
91 | NumberPromptOptions
92 | SnippetPromptOptions
93 | SortPromptOptions
94
95declare class BasePrompt extends EventEmitter {
96 constructor(options?: PromptOptions);
97
98 render(): void;
99
100 run(): Promise<any>;
101 }
102
103declare class Enquirer<T = object> extends EventEmitter {
104 constructor(options?: object, answers?: T);
105
106 /**
107 * Register a custom prompt type.
108 *
109 * @param type
110 * @param fn `Prompt` class, or a function that returns a `Prompt` class.
111 */
112 register(type: string, fn: typeof BasePrompt | (() => typeof BasePrompt)): this;
113
114 /**
115 * Register a custom prompt type.
116 */
117 register(type: { [key: string]: typeof BasePrompt | (() => typeof BasePrompt) }): this;
118
119 /**
120 * Prompt function that takes a "question" object or array of question objects,
121 * and returns an object with responses from the user.
122 *
123 * @param questions Options objects for one or more prompts to run.
124 */
125 prompt(
126 questions:
127 | PromptOptions
128 | ((this: Enquirer) => PromptOptions)
129 | (PromptOptions | ((this: Enquirer) => PromptOptions))[]
130 ): Promise<T>;
131
132 /**
133 * Use an enquirer plugin.
134 *
135 * @param plugin Plugin function that takes an instance of Enquirer.
136 */
137 use(plugin: (this: this, enquirer: this) => void): this;
138}
139
140declare namespace Enquirer {
141 function prompt<T = object>(
142 questions:
143 | PromptOptions
144 | ((this: Enquirer) => PromptOptions)
145 | (PromptOptions | ((this: Enquirer) => PromptOptions))[]
146 ): Promise<T>;
147
148 class Prompt extends BasePrompt {}
149}
150
151export = Enquirer;
152
\No newline at end of file