UNPKG

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