From 153565562fdf09b69b752b23be57d807f36bd183 Mon Sep 17 00:00:00 2001 From: Jim Buck Date: Tue, 30 Aug 2016 00:23:18 -0400 Subject: [PATCH 1/2] Initial definition setup for Vorpal. --- vorpal/vorpal-tests.ts | 8 +++ vorpal/vorpal.d.ts | 117 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 vorpal/vorpal-tests.ts create mode 100644 vorpal/vorpal.d.ts diff --git a/vorpal/vorpal-tests.ts b/vorpal/vorpal-tests.ts new file mode 100644 index 0000000000..a1dd744648 --- /dev/null +++ b/vorpal/vorpal-tests.ts @@ -0,0 +1,8 @@ +/// + +let vorpal = require('vorpal'); + +let app: VorpalInstance = vorpal(); + +app + .parse(process.argv); diff --git a/vorpal/vorpal.d.ts b/vorpal/vorpal.d.ts new file mode 100644 index 0000000000..fcd189257f --- /dev/null +++ b/vorpal/vorpal.d.ts @@ -0,0 +1,117 @@ +// Type definitions for vorpal 1.11.4 +// Project: https://github.com/dthree/vorpal +// Definitions by: Jim Buck +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare type CallbackFunction = (err: string | Error, data: T) => void; + +declare interface VorpalInstance { + + ui: UiInstance; + + constructor(); + + /** + * Parses the process's `process.argv` arguments and executes the matching command. + * + * @param {IArguments} argv + * @param {*} options When `use: 'minimist'` is passed in as an option, `.parse` will instead expose the `minimist` module's main method and return the results, executing no commands. + */ + parse(argv: string[], options?: {use?: 'minimist'}): void; + + /** + * Sets the prompt delimiter for the given Vorpal instance. + * + * @param {string} str + * @returns {this} + */ + delimiter(str: string): this; + + /** + * Attaches the TTY's CLI prompt to that given instance of Vorpal. + * + * As a note, multiple instances of Vorpal can run in the same Node instance. However, only one can be 'attached' to your TTY. The last instance given the show() command will be attached, and the previously shown instances will detach. + */ + show(): void; + + /** + * Returns a given command by its name. This is used instead of `vorpal.command()` as `.command` will overwrite a given command. If command is not found, `undefined` is returned. + * + * @param {string} name + * @returns {Command} + */ + find(name: string): Command; + + exec(command: string, cb: CallbackFunction): PromiseLike; + + execSync(command: string, options: {fatal: boolean}): T; + + log(message: string, ...messages: string[]); + + history(id: string); + + localStorage(id: string); + + help(strBuilder: (cmd: string) => string); + + pipe(pipeFn: (stdout: string) => string); + + use(extension: string | Function); + + + /** + * Adds a new command to your command line API. + * + * @param {string} command + * @param {string} [description] + * @returns {Command} + */ + command(command: string, description?: string): Command; + + catch(command: string, description?: string): Command; + + mode(command: string, description?: string): Mode; +} + +declare interface UiInstance { + + redraw: RedrawMethod; + + delimiter(text?: string); + + input(text?: string); + + imprint(); + + submit(text: string); + + cancel(); +} + +declare interface RedrawMethod +{ + (text: string, ...texts: string[]); + + clear(); + done(); +} + +declare interface Command { + +} + +declare interface Mode { + description(desc: string): this; + + delimiter(str: string): this; + + init(initFn: (args: any, callback: CallbackFunction) => void): this; + + action(actionFn: (command: string, callback: CallbackFunction) => void | T): this; +} + +declare module "vorpal" { + export = VorpalInstance; +} From 6c9414ca55ac950f7a6661413af6b15e0f827929 Mon Sep 17 00:00:00 2001 From: Jim Buck Date: Tue, 30 Aug 2016 20:59:05 -0400 Subject: [PATCH 2/2] Added Command methods. Got tests working. --- vorpal/vorpal-tests.ts | 50 +++++++++++++++++++++-- vorpal/vorpal.d.ts | 92 ++++++++++++++++++++++++++++-------------- 2 files changed, 108 insertions(+), 34 deletions(-) diff --git a/vorpal/vorpal-tests.ts b/vorpal/vorpal-tests.ts index a1dd744648..6650450f69 100644 --- a/vorpal/vorpal-tests.ts +++ b/vorpal/vorpal-tests.ts @@ -1,8 +1,50 @@ -/// +/// -let vorpal = require('vorpal'); +import {Vorpal} from './vorpal'; -let app: VorpalInstance = vorpal(); +declare namespace app { + function execSQL(...args: any[]): PromiseLike; +} -app +// Constructor +let vorpal = new Vorpal(); + +// new-less Constructor +let newLessApp = Vorpal(); + +// Parse +vorpal + .show() .parse(process.argv); + +// Parse Options +var results = vorpal.parse('foo -baz', { use: 'minimist' }); + +new Vorpal().delimiter('unicorn-approved-app$'); + +// Show + +vorpal + .delimiter('pg-cli:') + .show(); + +vorpal + .command('sql ', 'Executes arbitrary sql.') + .action(function (args) { + return app.execSQL(args.query); + }); + +// Show (multiple instances) + +var instances = []; +for (var i = 0; i < 3; ++i) { + instances[i] = new Vorpal() + .delimiter('instance' + i + '~$') + .command('switch ', 'Switches prompt to another instance.') + .action(function (args, cb) { + instances[args.instance].show(); + cb(); + }) +} + +instances[0].show(); \ No newline at end of file diff --git a/vorpal/vorpal.d.ts b/vorpal/vorpal.d.ts index fcd189257f..b327efe825 100644 --- a/vorpal/vorpal.d.ts +++ b/vorpal/vorpal.d.ts @@ -4,23 +4,38 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// -declare type CallbackFunction = (err: string | Error, data: T) => void; +import {} from 'inquirer'; +import {ParsedArgs} from 'minimist'; + +export type CallbackFunction = (err?: string | Error, data?: T) => void; + +interface TypesDefinition +{ + string?: string[]; + number?: string[]; + // TODO: Check other types. +} + +interface VorpalFactory { + new (): VorpalInstance; + + (): VorpalInstance; +} + +interface VorpalInstance { -declare interface VorpalInstance { - ui: UiInstance; - constructor(); - /** * Parses the process's `process.argv` arguments and executes the matching command. * - * @param {IArguments} argv - * @param {*} options When `use: 'minimist'` is passed in as an option, `.parse` will instead expose the `minimist` module's main method and return the results, executing no commands. + * @param {(string|string[])} argv + * @param {{use?: 'minimist'}} [options] When `use: 'minimist'` is passed in as an option, `.parse` will instead expose the `minimist` module's main method and return the results, executing no commands. */ - parse(argv: string[], options?: {use?: 'minimist'}): void; - + parse(argv: string | string[], options?: { use?: 'minimist' }): ParsedArgs; + /** * Sets the prompt delimiter for the given Vorpal instance. * @@ -31,11 +46,12 @@ declare interface VorpalInstance { /** * Attaches the TTY's CLI prompt to that given instance of Vorpal. - * * As a note, multiple instances of Vorpal can run in the same Node instance. However, only one can be 'attached' to your TTY. The last instance given the show() command will be attached, and the previously shown instances will detach. + * + * @returns {this} */ - show(): void; - + show(): this; + /** * Returns a given command by its name. This is used instead of `vorpal.command()` as `.command` will overwrite a given command. If command is not found, `undefined` is returned. * @@ -46,12 +62,12 @@ declare interface VorpalInstance { exec(command: string, cb: CallbackFunction): PromiseLike; - execSync(command: string, options: {fatal: boolean}): T; + execSync(command: string, options: { fatal: boolean }): T; log(message: string, ...messages: string[]); - + history(id: string); - + localStorage(id: string); help(strBuilder: (cmd: string) => string); @@ -60,7 +76,7 @@ declare interface VorpalInstance { use(extension: string | Function); - + /** * Adds a new command to your command line API. * @@ -75,34 +91,47 @@ declare interface VorpalInstance { mode(command: string, description?: string): Mode; } -declare interface UiInstance { +export interface UiInstance { redraw: RedrawMethod; delimiter(text?: string); input(text?: string); - + imprint(); submit(text: string); - + cancel(); } -declare interface RedrawMethod -{ - (text: string, ...texts: string[]); +export function RedrawMethod(text: string): void; +export function RedrawMethod(...texts: string[]): void; - clear(); - done(); +export interface RedrawMethod { + clear(): void; + done(): void; } -declare interface Command { - +export interface Command { + description(description: string): this; + alias(name: string): this; + alias(...names: string[]): this; + parse(parseFn: CommandParseFn): this; + option(flag: string, description: string, autocomplete: string[]): this; // TODO: Check autocomplete types. + types(types: TypesDefinition): this; + hidden(): this; // TODO: Check return type. + remove(); // TODO: Check return type. + help(helpFn: (args) => void); // TODO: Check args type. + validate(validateFn: CommandValidateFn); + autocomplete(choices: string[]): this; + autocomplete(choices: {}): this; // TODO: Revisit this. + autocomplete(choicesFn: CommandAutocompleteFn): this; // TODO: Revisit this. + action(actionFn: CommandActionFn): this; } -declare interface Mode { +export interface Mode { description(desc: string): this; delimiter(str: string): this; @@ -112,6 +141,9 @@ declare interface Mode { action(actionFn: (command: string, callback: CallbackFunction) => void | T): this; } -declare module "vorpal" { - export = VorpalInstance; -} +type CommandParseFn = (command: string, args) => string; // TODO: Check args type. +type CommandValidateFn = (args) => boolean | string; // TODO: Check args type. +type CommandAutocompleteFn = (text: string, iteration: number, cb?: CallbackFunction) => void | PromiseLike; +type CommandActionFn = (args, cb?: CallbackFunction) => void | PromiseLike + +export var Vorpal: VorpalFactory; \ No newline at end of file