diff --git a/package.json b/package.json index 8b7bf0b8..e8f7424b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Jeff Dickey @jdxcode", "bugs": "https://github.com/dxcli/command/issues", "dependencies": { - "@dxcli/config": "^0.1.6", + "@dxcli/config": "^0.1.9", "@dxcli/parser": "^0.0.2", "@dxcli/screen": "^0.0.0", "cli-ux": "^3.0.0-alpha.2", diff --git a/src/command.ts b/src/command.ts index f8f3d00b..98aed180 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,16 +1,20 @@ const pjson = require('../package.json') -import {ConfigOptions, IConfig, IPlugin, isIConfig, PluginConfig} from '@dxcli/config' +import * as Config from '@dxcli/config' import {args} from '@dxcli/parser' import {HTTP} from 'http-call' import deps from './deps' import * as flags from './flags' -export type CommandRunFn = (this: ICommandClass, argv?: string[], config?: IConfig | ConfigOptions) => Promise +export interface Options { + root?: string +} + +export type CommandRunFn = (this: ICommandClass, argv?: string[], opts?: Options) => Promise export interface ICommandClass { run: CommandRunFn - new (config: IConfig): T + new (): T } const parentModule = module.parent && module.parent.parent && module.parent.parent.filename @@ -26,26 +30,17 @@ export default abstract class Command { static flags: flags.Input static args: args.IArg[] = [] static _base = `${pjson.name}@${pjson.version}` - static plugin: IPlugin | undefined + static plugin: Config.IPlugin | undefined /** * instantiate and run the command */ - static run: CommandRunFn = async function (argv: string[] = process.argv.slice(2), config: IConfig | ConfigOptions = {}) { - if (!isIConfig(config)) config = await PluginConfig.create({root: parentModule!, ...config}) - const cmd = new this(config as any) - try { - await cmd.init(argv) - await cmd.run() - await cmd.done() - } catch (err) { - // throw HelpErr to allow the CLI to do something with it - if (err.code === 'EHELP') throw err - deps.cli.error(err) - } - return cmd + static run: CommandRunFn = function (argv: string[] = process.argv.slice(2), opts: Options = {}) { + const cmd = new this() + return cmd._run(argv, opts) } + config: Config.IConfig flags: { [name: string]: any } = {} argv: string[] args: { [name: string]: string } = {} @@ -67,18 +62,33 @@ export default abstract class Command { get http(): typeof HTTP { return require('http-call').HTTP } - constructor(protected config: IConfig) { - global['http-call'] = global['http-call'] || {} - global['http-call']!.userAgent = config.userAgent - this.debug = require('debug')(`cli:command:${this.ctor.id || config.name}`) - } - /** * actual command run code goes here */ abstract async run(): Promise - protected async init(argv: string[]) { + protected async _run(argv: string[], opts: Options) { + try { + await this.init(argv, opts) + await this.run() + await this.done() + } catch (err) { + // throw HelpErr to allow the CLI to do something with it + switch (err.code) { + case 'EEXIT': break + case 'EHELP': throw err + default: + deps.cli.error(err) + } + } + return this + } + + protected async init(argv: string[], {root}: {root?: string} = {}) { + this.config = await Config.read({root: root || parentModule!}) + global['http-call'] = global['http-call'] || {} + global['http-call']!.userAgent = this.config.userAgent + this.debug = require('debug')(`cli:command:${this.ctor.id || this.config.name}`) this.debug('init version: %s argv: %o', this.ctor._base, argv) deps.cli.config.errlog = this.config.errlog try { diff --git a/src/index.ts b/src/index.ts index 605b918d..d403bafd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,11 @@ -import Command from './command' +import Command, {Options} from './command' import * as flags from './flags' export * from './errors' export default Command -export {Command, flags} +export { + Command, + Options, + flags, +} diff --git a/yarn.lock b/yarn.lock index 293a9e53..4032eb2c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -106,15 +106,15 @@ dependencies: find-up "^2.1.0" -"@dxcli/config@^0.1.6": - version "0.1.6" - resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.6.tgz#c5787c1d21a150e03c6e3fcf2113cc0efb647a79" +"@dxcli/config@^0.1.9": + version "0.1.9" + resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.9.tgz#f9d033fec9d0236435b60da1d7457c78f48c631e" dependencies: debug "^3.1.0" fs-extra "^5.0.0" load-json-file "^4.0.0" lodash "^4.17.4" - read-pkg-up "^3.0.0" + read-pkg "^3.0.0" "@dxcli/dev-commitmsg@^0.0.3": version "0.0.3"