UNPKG

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