Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: update config
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 19, 2018
1 parent 78c5335 commit 4590963
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
58 changes: 34 additions & 24 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -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 = <T extends Command>(this: ICommandClass<T>, argv?: string[], config?: IConfig | ConfigOptions) => Promise<T>
export interface Options {
root?: string
}

export type CommandRunFn = <T extends Command>(this: ICommandClass<T>, argv?: string[], opts?: Options) => Promise<T>

export interface ICommandClass<T extends Command> {
run: CommandRunFn
new (config: IConfig): T
new (): T
}

const parentModule = module.parent && module.parent.parent && module.parent.parent.filename
Expand All @@ -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 } = {}
Expand All @@ -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<void>

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 {
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4590963

Please sign in to comment.