Skip to content

Commit

Permalink
Merge pull request DefinitelyTyped#33736 from chigix/master
Browse files Browse the repository at this point in the history
fix(inquirer): update api definitions to 6.x
  • Loading branch information
sheetalkamat authored Mar 22, 2019
2 parents 1fa4376 + a055ce7 commit fa0c76c
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 125 deletions.
147 changes: 85 additions & 62 deletions types/inquirer/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/tkQubo>
// Parvez <https://github.com/ppathan>
Expand All @@ -9,27 +9,26 @@
// Justin Rockwood <https://github.com/jrockwood>
// Keith Kelly <https://github.com/kwkelly>
// Junyoung Clare Jang <https://github.com/Ailrun>
// Richard Lea <https://github.com/chigix>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

// TypeScript Version: 2.3
/// <reference types="rx" />
// 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<T = Answers> =
| Question<T>
| ReadonlyArray<Question<T>>
| Rx.Observable<Question<T>>;
interface OutputStreamOption {
output: NodeJS.WriteStream
}
interface InputStreamOption {
input: NodeJS.ReadStream
}
type StreamOptions = InputStreamOption | OutputStreamOption | (InputStreamOption & OutputStreamOption);
| Observable<Question<T>>;
type StreamOptions = {
input?: NodeJS.ReadStream,
output?: NodeJS.WriteStream,
};

interface Inquirer {
restoreDefaultPrompts(): void;
Expand All @@ -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<T>(questions: Questions<T>, cb: (answers: T) => any): ui.Prompt;
prompt<T>(questions: Questions<T>): Promise<T>;
prompt: PromptModule;
prompts: Prompts;
Separator: objects.SeparatorStatic;
ui: {
BottomBar: ui.BottomBar;
Prompt: ui.Prompt;
Prompt: ui.PromptUI;
};
}

interface PromptModule {
<T>(questions: Questions<T>): Promise<T>;
<T>(questions: Questions<T>, cb: (answers: T) => any): ui.Prompt;
<T>(questions: Questions<T>): Promise<T> & { 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
*/
Expand Down Expand Up @@ -111,9 +107,9 @@ declare namespace inquirer {
* (to save in the answers hash). Values can also be a Separator.
*/
choices?:
| ReadonlyArray<ChoiceType>
| ((answers: T) => ReadonlyArray<ChoiceType>)
| ((answers: T) => Promise<ReadonlyArray<ChoiceType>>);
| ReadonlyArray<ChoiceType>
| ((answers: T) => ReadonlyArray<ChoiceType>)
| ((answers: T) => Promise<ReadonlyArray<ChoiceType>>);
/**
* 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.
Expand Down Expand Up @@ -161,43 +157,100 @@ 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 <T>(question: Question<T>, rl: readline.Interface, answers: Answers): Base;
/**
* Start the Inquiry session and manage output value filtering
*
* @returns {Promise<any>}
* @memberof Base
*/
run(): Promise<any>;
/**
* 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 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 Prompt extends BaseUI<Prompts> {
new (promptModule: Prompts): Prompt;
interface PromptUI extends BaseUI {
process: Observable<Answer>;
new(prompts: Prompts, opt: StreamOptions): PromptUI;
run<T>(questions: Questions<T>): Promise<Answers>;
/**
* Once all prompt are over
*/
onCompletion(): void;
processQuestion<T>(question: Question<T>): any;
fetchAnswer<T>(question: Question<T>): any;
setDefaultType<T>(question: Question<T>): any;
filterIfRunnable<T>(question: Question<T>): any;
processQuestion<T>(question: Question<T>): Observable<Question<T>>;
fetchAnswer<T>(question: Question<T>): Observable<Question<T>>;
setDefaultType<T>(question: Question<T>): Observable<Question<T>>;
filterIfRunnable<T>(question: Question<T>): Observable<Question<T>>;
}

/**
* Sticky bottom bar user interface
*/
interface BottomBar extends BaseUI<BottomBarOption> {
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
* @return self
*/
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
Expand All @@ -212,36 +265,6 @@ declare namespace inquirer {
log: through.ThroughStream;
}

interface BottomBarOption {
bottomBar?: string;
}
/**
* Base interface class other can inherits from
*/
interface BaseUI<TOpt> {
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 {
Expand Down
Loading

0 comments on commit fa0c76c

Please sign in to comment.