From d7fdf71f8c4ec7a6f7c1ff4deb24e67396f6227b Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 7 Apr 2017 10:57:56 +0200 Subject: [PATCH 1/5] feat(commander): improve typyings and fix lint issues --- types/commander/commander-tests.ts | 18 +- types/commander/index.d.ts | 380 +++++++++-------------------- 2 files changed, 128 insertions(+), 270 deletions(-) diff --git a/types/commander/commander-tests.ts b/types/commander/commander-tests.ts index 78bd873782..fe09be7b34 100644 --- a/types/commander/commander-tests.ts +++ b/types/commander/commander-tests.ts @@ -15,20 +15,20 @@ if (program['pineapple']) console.log(' - pineapple'); if (program['bbq']) console.log(' - bbq'); console.log(' - %s cheese', program['cheese']); -function range(val:string) { +function range(val: string) { return val.split('..').map(Number); } -function list(val:string) { +function list(val: string) { return val.split(','); } -function collect(val:string, memo:string[]) { +function collect(val: string, memo: string[]) { memo.push(val); return memo; } -function increaseVerbosity(v:any, total:number) { +function increaseVerbosity(v: any, total: number) { return total + 1; } @@ -80,11 +80,11 @@ program }); program - .version('0.0.1') - .arguments(' [env]') - .action(function (cmd, env) { - console.log(cmd, env); - }); + .version('0.0.1') + .arguments(' [env]') + .action((cmd, env) => { + console.log(cmd, env); + }); program.parse(process.argv); diff --git a/types/commander/index.d.ts b/types/commander/index.d.ts index 15a3246710..e109b618fd 100644 --- a/types/commander/index.d.ts +++ b/types/commander/index.d.ts @@ -1,27 +1,53 @@ -// Type definitions for commanderjs 2.3.0 +// Type definitions for commander 2.9 // Project: https://github.com/visionmedia/commander.js -// Definitions by: Marcelo Dezem , vvakame +// Definitions by: Alan Agius // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -declare var commander: commander.IExportedCommand; -export = commander; - declare namespace commander { - interface ICommandStatic { + + interface CommandOptions { + noHelp?: boolean; + isDefault?: boolean; + } + + interface ParseOptionsResult { + args: string[]; + unknown: string[]; + } + + class Option { + /** + * Initialize a new `Option` with the given `flags` and `description`. + * + * @param {string} flags + * @param {string} [description] + */ + constructor(flags: string, description?: string); + } + + class Command extends NodeJS.EventEmitter { + args: string[]; + /** * Initialize a new `Command`. * - * @param {String} name - * @api public + * @param {string} [name] */ - new (name?:string):ICommand; - } + constructor(name?: string); - interface ICommand extends NodeJS.EventEmitter { - args: string[]; - _args: { required:boolean; name: string; }[]; + /** + * Set the program version to `str`. + * + * This method auto-registers the "-V, --version" flag + * which will print the version number when passed. + * + * @param {string} str + * @param {string} [flags] + * @returns {Command} for chaining + */ + version(str: string, flags?: string): Command; /** * Add command `name`. @@ -35,8 +61,7 @@ declare namespace commander { * will be passed as the first arg, followed by * the rest of __ARGV__ remaining. * - * Examples: - * + * @example * program * .version('0.0.1') * .option('-C, --chdir ', 'change the working directory') @@ -46,75 +71,78 @@ declare namespace commander { * program * .command('setup') * .description('run remote setup commands') - * .action(function(){ + * .action(function() { * console.log('setup'); * }); * * program * .command('exec ') * .description('run the given remote command') - * .action(function(cmd){ + * .action(function(cmd) { * console.log('exec "%s"', cmd); * }); * * program + * .command('teardown [otherDirs...]') + * .description('run teardown commands') + * .action(function(dir, otherDirs) { + * console.log('dir "%s"', dir); + * if (otherDirs) { + * otherDirs.forEach(function (oDir) { + * console.log('dir "%s"', oDir); + * }); + * } + * }); + * + * program * .command('*') * .description('deploy the given env') - * .action(function(env){ + * .action(function(env) { * console.log('deploying "%s"', env); * }); * * program.parse(process.argv); * - * @param {String} name - * @param {String} [desc] - * @param {Mixed} [opts] - * @return {Command} the new command - * @api public + * @param {string} name + * @param {string} [desc] for git-style sub-commands + * @param {CommandOptions} [opts] command options + * @returns {Command} the new command */ - command(name:string, desc?:string, opts?: any):ICommand; + command(name: string, desc?: string, opts?: CommandOptions): Command; + /** - * Set / get the arguments usage `str`. - */ - arguments(str: string):ICommand; - - /** - * Add an implicit `help [cmd]` subcommand - * which invokes `--help` for the given command. + * Define argument syntax for the top-level command. * - * @api private + * @param {string} desc + * @returns {Command} for chaining */ - addImplicitHelpCommand():void; + arguments(desc: string): Command; /** * Parse expected `args`. * * For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`. * - * @param {Array} args - * @return {Command} for chaining - * @api public + * @param {string[]} args + * @returns {Command} for chaining */ - parseExpectedArgs(args:string[]):ICommand; - + parseExpectedArgs(args: string[]): Command; /** * Register callback `fn` for the command. * - * Examples: - * + * @example * program * .command('help') * .description('display verbose help') - * .action(function(){ + * .action(function() { * // output help here * }); * - * @param {Function} fn - * @return {Command} for chaining - * @api public - */ - action(fn:(...args:any[])=>void):ICommand; + * @param {(...args: any[]) => void} fn + * @returns {Command} for chaining + */ + action(fn: (...args: any[]) => void): Command; /** * Define option with `flags`, `description` and optional @@ -128,8 +156,7 @@ declare namespace commander { * "-p|--pepper" * "-p --pepper" * - * Examples: - * + *@example * // simple boolean defaulting to false * program.option('-p, --pepper', 'add pepper'); * @@ -157,260 +184,91 @@ declare namespace commander { * // optional argument * program.option('-c, --cheese [type]', 'add cheese [marble]'); * - * @param {String} flags - * @param {String} description - * @param {Function|Mixed} fn or default - * @param {Mixed} defaultValue - * @return {Command} for chaining - * @api public + * @param {string} flags + * @param {string} [description] + * @param {((arg1: any, arg2: any) => void) | RegExp} [fn] function or default + * @param {*} [defaultValue] + * @returns {Command} for chaining */ - option(flags:string, description?:string, fn?:((arg1:any, arg2:any)=>void)|RegExp, defaultValue?:any):ICommand; - option(flags:string, description?:string, defaultValue?:any):ICommand; + option(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command; + option(flags: string, description?: string, defaultValue?: any): Command; /** * Allow unknown options on the command line. * - * @param {Boolean} arg if `true` or omitted, no error will be thrown - * for unknown options. - * @api public + * @param {boolean} [arg] if `true` or omitted, no error will be thrown for unknown options. + * @returns {Command} for chaining */ - allowUnknownOption(arg?: boolean):ICommand; + allowUnknownOption(arg?: boolean): Command; /** * Parse `argv`, settings options and invoking commands when defined. * - * @param {Array} argv - * @return {Command} for chaining - * @api public + * @param {string[]} argv + * @returns {Command} for chaining */ - parse(argv:string[]):ICommand; + parse(argv: string[]): Command; /** - * Execute a sub-command executable. + * Parse options from `argv` returning `argv` void of these options. * - * @param {Array} argv - * @param {Array} args - * @param {Array} unknown - * @api private + * @param {string[]} argv + * @returns {ParseOptionsResult} */ - executeSubCommand(argv:string[], args:string[], unknown:string[]):any; /* child_process.ChildProcess */ - - /** - * Normalize `args`, splitting joined short flags. For example - * the arg "-abc" is equivalent to "-a -b -c". - * This also normalizes equal sign and splits "--abc=def" into "--abc def". - * - * @param {Array} args - * @return {Array} - * @api private - */ - normalize(args:string[]):string[]; - - /** - * Parse command `args`. - * - * When listener(s) are available those - * callbacks are invoked, otherwise the "*" - * event is emitted and those actions are invoked. - * - * @param {Array} args - * @return {Command} for chaining - * @api private - */ - parseArgs(args:string[], unknown:string[]):ICommand; - - /** - * Return an option matching `arg` if any. - * - * @param {String} arg - * @return {Option} - * @api private - */ - optionFor(arg:string):IOption; - - /** - * Parse options from `argv` returning `argv` - * void of these options. - * - * @param {Array} argv - * @return {Array} - * @api public - */ - parseOptions(argv:string[]): {args:string[]; unknown:string[];}; + parseOptions(argv: string[]): ParseOptionsResult; /** * Return an object containing options as key-value pairs * - * @return {Object} - * @api public + * @returns {{[key: string]: string}} */ - opts():any; - - /** - * Argument `name` is missing. - * - * @param {String} name - * @api private - */ - missingArgument(name:string):void; - - /** - * `Option` is missing an argument, but received `flag` or nothing. - * - * @param {String} option - * @param {String} flag - * @api private - */ - optionMissingArgument(option:{flags:string;}, flag?:string):void; - - /** - * Unknown option `flag`. - * - * @param {String} flag - * @api private - */ - unknownOption(flag:string):void; - - /** - * Set the program version to `str`. - * - * This method auto-registers the "-V, --version" flag - * which will print the version number when passed. - * - * @param {String} str - * @param {String} flags - * @return {Command} for chaining - * @api public - */ - version(str:string, flags?:string):ICommand; + opts(): { [key: string]: string }; /** * Set the description to `str`. * - * @param {String} str - * @return {String|Command} - * @api public + * @param {string} str + * @return {(Command | string)} */ - description(str:string):ICommand; - description():string; + description(str: string): Command; + description(): string; /** - * Set an alias for the command + * Set an alias for the command. * - * @param {String} alias - * @return {String|Command} - * @api public + * @param {string} alias + * @return {(Command | string)} */ - alias(alias:string):ICommand; - alias():string; + alias(alias: string): Command; + alias(): string; /** - * Set / get the command usage `str`. + * Set or get the command usage. * - * @param {String} str - * @return {String|Command} - * @api public + * @param {string} str + * @return {(Command | string)} */ - usage(str:string):ICommand; - usage():string; + usage(str: string): Command; + usage(): string; /** - * Get the name of the command + * Get the name of the command. * - * @param {String} name - * @return {String|Command} - * @api public + * @return {string} */ - name():string; + name(): string; /** - * Return the largest option length. + * Output help information for this command. * - * @return {Number} - * @api private + * @param {() => void} [cb] Callback method */ - largestOptionLength():number; + outputHelp(cb?: () => void): void; - /** - * Return help for options. - * - * @return {String} - * @api private - */ - optionHelp():string; - - /** - * Return command help documentation. - * - * @return {String} - * @api private - */ - commandHelp():string; - - /** - * Return program help documentation. - * - * @return {String} - * @api private - */ - helpInformation():string; - - /** - * Output help information for this command - * - * @api public - */ - outputHelp():void; - - /** - * Output help information and exit. - * - * @api public - */ - help():void; - } - - interface IOptionStatic { - /** - * Initialize a new `Option` with the given `flags` and `description`. - * - * @param {String} flags - * @param {String} description - * @api public - */ - new (flags:string, description?:string):IOption; - } - - interface IOption { - flags:string; - required:boolean; - optional:boolean; - bool:boolean; - short?:string; - long:string; - description:string; - - /** - * Return option name. - * - * @return {String} - * @api private - */ - name():string; - - /** - * Check if `arg` matches the short or long flag. - * - * @param {String} arg - * @return {Boolean} - * @api private - */ - is(arg:string):boolean; - } - - interface IExportedCommand extends ICommand { - Command: commander.ICommandStatic; - Option: commander.IOptionStatic; - [key: string]: any; + /** Output help information and exit. */ + help(): void; } } + +declare var command: commander.Command & { [key: string]: any }; +export = command; From 4c4ffd71988a167025419a662db69a5ba11f4139 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 7 Apr 2017 11:00:00 +0200 Subject: [PATCH 2/5] add definitions by --- types/commander/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/commander/index.d.ts b/types/commander/index.d.ts index e109b618fd..0269cfba4a 100644 --- a/types/commander/index.d.ts +++ b/types/commander/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for commander 2.9 // Project: https://github.com/visionmedia/commander.js -// Definitions by: Alan Agius +// Definitions by: Alan Agius , Marcelo Dezem , vvakame // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// From 550f778e02830258e503c54bca607ce9cd89de55 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 7 Apr 2017 11:01:40 +0200 Subject: [PATCH 3/5] fix export type --- types/commander/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/commander/index.d.ts b/types/commander/index.d.ts index 0269cfba4a..62aa90a536 100644 --- a/types/commander/index.d.ts +++ b/types/commander/index.d.ts @@ -270,5 +270,5 @@ declare namespace commander { } } -declare var command: commander.Command & { [key: string]: any }; +declare var command: commander.Command & commander.Option & { [key: string]: any }; export = command; From fbfa15186b5d4830da1daf10869c4d45992e85e8 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 7 Apr 2017 11:01:58 +0200 Subject: [PATCH 4/5] style(commander): fix lint issues --- types/commander/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/commander/index.d.ts b/types/commander/index.d.ts index 62aa90a536..3f6a29dfe6 100644 --- a/types/commander/index.d.ts +++ b/types/commander/index.d.ts @@ -270,5 +270,5 @@ declare namespace commander { } } -declare var command: commander.Command & commander.Option & { [key: string]: any }; +declare var command: commander.Command & commander.Option & { [key: string]: any }; export = command; From 0c90377d9f05d846cd675fded3aa423514c327b1 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 7 Apr 2017 11:04:45 +0200 Subject: [PATCH 5/5] add option properties --- types/commander/index.d.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/commander/index.d.ts b/types/commander/index.d.ts index 3f6a29dfe6..675ece0417 100644 --- a/types/commander/index.d.ts +++ b/types/commander/index.d.ts @@ -18,6 +18,14 @@ declare namespace commander { } class Option { + flags: string; + required: boolean; + optional: boolean; + bool: boolean; + short?: string; + long: string; + description: string; + /** * Initialize a new `Option` with the given `flags` and `description`. *