Skip to content

Commit

Permalink
fix: fixed config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent e6bd86e commit 11b7668
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@dxcli/engine": "^0.1.12",
"@dxcli/loader": "^0.2.8",
"@dxcli/semantic-release": "^0.3.3",
"@dxcli/tslint": "^0.0.23",
"@dxcli/tslint": "^0.0.24",
"@dxcli/version": "^0.1.6",
"chai": "^4.1.2",
"cli-ux": "^3.2.2",
Expand Down
3 changes: 3 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {IConfig} from '@dxcli/config'
import run from '@dxcli/engine'
import * as _ from 'lodash'

import loadConfig from './load_config'

export default (args: string[] | string | undefined) => ({
async run(ctx: {config: IConfig, expectation: string}) {
if (!ctx.config) ctx.config = await loadConfig().run({} as any)
args = _.castArray(args)
ctx.expectation = ctx.expectation || `runs ${args.join(' ')}`
await run(args, ctx.config)
Expand Down
3 changes: 3 additions & 0 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {IConfig} from '@dxcli/config'
import {Engine} from '@dxcli/engine'

import loadConfig from './load_config'

/**
* tests a dxcli hook
*
Expand All @@ -14,6 +16,7 @@ import {Engine} from '@dxcli/engine'
*/
export default (event?: string, hookOpts: object = {}) => ({
async run(ctx: {config: IConfig, expectation: string}) {
if (!ctx.config) ctx.config = await loadConfig().run({} as any)
ctx.expectation = ctx.expectation || `runs ${event} hook`
const engine = new Engine()
await engine.load(ctx.config.root)
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import exit from './exit'
import hook from './hook'
import loadConfig from './load_config'

loadConfig.root = module.parent!.filename

export const test = fancy
.register('loadConfig', loadConfig)
.register('command', command)
Expand Down
18 changes: 11 additions & 7 deletions src/load_config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as path from 'path'

import {IConfig} from '@dxcli/config'
import {load} from '@dxcli/loader'

export type WithRoot<T> = T & {root: string}

/**
* loads CLI plugin/multi config
*/
export default (opts: {root?: string} = {}) => ({
async run(ctx: {config: IConfig}) {
const {config} = await load({root: opts.root || path.dirname(module.parent!.filename)})
ctx.config = config
export const _loadConfig = (opts: {root?: string} = {}) => {
return {
async run(ctx: {config: IConfig}) {
const {config} = await load({root: opts.root || loadConfig.root})
return ctx.config = config
}
}
})
}
const loadConfig = _loadConfig as WithRoot<typeof _loadConfig>
export default loadConfig
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@
execa "^0.9.0"
semantic-release "^12.2.5"

"@dxcli/tslint@^0.0.23":
version "0.0.23"
resolved "https://registry.yarnpkg.com/@dxcli/tslint/-/tslint-0.0.23.tgz#c9877ec9927bb958c1a71edac3263007ce64b22d"
"@dxcli/tslint@^0.0.24":
version "0.0.24"
resolved "https://registry.yarnpkg.com/@dxcli/tslint/-/tslint-0.0.24.tgz#32c19a574828d643bcb446cbfc0b2dbbbf1c1da0"
dependencies:
tslint "^5.9.1"
tslint-xo "^0.5.0"
Expand Down

0 comments on commit 11b7668

Please sign in to comment.