Skip to content

Commit

Permalink
fix: put reset flag on loadConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 3, 2018
1 parent 89be852 commit 6979084
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"lodash": "^4.17.4"
},
"devDependencies": {
"@anycli/command": "^1.2.1",
"@anycli/config": "^1.0.15",
"@anycli/command": "^1.2.3",
"@anycli/config": "^1.0.18",
"@anycli/tslint": "^0.2.5",
"@anycli/version": "^0.1.19",
"@types/chai": "^4.1.2",
Expand Down
14 changes: 2 additions & 12 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ import * as _ from 'lodash'

import {loadConfig} from './load_config'

export function command(args: string[] | string | undefined, opts: command.Options = {}) {
export function command(args: string[] | string | undefined, opts: loadConfig.Options = {}) {
return {
async run(ctx: {config: Config.IConfig, expectation: string}) {
if (!ctx.config || opts.resetConfig) {
ctx.config = loadConfig(opts.root).run({} as any)
Config.Plugin.loadedPlugins = {}
}
if (!ctx.config || opts.reset) ctx.config = loadConfig(opts).run({} as any)
args = _.castArray(args)
let [cmd, ...extra] = args
ctx.expectation = ctx.expectation || `runs ${args.join(' ')}`
await ctx.config.runCommand(cmd, extra)
}
}
}

export namespace command {
export interface Options {
root?: string
resetConfig?: boolean
}
}
4 changes: 2 additions & 2 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {loadConfig} from './load_config'
* @param event - hook to run
* @param hookOpts - options to pass to hook. Config object will be passed automatically.
*/
export default (event?: string, hookOpts: object = {}) => ({
export default (event?: string, hookOpts: object = {}, options: loadConfig.Options = {}) => ({
async run(ctx: {config: Config.IConfig, expectation: string}) {
if (!event) throw new Error('no hook provided')
if (!ctx.config) ctx.config = loadConfig().run({} as any)
if (!ctx.config) ctx.config = loadConfig(options).run({} as any)
ctx.expectation = ctx.expectation || `runs ${event} hook`
await ctx.config.runHook(event, hookOpts || {})
}
Expand Down
12 changes: 10 additions & 2 deletions src/load_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import * as Config from '@anycli/config'
/**
* loads CLI plugin/multi config
*/
export function loadConfig(root?: string) {
export function loadConfig(opts: loadConfig.Options = {}) {
return {
run(ctx: {config: Config.IConfig}) {
return ctx.config = Config.load(root || loadConfig.root)
ctx.config = Config.load(opts.root || loadConfig.root)
if (opts.reset) {
Config.Plugin.loadedPlugins = {}
}
return ctx.config
}
}
}

export namespace loadConfig {
export let root: string
export interface Options {
root?: string
reset?: boolean
}
}
12 changes: 6 additions & 6 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ const root = path.join(__dirname, 'fixtures/multi')

describe('command', () => {
test
.loadConfig(root)
.loadConfig({root})
.stdout()
.command(['foo:bar'], {root})
.command(['foo:bar'])
.do(output => expect(output.stdout).to.equal('hello world!\n'))
.it()

test
.loadConfig(root)
.loadConfig({root})
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.command(['foo:bar', '--name=foo'])
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
.it()

test
.loadConfig(root)
.loadConfig({root})
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.command(['foo:bar', '--name=foo'])
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
.it()
})
6 changes: 3 additions & 3 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ describe('hooks', () => {
const stdout = `test/0.0.0 (${process.platform}-${process.arch}) node-${process.version}\n`

test
.loadConfig(root)
.loadConfig({root})
.stdout()
.hook('init', {id: '-v'}, {root})
.exit(0)
.do(output => expect(output.stdout).to.equal(stdout))
.it('catches -v')

test
.loadConfig(root)
.loadConfig({root})
.stdout()
.hook('init', {id: '--version'}, {root})
.exit(0)
.do(output => expect(output.stdout).to.equal(stdout))
.it('catches --version')

test
.loadConfig(root)
.loadConfig({root})
.stdout()
.hook('init', {}, {root})
.it()
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
lodash "^4.17.4"
tslib "^1.9.0"

"@anycli/command@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@anycli/command/-/command-1.2.1.tgz#026ee48b85c53850bfef28f1a08917f489f110d5"
"@anycli/command@^1.2.3":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@anycli/command/-/command-1.2.3.tgz#b239669674f98098b1af096f081958b270fab0b6"
dependencies:
"@anycli/parser" "^3.2.1"
"@anycli/parser" "^3.2.2"
cli-ux "^3.3.13"
debug "^3.1.0"
fs-extra "^5.0.0"
Expand All @@ -35,9 +35,9 @@
lodash "^4.17.4"
read-pkg "^3.0.0"

"@anycli/config@^1.0.15":
version "1.0.15"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-1.0.15.tgz#72f175df4be6c1fd4c8630c28d2c7243dac3c81c"
"@anycli/config@^1.0.18":
version "1.0.18"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-1.0.18.tgz#f6f80403783a78eeebccc5f218cd07485308997c"
dependencies:
cli-ux "^3.3.13"
debug "^3.1.0"
Expand All @@ -56,9 +56,9 @@
chalk "^2.3.0"
lodash "^4.17.4"

"@anycli/parser@^3.2.1":
version "3.2.1"
resolved "https://registry.yarnpkg.com/@anycli/parser/-/parser-3.2.1.tgz#89fe1c37f8792d6c1cba7d1c4ec2fb42b290ce05"
"@anycli/parser@^3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@anycli/parser/-/parser-3.2.2.tgz#ee2616e889e7dd78bc269638b7eee3f3096e2c91"
dependencies:
"@anycli/screen" "^0.0.3"
chalk "^2.3.0"
Expand Down

0 comments on commit 6979084

Please sign in to comment.