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