UNPKG

11.8 kBTypeScriptView Raw
1// Type definitions for Inquirer.js
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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11
12// TypeScript Version: 2.3
13/// <reference types="rx" />
14
15import through = require('through');
16
17declare namespace inquirer {
18 type Prompts = { [name: string]: PromptModule };
19 type ChoiceType = string | objects.ChoiceOption | objects.Separator;
20 type Questions<T = Answers> =
21 | Question<T>
22 | ReadonlyArray<Question<T>>
23 | Rx.Observable<Question<T>>;
24
25 interface Inquirer {
26 restoreDefaultPrompts(): void;
27 /**
28 * Expose helper functions on the top level for easiest usage by common users
29 * @param name
30 * @param prompt
31 */
32 registerPrompt(name: string, prompt: PromptModule): void;
33 /**
34 * Create a new self-contained prompt module.
35 */
36 createPromptModule(): PromptModule;
37 /**
38 * Public CLI helper interface
39 * @param questions Questions settings array
40 * @param cb Callback being passed the user answers
41 * @return
42 */
43 prompt<T>(questions: Questions<T>, cb: (answers: T) => any): ui.Prompt;
44 prompt<T>(questions: Questions<T>): Promise<T>;
45 prompts: Prompts;
46 Separator: objects.SeparatorStatic;
47 ui: {
48 BottomBar: ui.BottomBar;
49 Prompt: ui.Prompt;
50 };
51 }
52
53 interface PromptModule {
54 <T>(questions: Questions<T>): Promise<T>;
55 <T>(questions: Questions<T>, cb: (answers: T) => any): ui.Prompt;
56 /**
57 * Register a prompt type
58 * @param name Prompt type name
59 * @param prompt Prompt constructor
60 */
61 registerPrompt(name: string, prompt: PromptModule): ui.Prompt;
62 /**
63 * Register the defaults provider prompts
64 */
65 restoreDefaultPrompts(): void;
66 }
67
68 interface Question<T = Answers> {
69 /**
70 * Type of the prompt.
71 * Possible values:
72 * <ul>
73 * <li>input</li>
74 * <li>confirm</li>
75 * <li>list</li>
76 * <li>rawlist</li>
77 * <li>password</li>
78 * </ul>
79 * @defaults: 'input'
80 */
81 type?: string;
82 /**
83 * The name to use when storing the answer in the anwers hash.
84 */
85 name?: string;
86 /**
87 * The question to print. If defined as a function,
88 * the first parameter will be the current inquirer session answers.
89 */
90 message?: string | ((answers: T) => string);
91 /**
92 * Default value(s) to use if nothing is entered, or a function that returns the default value(s).
93 * If defined as a function, the first parameter will be the current inquirer session answers.
94 */
95 default?: any | ((answers: T) => any) | ((answers: T) => Promise<any>);
96 /**
97 * Choices array or a function returning a choices array. If defined as a function,
98 * the first parameter will be the current inquirer session answers.
99 * Array values can be simple strings, or objects containing a name (to display) and a value properties
100 * (to save in the answers hash). Values can also be a Separator.
101 */
102 choices?:
103 | ReadonlyArray<ChoiceType>
104 | ((answers: T) => ReadonlyArray<ChoiceType>)
105 | ((answers: T) => Promise<ReadonlyArray<ChoiceType>>);
106 /**
107 * Receive the user input and should return true if the value is valid, and an error message (String)
108 * otherwise. If false is returned, a default error message is provided.
109 */
110 validate?(input: string, answers?: T): boolean | string | Promise<boolean | string>;
111 /**
112 * Receive the user input and return the filtered value to be used inside the program.
113 * The value returned will be added to the Answers hash.
114 */
115 filter?(input: string): string | Promise<string>;
116 /**
117 * Receive the user input and return the transformed value to be displayed to the user. The
118 * transformation only impacts what is shown while editing. It does not impact the answers
119 * hash.
120 */
121 transformer?(input: string): string;
122 /**
123 * Receive the current user answers hash and should return true or false depending on whether or
124 * not this question should be asked. The value can also be a simple boolean.
125 */
126 when?: boolean | ((answers: T) => boolean) | ((answers: T) => Promise<boolean>);
127 paginated?: boolean;
128 /**
129 * Change the number of lines that will be rendered when using list, rawList, expand or checkbox.
130 */
131 pageSize?: number;
132 /**
133 * Add a mask when password will entered
134 */
135 mask?: string;
136 /**
137 * Change the default prefix message.
138 */
139 prefix?: string;
140 /**
141 * Change the default suffix message.
142 */
143 suffix?: string;
144 }
145
146 /**
147 * A key/value hash containing the client answers in each prompt.
148 */
149 interface Answers {
150 [key: string]: any;
151 }
152
153 namespace ui {
154 /**
155 * Base interface class other can inherits from
156 */
157 interface Prompt extends BaseUI<Prompts> {
158 new (promptModule: Prompts): Prompt;
159 /**
160 * Once all prompt are over
161 */
162 onCompletion(): void;
163 processQuestion<T>(question: Question<T>): any;
164 fetchAnswer<T>(question: Question<T>): any;
165 setDefaultType<T>(question: Question<T>): any;
166 filterIfRunnable<T>(question: Question<T>): any;
167 }
168
169 /**
170 * Sticky bottom bar user interface
171 */
172 interface BottomBar extends BaseUI<BottomBarOption> {
173 new (opt?: BottomBarOption): BottomBar;
174 /**
175 * Render the prompt to screen
176 * @return self
177 */
178 render(): BottomBar;
179 /**
180 * Update the bottom bar content and rerender
181 * @param bottomBar Bottom bar content
182 * @return self
183 */
184 updateBottomBar(bottomBar: string): BottomBar;
185 /**
186 * Rerender the prompt
187 * @return self
188 */
189 writeLog(data: any): BottomBar;
190 /**
191 * Make sure line end on a line feed
192 * @param str Input string
193 * @return The input string with a final line feed
194 */
195 enforceLF(str: string): string;
196 /**
197 * Helper for writing message in Prompt
198 * @param message The message to be output
199 */
200 write(message: string): void;
201 log: through.ThroughStream;
202 }
203
204 interface BottomBarOption {
205 bottomBar?: string;
206 }
207 /**
208 * Base interface class other can inherits from
209 */
210 interface BaseUI<TOpt> {
211 new (opt: TOpt): void;
212 /**
213 * Handle the ^C exit
214 * @return {null}
215 */
216 onForceClose(): void;
217 /**
218 * Close the interface and cleanup listeners
219 */
220 close(): void;
221 /**
222 * Handle and propagate keypress events
223 */
224 onKeypress(s: string, key: Key): void;
225 }
226
227 interface Key {
228 sequence: string;
229 name: string;
230 meta: boolean;
231 shift: boolean;
232 ctrl: boolean;
233 }
234 }
235
236 namespace objects {
237 /**
238 * Choice object
239 * Normalize input as choice object
240 * @constructor
241 * @param {String|Object} val Choice value. If an object is passed, it should contains
242 * at least one of `value` or `name` property
243 */
244 interface Choice {
245 new (str: string): Choice;
246 new (separator: Separator): Choice;
247 new (option: ChoiceOption): Choice;
248 }
249
250 interface ChoiceOption {
251 name?: string;
252 value?: any;
253 type?: string;
254 extra?: any;
255 key?: string;
256 checked?: boolean;
257 disabled?: string | (<T = Answers>(answers: T) => any);
258 }
259
260 /**
261 * Choices collection
262 * Collection of multiple `choice` object
263 * @constructor
264 * @param choices All `choice` to keep in the collection
265 */
266 interface Choices {
267 new <T = Answers>(
268 choices: ReadonlyArray<string | Separator | ChoiceOption>,
269 answers?: T
270 ): Choices;
271 choices: ReadonlyArray<Choice>;
272 realChoices: ReadonlyArray<Choice>;
273 length: number;
274 realLength: number;
275 /**
276 * Get a valid choice from the collection
277 * @param selector The selected choice index
278 * @return Return the matched choice or undefined
279 */
280 getChoice(selector: number): Choice;
281 /**
282 * Get a raw element from the collection
283 * @param selector The selected index value
284 * @return Return the matched choice or undefined
285 */
286 get(selector: number): Choice;
287 /**
288 * Match the valid choices against a where clause
289 * @param whereClause Lodash `where` clause
290 * @return Matching choices or empty array
291 */
292 where<U extends {}>(whereClause: U): Choice[];
293 /**
294 * Pluck a particular key from the choices
295 * @param propertyName Property name to select
296 * @return Selected properties
297 */
298 pluck(propertyName: string): any[];
299 forEach<T>(application: (choice: Choice) => T): T[];
300 }
301
302 interface SeparatorStatic {
303 /**
304 * @param line Separation line content (facultative)
305 */
306 new (line?: string): Separator;
307 /**
308 * Helper function returning false if object is a separator
309 * @param obj object to test against
310 * @return `false` if object is a separator
311 */
312 exclude(obj: any): boolean;
313 }
314
315 /**
316 * Separator object
317 * Used to space/separate choices group
318 * @constructor
319 * @param {String} line Separation line content (facultative)
320 */
321 interface Separator {
322 type: string;
323 line: string;
324 /**
325 * Stringify separator
326 * @return {String} the separator display string
327 */
328 toString(): string;
329 }
330 }
331}
332
333declare var inquirer: inquirer.Inquirer;
334
335export = inquirer;
336
\No newline at end of file