UNPKG

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