Added Command methods. Got tests working.

This commit is contained in:
Jim Buck
2017-11-28 09:45:50 -05:00
parent b72b21bf19
commit 858f2ba10d
2 changed files with 108 additions and 34 deletions
+46 -4
View File
@@ -1,8 +1,50 @@
///<reference path="vorpal.d.ts"/>
///<reference path="./vorpal.d.ts"/>
let vorpal = require('vorpal');
import {Vorpal} from './vorpal';
let app: VorpalInstance = vorpal();
declare namespace app {
function execSQL(...args: any[]): PromiseLike<any>;
}
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 <query>', '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 <instance>', 'Switches prompt to another instance.')
.action(function (args, cb) {
instances[args.instance].show();
cb();
})
}
instances[0].show();
+62 -30
View File
@@ -4,23 +4,38 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../inquirer/inquirer.d.ts" />
/// <reference path="../minimist/minimist.d.ts" />
declare type CallbackFunction<T> = (err: string | Error, data: T) => void;
import {} from 'inquirer';
import {ParsedArgs} from 'minimist';
export type CallbackFunction<T> = (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<T>(command: string, cb: CallbackFunction<T>): PromiseLike<T>;
execSync<T>(command: string, options: {fatal: boolean}): T;
execSync<T>(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<T>(choicesFn: CommandAutocompleteFn<T>): this; // TODO: Revisit this.
action<T>(actionFn: CommandActionFn<T>): this;
}
declare interface Mode {
export interface Mode {
description(desc: string): this;
delimiter(str: string): this;
@@ -112,6 +141,9 @@ declare interface Mode {
action<T>(actionFn: (command: string, callback: CallbackFunction<void | T>) => 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<T> = (text: string, iteration: number, cb?: CallbackFunction<T>) => void | PromiseLike<T>;
type CommandActionFn<T> = (args, cb?: CallbackFunction<T>) => void | PromiseLike<T>
export var Vorpal: VorpalFactory;