From 6979084b79f1c39ee6b8c2da201f70d6f0077faf Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Sat, 3 Feb 2018 12:17:54 -0800 Subject: [PATCH] fix: put reset flag on loadConfig --- package.json | 4 ++-- src/command.ts | 14 ++------------ src/hook.ts | 4 ++-- src/load_config.ts | 12 ++++++++++-- test/command.test.ts | 12 ++++++------ test/hook.test.ts | 6 +++--- yarn.lock | 20 ++++++++++---------- 7 files changed, 35 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index eab7dd88..5aa3c3ac 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/command.ts b/src/command.ts index 0fe166c9..44fcb90d 100644 --- a/src/command.ts +++ b/src/command.ts @@ -3,13 +3,10 @@ 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(' ')}` @@ -17,10 +14,3 @@ export function command(args: string[] | string | undefined, opts: command.Optio } } } - -export namespace command { - export interface Options { - root?: string - resetConfig?: boolean - } -} diff --git a/src/hook.ts b/src/hook.ts index c6059d60..5d00e9c8 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -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 || {}) } diff --git a/src/load_config.ts b/src/load_config.ts index 734c03c5..688c7dde 100644 --- a/src/load_config.ts +++ b/src/load_config.ts @@ -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 + } } diff --git a/test/command.test.ts b/test/command.test.ts index f23bdd1a..317ef1b4 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -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() }) diff --git a/test/hook.test.ts b/test/hook.test.ts index abce85de..a46866ad 100644 --- a/test/hook.test.ts +++ b/test/hook.test.ts @@ -8,7 +8,7 @@ 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) @@ -16,7 +16,7 @@ describe('hooks', () => { .it('catches -v') test - .loadConfig(root) + .loadConfig({root}) .stdout() .hook('init', {id: '--version'}, {root}) .exit(0) @@ -24,7 +24,7 @@ describe('hooks', () => { .it('catches --version') test - .loadConfig(root) + .loadConfig({root}) .stdout() .hook('init', {}, {root}) .it() diff --git a/yarn.lock b/yarn.lock index ef5267b6..c079f478 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"