diff --git a/types/inquirer/index.d.ts b/types/inquirer/index.d.ts index 74aee08ed3..d61b2adde4 100644 --- a/types/inquirer/index.d.ts +++ b/types/inquirer/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Inquirer.js +// Type definitions for Inquirer.js 6.x // Project: https://github.com/SBoudrias/Inquirer.js // Definitions by: Qubo // Parvez @@ -9,27 +9,26 @@ // Justin Rockwood // Keith Kelly // Junyoung Clare Jang +// Richard Lea // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 -/// +// TypeScript Version: 2.8 import through = require('through'); +import { Observable } from 'rxjs'; +import * as readline from 'readline'; declare namespace inquirer { - type Prompts = { [name: string]: PromptModule }; + type Prompts = { [name: string]: prompts.Base }; type ChoiceType = string | objects.ChoiceOption | objects.Separator; type Questions = | Question | ReadonlyArray> - | Rx.Observable>; - interface OutputStreamOption { - output: NodeJS.WriteStream - } - interface InputStreamOption { - input: NodeJS.ReadStream - } - type StreamOptions = InputStreamOption | OutputStreamOption | (InputStreamOption & OutputStreamOption); + | Observable>; + type StreamOptions = { + input?: NodeJS.ReadStream, + output?: NodeJS.WriteStream, + }; interface Inquirer { restoreDefaultPrompts(): void; @@ -47,28 +46,25 @@ declare namespace inquirer { /** * Public CLI helper interface * @param questions Questions settings array - * @param cb Callback being passed the user answers * @return */ - prompt(questions: Questions, cb: (answers: T) => any): ui.Prompt; - prompt(questions: Questions): Promise; + prompt: PromptModule; prompts: Prompts; Separator: objects.SeparatorStatic; ui: { BottomBar: ui.BottomBar; - Prompt: ui.Prompt; + Prompt: ui.PromptUI; }; } interface PromptModule { - (questions: Questions): Promise; - (questions: Questions, cb: (answers: T) => any): ui.Prompt; + (questions: Questions): Promise & { ui: ui.PromptUI }; /** * Register a prompt type * @param name Prompt type name * @param prompt Prompt constructor */ - registerPrompt(name: string, prompt: PromptModule): ui.Prompt; + registerPrompt(name: string, prompt: prompts.Base): PromptModule; /** * Register the defaults provider prompts */ @@ -111,9 +107,9 @@ declare namespace inquirer { * (to save in the answers hash). Values can also be a Separator. */ choices?: - | ReadonlyArray - | ((answers: T) => ReadonlyArray) - | ((answers: T) => Promise>); + | ReadonlyArray + | ((answers: T) => ReadonlyArray) + | ((answers: T) => Promise>); /** * Receive the user input and should return true if the value is valid, and an error message (String) * otherwise. If false is returned, a default error message is provided. @@ -161,32 +157,88 @@ declare namespace inquirer { [key: string]: any; } + /** + * Corresponding to the answer object creation in: + * https://github.com/SBoudrias/Inquirer.js/blob/ff075f587ef78504f0eae4ee5ca0656432429026/packages/inquirer/lib/ui/prompt.js#L88 + */ + interface Answer { + name: string, + answer: any, + } + + namespace prompts { + /** + * Base prompt implementation + * Should be extended by prompt types. + * + * @interface Base + */ + interface Base { + new (question: Question, rl: readline.Interface, answers: Answers): Base; + /** + * Start the Inquiry session and manage output value filtering + * + * @returns {Promise} + * @memberof Base + */ + run(): Promise; + /** + * Called when the UI closes. Override to do any specific cleanup necessary + */ + close(): void; + /** + * Generate the prompt question string + */ + getQuestion(): string; + } + } + namespace ui { + /** * Base interface class other can inherits from */ - interface Prompt extends BaseUI { - new (promptModule: Prompts): Prompt; + interface BaseUI { + rl: readline.Interface; + new(opt: StreamOptions): BaseUI; + /** + * Handle the ^C exit + * @return {null} + */ + onForceClose(): void; + /** + * Close the interface and cleanup listeners + */ + close(): void; + } + /** + * Base interface class other can inherits from + */ + interface PromptUI extends BaseUI { + process: Observable; + new(prompts: Prompts, opt: StreamOptions): PromptUI; + run(questions: Questions): Promise; /** * Once all prompt are over */ onCompletion(): void; - processQuestion(question: Question): any; - fetchAnswer(question: Question): any; - setDefaultType(question: Question): any; - filterIfRunnable(question: Question): any; + processQuestion(question: Question): Observable>; + fetchAnswer(question: Question): Observable>; + setDefaultType(question: Question): Observable>; + filterIfRunnable(question: Question): Observable>; } /** * Sticky bottom bar user interface */ - interface BottomBar extends BaseUI { - new (opt?: BottomBarOption): BottomBar; + interface BottomBar extends BaseUI { + new(opt?: StreamOptions & { bottomBar?: string }): BottomBar; /** * Render the prompt to screen * @return self */ render(): BottomBar; + clean(): BottomBar; /** * Update the bottom bar content and rerender * @param bottomBar Bottom bar content @@ -194,10 +246,11 @@ declare namespace inquirer { */ updateBottomBar(bottomBar: string): BottomBar; /** - * Rerender the prompt + * Write out log data + * @param {String} data - The log data to be output * @return self */ - writeLog(data: any): BottomBar; + writeLog(data: string): BottomBar; /** * Make sure line end on a line feed * @param str Input string @@ -212,36 +265,6 @@ declare namespace inquirer { log: through.ThroughStream; } - interface BottomBarOption { - bottomBar?: string; - } - /** - * Base interface class other can inherits from - */ - interface BaseUI { - new (opt: TOpt): void; - /** - * Handle the ^C exit - * @return {null} - */ - onForceClose(): void; - /** - * Close the interface and cleanup listeners - */ - close(): void; - /** - * Handle and propagate keypress events - */ - onKeypress(s: string, key: Key): void; - } - - interface Key { - sequence: string; - name: string; - meta: boolean; - shift: boolean; - ctrl: boolean; - } } namespace objects { diff --git a/types/inquirer/inquirer-tests.ts b/types/inquirer/inquirer-tests.ts index 4c8ba9491f..95f8d442a6 100644 --- a/types/inquirer/inquirer-tests.ts +++ b/types/inquirer/inquirer-tests.ts @@ -3,11 +3,10 @@ import inquirer = require("inquirer"); inquirer.prompt( [ /* Pass your questions in here */ - ], - function(answers: inquirer.Answers) { - // Use user feedback for... whatever!! - } -); + ] +).then((answers: inquirer.Answers) => { + // Use user feedback for... whatever!! +}); // // examples/bottom-bar.js @@ -87,11 +86,10 @@ inquirer.prompt<{ toppings: string }>( return true; } } - ], - function(answers) { - console.log(JSON.stringify(answers, null, " ")); - } -); + ] +).then(answers => { + console.log(JSON.stringify(answers, null, " ")); +}); // // examples/expand.js @@ -134,11 +132,10 @@ inquirer.prompt( } ] } - ], - function(answers: inquirer.Answers) { - console.log(JSON.stringify(answers, null, " ")); - } -); + ] +).then((answers: inquirer.Answers) => { + console.log(JSON.stringify(answers, null, " ")); +}); // // examples/input.js @@ -184,9 +181,9 @@ var questions = [ } ]; -inquirer.prompt(questions, function(answers) { - console.log(JSON.stringify(answers, null, " ")); -}); +inquirer.prompt(questions).then( + answers => console.log(JSON.stringify(answers, null, " ")) +); // // examples/list.js @@ -222,11 +219,10 @@ inquirer.prompt( return val.toLowerCase(); } } - ], - function(answers: inquirer.Answers) { - console.log(JSON.stringify(answers, null, " ")); - } -); + ] +).then((answers: inquirer.Answers) => { + console.log(JSON.stringify(answers, null, " ")); +}); // // examples/long-list.js @@ -263,11 +259,10 @@ inquirer.prompt( paginated: true, choices: choices } - ], - function(answers: inquirer.Answers) { - console.log(JSON.stringify(answers, null, " ")); - } -); + ] +).then((answers: inquirer.Answers) => { + console.log(JSON.stringify(answers, null, " ")); +}); // // examples/nested-call.js @@ -286,16 +281,15 @@ inquirer.prompt( name: "chocolate", message: "What's your favorite chocolate?", choices: ["Mars", "Oh Henry", "Hershey"] - }, - function(answers: inquirer.Answers) { - inquirer.prompt({ - type: "list", - name: "beverage", - message: "And your favorite beverage?", - choices: ["Pepsi", "Coke", "7up", "Mountain Dew", "Red Bull"] - }); } -); +).then((answers: inquirer.Answers) => { + inquirer.prompt({ + type: "list", + name: "beverage", + message: "And your favorite beverage?", + choices: ["Pepsi", "Coke", "7up", "Mountain Dew", "Red Bull"] + }); +}); // // examples/password.js @@ -315,11 +309,10 @@ inquirer.prompt( message: "Enter your git password", name: "password" } - ], - function(answers: inquirer.Answers) { - console.log(JSON.stringify(answers, null, " ")); - } -); + ] +).then((answers: inquirer.Answers) => { + console.log(JSON.stringify(answers, null, " ")); +}); // // examples/pizza.js @@ -421,7 +414,7 @@ var questions2 = [ } ]; -inquirer.prompt(questions, function(answers) { +inquirer.prompt(questions).then(answers => { console.log("\nOrder receipt:"); console.log(JSON.stringify(answers, null, " ")); }); @@ -460,11 +453,10 @@ inquirer.prompt( return val.toLowerCase(); } } - ], - function(answers: inquirer.Answers) { - console.log(JSON.stringify(answers, null, " ")); - } -); + ] +).then((answers: inquirer.Answers) => { + console.log(JSON.stringify(answers, null, " ")); +}); // // examples/recursive.js @@ -495,9 +487,8 @@ var questions3 = [ ]; function ask() { - inquirer.prompt<{ tvShow: string; askAgain: boolean }>(questions3, function( - answers - ) { + inquirer.prompt<{ tvShow: string; askAgain: boolean }>(questions3) + .then((answers) => { output2.push(answers.tvShow); if (answers.askAgain) { ask(); @@ -560,7 +551,7 @@ function likesFood(aFood: string) { }; } -inquirer.prompt(questions, function(answers) { +inquirer.prompt(questions).then(answers => { console.log(JSON.stringify(answers, null, " ")); }); @@ -579,11 +570,10 @@ inquirer.prompt( message: 'What do you want to do?', choices: immutableChoices } - ], - function(answers: inquirer.Answers) { - console.log(JSON.stringify(answers, null, ' ')); - } -); + ] +).then((answers: inquirer.Answers) => { + console.log(JSON.stringify(answers, null, ' ')); +}); // // Other tests not covered in the examples provided with inquirer @@ -667,9 +657,10 @@ var questions = [ } ]; -inquirer.createPromptModule({ output: process.stderr })(questions, function(answers) { - console.log(JSON.stringify(answers, null, " ")); -}); +inquirer.createPromptModule({ output: process.stderr })(questions) + .then(answers => { + console.log(JSON.stringify(answers, null, " ")); + }); // Work with JS inquirer but rejected by typing. inquirer.prompt([ diff --git a/types/inquirer/package.json b/types/inquirer/package.json new file mode 100644 index 0000000000..021b40d754 --- /dev/null +++ b/types/inquirer/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "rxjs": ">=6.4.0" + } +} diff --git a/types/yeoman-generator/index.d.ts b/types/yeoman-generator/index.d.ts index fe39171825..7d461e7b78 100644 --- a/types/yeoman-generator/index.d.ts +++ b/types/yeoman-generator/index.d.ts @@ -5,11 +5,13 @@ // Ika // Joshua Cherry // Arthur Corenzan +// Richard Lea // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.8 import { EventEmitter } from 'events'; import * as inquirer from 'inquirer'; +import { Observable } from 'rxjs'; type Callback = (err: any) => void; @@ -20,7 +22,7 @@ declare namespace Generator { */ store?: boolean; } - type Questions = Question | Question[] | Rx.Observable; + type Questions = Question | Question[] | Observable; type Answers = inquirer.Answers; class Storage { diff --git a/types/yeoman-generator/package.json b/types/yeoman-generator/package.json new file mode 100644 index 0000000000..021b40d754 --- /dev/null +++ b/types/yeoman-generator/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "rxjs": ">=6.4.0" + } +} diff --git a/types/yeoman-test/index.d.ts b/types/yeoman-test/index.d.ts index acccfba1bb..3312f748ff 100644 --- a/types/yeoman-test/index.d.ts +++ b/types/yeoman-test/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/yeoman/yeoman-test, http://yeoman.io/authoring/testing.html // Definitions by: Ika // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.8 import { EventEmitter } from 'events'; import Generator = require('yeoman-generator');