Skip to content

Commit 51223da

Browse files
committed
fix: merge config bug
If you didn't set any bools, then version and help would work, but if you added any bools, it would strip the free defaults.
1 parent c4bc16a commit 51223da

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"forin": true,
88
"indent": 2,
99
"noarg": true,
10+
"esversion": 6,
1011
"node": true,
1112
"quotmark": "single",
1213
"undef": true,

lib/args.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function args(argv, config) {
3232

3333
let override = false;
3434

35-
3635
if (res.help) {
3736
override = 'help';
3837
if (res._.length) {

lib/settings.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
module.exports = getSettings;
22

33
const debug = require('debug')('clite');
4-
const defaultsDeep = require('lodash.defaultsdeep');
4+
const mergeWith = require('lodash.mergewith');
55

66
function getSettings(options) {
77
var settings = {};
88
var defaults = require('./defaults')();
99
checkFor('version', options, defaults);
1010
checkFor('help', options, defaults);
1111

12-
defaultsDeep(settings, options, defaults); // clone
12+
mergeWith(settings, defaults, options, customizer);
1313
return settings;
1414
}
1515

16+
function customizer(obj, src) {
17+
if (Array.isArray(obj)) {
18+
return obj.concat(src);
19+
}
20+
}
21+
1622
function checkFor(key, config, defaults) {
1723
if (!config) {
1824
config = {};
@@ -22,7 +28,7 @@ function checkFor(key, config, defaults) {
2228
return;
2329
}
2430

25-
debug('stripping internal command');
31+
debug('stripping internal command: %s', key);
2632

2733
delete defaults.commands[key];
2834
defaults.booleans.splice(defaults.booleans.indexOf(key), 1);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"debug": "^2.2.0",
3737
"es6-promise": "^3.1.2",
3838
"lodash.defaults": "^4.0.1",
39-
"lodash.defaultsdeep": "^4.1.0",
39+
"lodash.defaultsdeep": "^4.3.1",
40+
"lodash.mergewith": "^4.3.1",
4041
"minimist": "^1.2.0",
4142
"then-fs": "^2.0.0",
4243
"update-notifier": "^0.6.0"

test/unit/args.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ test('bools', function (t) {
3131
t.end();
3232
});
3333

34+
test('bools don\'t lose internal defaults', function (t) {
35+
var res = args(sampleArgs.concat('-v'), {
36+
booleans: ['foo', 'bar']
37+
});
38+
t.equal(res.foo, false, 'foo is true');
39+
t.equal(res.version, true, 'version is true');
40+
t.end();
41+
});
42+
3443
test('mixed args', function (t) {
3544
var res = args(sampleArgs.concat('--foo=10', '--bar=20'), {
3645
booleans: ['foo'] // expect true/false
@@ -49,6 +58,7 @@ test('flag shortcuts', function (t) {
4958

5059
test('alias have precedence', function (t) {
5160
var res = args(sampleArgs.concat('-f=10'), { booleans: ['foo'], alias: { f: 'far' } });
61+
5262
t.equal(res.foo, false, 'foo is false');
5363
t.equal(res.far, 10, 'alias won the value');
5464
t.end();

0 commit comments

Comments
 (0)