From a3e3d6ab2a70729c42e9c3c84ac708a80937d034 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Mon, 3 Feb 2020 12:20:13 -0800 Subject: [PATCH] fix(cli): ensure user-supplied args override default transformer config --- packages/cli/src/commands/common.js | 15 ++++++++++++++ packages/cli/test/common.spec.js | 31 +++++++++++++++++++++++++++++ packages/common/src/util.js | 2 ++ 3 files changed, 48 insertions(+) diff --git a/packages/cli/src/commands/common.js b/packages/cli/src/commands/common.js index 6e80cb3..2ff771a 100644 --- a/packages/cli/src/commands/common.js +++ b/packages/cli/src/commands/common.js @@ -155,6 +155,16 @@ export const mergeCommandConfig = ( commandName, _.mergeAll([ _.defaultsDeep(defaultConfig, _.getOr({}, commandName, defaultConfig)), + { + transformer: _.mapValues( + transformerConfig => + _.defaultsDeep( + transformerConfig, + _.omit(['$0', 'config', '_'], argv) + ), + _.getOr({}, 'transformer', defaultConfig) + ) + }, _.defaultsDeep( _.getOr({}, 'config', argv), _.getOr({}, `config.${commandName}`, argv) @@ -162,6 +172,11 @@ export const mergeCommandConfig = ( _.omit(['$0', 'config', '_'], argv) ]) ); + // @ts-ignore + if (_.isEmpty(config.transformer)) { + // @ts-ignore + delete config.transformer; + } createDebugger( 'cli', 'commands', diff --git a/packages/cli/test/common.spec.js b/packages/cli/test/common.spec.js index ddbd704..96ab4be 100644 --- a/packages/cli/test/common.spec.js +++ b/packages/cli/test/common.spec.js @@ -115,6 +115,37 @@ describe('@report-toolkit/cli:commands:common', function() { } ); }); + + it('should prefer argv over transform-specific config', function() { + const argv = { + config: { + foo: 'baz', + commandName: {foo: 'butts'} + }, + _: ['some', 'positional', 'args'], + 'max-width': 72, + $0: 'report-toolkit', + foo: 'rubberduck' + }; + const defaultConfig = { + foo: 'quux', + commandName: {foo: 'spam'}, + transformer: { + foo: { + 'max-width': 80 + } + } + }; + expect( + mergeCommandConfig('commandName', argv, defaultConfig), + 'to equal', + { + foo: 'rubberduck', + transformer: {foo: {'max-width': 72, foo: 'rubberduck'}}, + 'max-width': 72 + } + ); + }); }); }); }); diff --git a/packages/common/src/util.js b/packages/common/src/util.js index 2c5f7ae..bcf0c19 100644 --- a/packages/common/src/util.js +++ b/packages/common/src/util.js @@ -39,6 +39,7 @@ import keys from 'lodash/fp/keys.js'; import lowerCase from 'lodash/fp/lowerCase.js'; import map from 'lodash/fp/map.js'; import mapKeys from 'lodash/fp/mapKeys.js'; +import mapValues from 'lodash/fp/mapValues.js'; import memoize from 'lodash/fp/memoize.js'; import merge from 'lodash/fp/merge.js'; import mergeAll from 'lodash/fp/mergeAll.js'; @@ -130,6 +131,7 @@ export const _ = { lowerCase, map, mapKeys, + mapValues, memoize, merge, mergeAll,