@anycli/command
Advanced tools
Comparing version 0.3.11 to 1.0.0
@@ -0,1 +1,16 @@ | ||
<a name="1.0.0"></a> | ||
# [1.0.0](https://github.com/anycli/command/compare/f3a45a20a89a517d022c90990e96039995dd52b5...v1.0.0) (2018-02-03) | ||
### Features | ||
* simplify command with new config ([1599c40](https://github.com/anycli/command/commit/1599c40)) | ||
### BREAKING CHANGES | ||
* This is preparation for using the new config object that will not | ||
require the engine. It is likely incompatible with any anycli version | ||
before it | ||
<a name="0.3.11"></a> | ||
@@ -2,0 +17,0 @@ ## [0.3.11](https://github.com/anycli/command/compare/57659f6122fefe19ba41804633ac7edea5f007a2...v0.3.11) (2018-02-02) |
import * as Config from '@anycli/config'; | ||
import { args } from '@anycli/parser'; | ||
import * as Parser from '@anycli/parser'; | ||
import * as flags from './flags'; | ||
export default abstract class Command { | ||
argv: string[]; | ||
opts: Config.ICommandOptions; | ||
static _base: string; | ||
@@ -17,3 +16,3 @@ static id: string; | ||
static flags: flags.Input<any>; | ||
static args: args.IArg[]; | ||
static args: Parser.args.IArg[]; | ||
static plugin: Config.IPlugin | undefined; | ||
@@ -24,6 +23,10 @@ static examples: string[] | undefined; | ||
*/ | ||
static run: Config.ICommand['run']; | ||
static load(): Promise<typeof default>; | ||
static convertToCached(this: Config.ICommand, opts?: Config.IConvertToCachedOptions): Config.ICachedCommand; | ||
static run: Config.Command.Full['run']; | ||
config: Config.IConfig; | ||
flags: { | ||
[name: string]: any; | ||
}; | ||
args: { | ||
[name: string]: any; | ||
}; | ||
description: null; | ||
@@ -37,3 +40,3 @@ hidden: null; | ||
protected debug: (...args: any[]) => void; | ||
constructor(argv: string[], opts: Config.ICommandOptions); | ||
constructor(argv: string[], options: Config.Options); | ||
readonly ctor: typeof Command; | ||
@@ -44,5 +47,6 @@ readonly http: any; | ||
*/ | ||
abstract run(): Promise<void>; | ||
protected init(): void; | ||
protected finally(): Promise<void>; | ||
abstract run(): Promise<any>; | ||
protected init(): Promise<void>; | ||
protected catch(err: Error): Promise<void>; | ||
protected finally(_: Error | undefined): Promise<void>; | ||
} |
@@ -5,13 +5,12 @@ "use strict"; | ||
const Config = require("@anycli/config"); | ||
const Parser = require("@anycli/parser"); | ||
const cli_ux_1 = require("cli-ux"); | ||
const _ = require("lodash"); | ||
const cache_1 = require("./cache"); | ||
const flags = require("./flags"); | ||
const g = global; | ||
g.anycli = g.anycli || {}; | ||
const parentModule = module.parent && module.parent.parent && module.parent.parent.filename; | ||
class Command { | ||
constructor(argv, opts) { | ||
constructor(argv, options) { | ||
this.argv = argv; | ||
this.opts = opts; | ||
this.config = opts.config; | ||
this.config = Config.load(options || module.parent && module.parent.filename || __dirname); | ||
this.debug = require('debug')(this.ctor.id ? `${this.config.bin}:${this.ctor.id}` : this.config.bin); | ||
@@ -26,8 +25,3 @@ this.debug('init version: %s argv: %o', this.ctor._base, argv); | ||
g['http-call'].userAgent = this.config.userAgent; | ||
this.init(); | ||
} | ||
static async load() { return this; } | ||
static convertToCached(opts = {}) { | ||
return cache_1.convertToCached(this, opts); | ||
} | ||
get ctor() { | ||
@@ -37,4 +31,17 @@ return this.constructor; | ||
get http() { return require('http-call').HTTP; } | ||
init() { } | ||
async finally() { | ||
async init() { | ||
const o = Parser.parse(this.argv, { | ||
flags: this.ctor.flags, | ||
args: this.ctor.args, | ||
strict: this.ctor.strict || !this.ctor.variableArgs, | ||
context: this, | ||
}); | ||
this.flags = o.flags; | ||
this.args = o.args; | ||
this.argv = o.argv; | ||
} | ||
async catch(err) { | ||
cli_ux_1.default.error(err); | ||
} | ||
async finally(_) { | ||
try { | ||
@@ -51,2 +58,6 @@ await cli_ux_1.default.done(); | ||
Command.strict = true; | ||
Command.flags = { | ||
version: flags.version(), | ||
help: flags.help(), | ||
}; | ||
Command.args = []; | ||
@@ -56,47 +67,40 @@ /** | ||
*/ | ||
Command.run = async function (argv = process.argv.slice(2), opts = {}) { | ||
class HelpErr extends Error { | ||
constructor() { | ||
super(...arguments); | ||
this.code = 'EHELP'; | ||
} | ||
} | ||
class VersionErr extends Error { | ||
constructor() { | ||
super(...arguments); | ||
this.code = 'EVERSION'; | ||
} | ||
} | ||
let config; | ||
if (opts.config && Config.isIConfig(opts.config)) | ||
config = opts.config; | ||
else | ||
config = await Config.read({ root: opts.root || parentModule }); | ||
g.anycli.command = {}; | ||
Command.run = async function (argv = process.argv.slice(2), opts) { | ||
let err; | ||
let cmd; | ||
try { | ||
cmd = new this(argv, Object.assign({}, opts, { config })); | ||
if (g.anycli.command.showVersion) | ||
throw new VersionErr(); | ||
if (g.anycli.command.showHelp) | ||
throw new HelpErr(); | ||
return await cmd.run(); | ||
cmd = new this(argv, opts); | ||
await cmd.init(); | ||
await cmd.run(); | ||
} | ||
catch (err) { | ||
if (err instanceof VersionErr) { | ||
cli_ux_1.default.info(config.userAgent); | ||
} | ||
else if (err instanceof HelpErr || err.message.match(/Unexpected argument: -h/)) { | ||
const Helper = require('@anycli/plugin-help').default; | ||
const help = new Helper(config); | ||
help.showHelp(this, argv); | ||
} | ||
catch (e) { | ||
err = e; | ||
if (cmd) | ||
await cmd.catch(e); | ||
else | ||
cli_ux_1.default.error(err); | ||
cli_ux_1.default.error(e); | ||
} | ||
finally { | ||
if (cmd) | ||
await cmd.finally(); | ||
await cmd.finally(err); | ||
} | ||
// g.anycli.command = {} | ||
// let cmd!: Command | ||
// try { | ||
// cmd = new this(argv, {...opts, config}) | ||
// if (g.anycli.command.showVersion) throw new VersionErr() | ||
// if (g.anycli.command.showHelp) throw new HelpErr() | ||
// return await cmd.run() | ||
// } catch (err) { | ||
// if (err instanceof VersionErr) { | ||
// cli.info(config.userAgent) | ||
// } else if (err instanceof HelpErr || err.message.match(/Unexpected argument: -h/)) { | ||
// const Helper: typeof Help = require('@anycli/plugin-help').default | ||
// const help = new Helper(config) | ||
// help.showHelp(this, argv) | ||
// } else cli.error(err) | ||
// } finally { | ||
// if (cmd) await cmd.finally() | ||
// } | ||
}; | ||
exports.default = Command; |
"use strict"; | ||
// tslint:disable interface-over-type-literal | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Parser = require("@anycli/parser"); | ||
const cli_ux_1 = require("cli-ux"); | ||
function build(defaults) { | ||
@@ -25,12 +25,11 @@ return Parser.flags.build(defaults); | ||
exports.boolean = flags_1.boolean; | ||
const g = global; | ||
exports.version = (opts = {}) => { | ||
return Parser.flags.boolean(Object.assign({ char: 'v', description: 'show CLI version' }, opts, { parse: () => { | ||
g.anycli.command.showVersion = true; | ||
return Parser.flags.boolean(Object.assign({ char: 'v', description: 'show CLI version' }, opts, { parse: (_, cmd) => { | ||
cli_ux_1.default.info(cmd.config.userAgent); | ||
} })); | ||
}; | ||
exports.help = (opts = {}) => { | ||
return Parser.flags.boolean(Object.assign({ char: 'h', description: 'show CLI help' }, opts, { parse: () => { | ||
g.anycli.command.showHelp = true; | ||
return Parser.flags.boolean(Object.assign({ char: 'h', description: 'show CLI help' }, opts, { parse: (_, cmd) => { | ||
cli_ux_1.default.info(cmd); | ||
} })); | ||
}; |
@@ -1,6 +0,6 @@ | ||
export { parse } from '@anycli/parser'; | ||
export { convertToCached } from './cache'; | ||
import * as Config from '@anycli/config'; | ||
import Command from './command'; | ||
import * as flags from './flags'; | ||
export { parse } from '@anycli/parser'; | ||
export default Command; | ||
export { Command, flags }; | ||
export { Config, Command, flags }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var parser_1 = require("@anycli/parser"); | ||
exports.parse = parser_1.parse; | ||
var cache_1 = require("./cache"); | ||
exports.convertToCached = cache_1.convertToCached; | ||
const Config = require("@anycli/config"); | ||
exports.Config = Config; | ||
const command_1 = require("./command"); | ||
@@ -11,2 +9,4 @@ exports.Command = command_1.default; | ||
exports.flags = flags; | ||
var parser_1 = require("@anycli/parser"); | ||
exports.parse = parser_1.parse; | ||
exports.default = command_1.default; |
{ | ||
"name": "@anycli/command", | ||
"description": "anycli base command", | ||
"version": "0.3.11", | ||
"version": "1.0.0", | ||
"author": "Jeff Dickey @jdxcode", | ||
"bugs": "https://github.com/anycli/command/issues", | ||
"dependencies": { | ||
"@anycli/parser": "^3.0.4", | ||
"@anycli/parser": "^3.1.1", | ||
"cli-ux": "^3.3.13", | ||
"debug": "^3.1.0", | ||
"fs-extra": "^5.0.0", | ||
"load-json-file": "^4.0.0", | ||
"lodash": "^4.17.4", | ||
@@ -15,6 +17,8 @@ "tslib": "^1.9.0" | ||
"devDependencies": { | ||
"@anycli/config": "^0.3.1", | ||
"@anycli/config": "^1.0.3", | ||
"@anycli/plugin-help": "^0.5.0", | ||
"@anycli/tslint": "^0.2.2", | ||
"@anycli/tslint": "^0.2.5", | ||
"@types/chai": "^4.1.2", | ||
"@types/fs-extra": "^5.0.0", | ||
"@types/load-json-file": "^2.0.7", | ||
"@types/lodash": "^4.14.100", | ||
@@ -28,4 +32,4 @@ "@types/mocha": "^2.2.48", | ||
"concurrently": "^3.5.1", | ||
"eslint": "^4.16.0", | ||
"eslint-config-anycli": "^1.3.1", | ||
"eslint": "^4.17.0", | ||
"eslint-config-anycli": "^1.3.2", | ||
"fancy-test": "^0.6.6", | ||
@@ -32,0 +36,0 @@ "http-call": "^5.0.2", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
27440
7
22
9
249
+ Addedfs-extra@^5.0.0
+ Addedload-json-file@^4.0.0
+ Addederror-ex@1.3.2(transitive)
+ Addedfs-extra@5.0.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedjson-parse-better-errors@1.0.2(transitive)
+ Addedload-json-file@4.0.0(transitive)
+ Addedparse-json@4.0.0(transitive)
+ Addedpify@3.0.0(transitive)
+ Addedstrip-bom@3.0.0(transitive)
Updated@anycli/parser@^3.1.1