Skip to content

Commit

Permalink
fix(cli): ensure user-supplied args override default transformer config
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Feb 3, 2020
1 parent 097eee4 commit a3e3d6a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/cli/src/commands/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,28 @@ 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)
),
_.omit(['$0', 'config', '_'], argv)
])
);
// @ts-ignore
if (_.isEmpty(config.transformer)) {
// @ts-ignore
delete config.transformer;
}
createDebugger(
'cli',
'commands',
Expand Down
31 changes: 31 additions & 0 deletions packages/cli/test/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
);
});
});
});
});
2 changes: 2 additions & 0 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -130,6 +131,7 @@ export const _ = {
lowerCase,
map,
mapKeys,
mapValues,
memoize,
merge,
mergeAll,
Expand Down

0 comments on commit a3e3d6a

Please sign in to comment.