Skip to content

Commit 1b80d67

Browse files
sttkphated
authored andcommitted
Fix: Prioritize CLI flags over config file properties (fixes #185)
1 parent 33e14d7 commit 1b80d67

17 files changed

+347
-23
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function run() {
9292
var cfgLoadOrder = ['home', 'cwd'];
9393
var cfg = loadConfigFiles(env.configFiles['.gulp'], cfgLoadOrder);
9494
opts = mergeConfigToCliFlags(opts, cfg);
95-
env = mergeConfigToEnvFlags(env, cfg);
95+
env = mergeConfigToEnvFlags(env, cfg, opts);
9696
env.configProps = cfg;
9797

9898
// Set up event listeners for logging again after configuring.

lib/shared/cli-options.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,20 @@ module.exports = {
6262
alias: 'depth',
6363
type: 'number',
6464
requiresArg: true,
65+
default: undefined, // To detect if this cli option is specified.
6566
desc: ansi.gray(
6667
'Specify the depth of the task dependency tree.'),
6768
},
6869
'compact-tasks': {
6970
type: 'boolean',
71+
default: undefined, // To detect if this cli option is specified.
7072
desc: ansi.gray(
7173
'Reduce the output of task dependency tree by printing ' +
7274
'only top tasks and their child tasks.'),
7375
},
7476
'sort-tasks': {
7577
type: 'boolean',
78+
default: undefined, // To detect if this cli option is specified.
7679
desc: ansi.gray(
7780
'Will sort top tasks of task dependency tree.'),
7881
},
@@ -91,24 +94,27 @@ module.exports = {
9194
silent: {
9295
alias: 'S',
9396
type: 'boolean',
97+
default: undefined, // To detect if this cli option is specified.
9498
desc: ansi.gray(
9599
'Suppress all gulp logging.'),
96100
},
97101
continue: {
98102
type: 'boolean',
103+
default: undefined, // To detect if this cli option is specified.
99104
desc: ansi.gray(
100105
'Continue execution of tasks upon failure.'),
101106
},
102107
series: {
103108
type: 'boolean',
109+
default: undefined, // To detect if this cli option is specified.
104110
desc: ansi.gray(
105111
'Run tasks given on the CLI in series (the default is parallel).'),
106112
},
107113
'log-level': {
108114
alias: 'L',
109115
// Type isn't needed because count acts as a boolean
110116
count: true,
111-
// Can't use `default` because it seems to be off by one
117+
default: undefined, // To detect if this cli option is specified.
112118
desc: ansi.gray(
113119
'Set the loglevel. -L for least verbose and -LLLL for most verbose. ' +
114120
'-LLL is default.'),

lib/shared/config/cli-flags.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ var fromTo = {
1313
};
1414

1515
function mergeConfigToCliFlags(opt, config) {
16-
return copyProps(config, opt, fromTo);
16+
return copyProps(config, opt, fromTo, defaults);
17+
}
18+
19+
function defaults(cfgInfo, optInfo) {
20+
if (optInfo.value === undefined) {
21+
return cfgInfo.value;
22+
}
1723
}
1824

1925
module.exports = mergeConfigToCliFlags;

lib/shared/config/env-flags.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@ var toFrom = {
99
require: 'flags.require',
1010
};
1111

12-
function mergeConfigToEnvFlags(env, config) {
13-
return copyProps(env, config, toFrom, convert, true);
14-
}
12+
function mergeConfigToEnvFlags(env, config, cliOpts) {
13+
// This must reverse because `flags.gulpfile` determines 2 different properties
14+
var reverse = true;
15+
return copyProps(env, config, toFrom, convert, reverse);
1516

16-
function convert(configInfo, envInfo) {
17-
if (envInfo.keyChain === 'configBase') {
18-
return path.dirname(configInfo.value);
19-
}
20-
if (envInfo.keyChain === 'require') {
21-
return [].concat(envInfo.value, configInfo.value);
17+
function convert(configInfo, envInfo) {
18+
if (envInfo.keyChain === 'configBase') {
19+
if (cliOpts.gulpfile === undefined) {
20+
return path.dirname(configInfo.value);
21+
}
22+
return;
23+
}
24+
25+
if (envInfo.keyChain === 'configPath') {
26+
if (cliOpts.gulpfile === undefined) {
27+
return configInfo.value;
28+
}
29+
return;
30+
}
31+
32+
if (envInfo.keyChain === 'require') {
33+
return [].concat(envInfo.value, configInfo.value);
34+
}
35+
36+
return configInfo.value;
2237
}
23-
return configInfo.value;
2438
}
2539

2640
module.exports = mergeConfigToEnvFlags;

test/config-flags-compactTasks.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var fixturesDir = path.join(__dirname, 'fixtures/config');
1010
var expectedDir = path.join(__dirname, 'expected');
1111
var runner = require('gulp-test-tools').gulpRunner().basedir(fixturesDir);
1212

13-
describe ('config: flags.compactTasks', function() {
13+
describe('config: flags.compactTasks', function() {
1414

1515
it('Should compact task lists when `flags.compactTasks` is true in .gulp.*',
1616
function(done) {
@@ -52,4 +52,41 @@ describe ('config: flags.compactTasks', function() {
5252
}
5353
});
5454

55+
it('Should overridden by cli flag: --compact-tasks', function(done) {
56+
runner
57+
.chdir('flags/compactTasks/f')
58+
.gulp('--tasks --compact-tasks')
59+
.run(cb);
60+
61+
function cb(err, stdout, stderr) {
62+
var filepath = path.join(expectedDir, 'flags-tasks-compact.txt');
63+
var expected = fs.readFileSync(filepath, 'utf-8');
64+
expected = skipLines(expected, 1);
65+
66+
stdout = eraseTime(skipLines(stdout, 1));
67+
68+
expect(stdout).toEqual(expected);
69+
expect(stderr).toEqual('');
70+
done(err);
71+
}
72+
});
73+
74+
it('Should overridden by cli flag: --no-compact-tasks', function(done) {
75+
runner
76+
.chdir('flags/compactTasks/t')
77+
.gulp('--tasks --no-compact-tasks')
78+
.run(cb);
79+
80+
function cb(err, stdout, stderr) {
81+
var filepath = path.join(expectedDir, 'flags-tasks-unsorted.txt');
82+
var expected = fs.readFileSync(filepath, 'utf-8');
83+
expected = skipLines(expected, 1);
84+
85+
stdout = eraseTime(skipLines(stdout, 1));
86+
87+
expect(stdout).toEqual(expected);
88+
expect(stderr).toEqual('');
89+
done(err);
90+
}
91+
});
5592
});

test/config-flags-continue.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,54 @@ describe('config: flags.continue', function() {
6464
}
6565
});
6666

67+
it('Should overridden by cli flag: --continue', function(done) {
68+
runner
69+
.chdir('flags/continue/f')
70+
.gulp('--continue')
71+
.run(cb);
72+
73+
function cb(err, stdout, stderr) {
74+
expect(err).toNotEqual(null);
75+
76+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
77+
expect(stdout).toEqual(
78+
'Starting \'default\'...\n' +
79+
'Starting \'err\'...\n' +
80+
'Starting \'next\'...\n' +
81+
'Finished \'next\' after ?\n' +
82+
''
83+
);
84+
stderr = eraseLapse(eraseTime(headLines(stderr, 2)));
85+
expect(stderr).toEqual(
86+
'\'err\' errored after ?\n' +
87+
'Error: Error!'
88+
);
89+
done();
90+
}
91+
});
92+
93+
it('Should overridden by cli flag: --no-continue', function(done) {
94+
runner
95+
.chdir('flags/continue/t')
96+
.gulp('--no-continue')
97+
.run(cb);
98+
99+
function cb(err, stdout, stderr) {
100+
expect(err).toNotEqual(null);
101+
102+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
103+
expect(stdout).toEqual(
104+
'Starting \'default\'...\n' +
105+
'Starting \'err\'...\n' +
106+
''
107+
);
108+
stderr = eraseLapse(eraseTime(headLines(stderr, 2)));
109+
expect(stderr).toEqual(
110+
'\'err\' errored after ?\n' +
111+
'Error: Error!'
112+
);
113+
done();
114+
}
115+
});
116+
67117
});

test/config-flags-gulpfile.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ describe('config: flags.gulpfile', function() {
8989
}
9090
});
9191

92+
it('Should overridden by cli flag: --gulpfile', function(done) {
93+
runner
94+
.chdir('./flags/gulpfile/override-by-cliflag')
95+
.gulp('--gulpfile mygulpfile.js')
96+
.run(cb);
97+
98+
function cb(err, stdout, stderr) {
99+
expect(err).toEqual(null);
100+
expect(stderr).toEqual('');
101+
stdout = headLines(stdout, 1, 2);
102+
expect(stdout).toEqual('Gulpfile : ' + path.join(fixturesDir, 'flags/gulpfile/override-by-cliflag/mygulpfile.js'));
103+
done(err);
104+
}
105+
});
106+
92107
it('Should autoload a module for loading a specified gulpfile', function(done) {
93108
this.timeout(0);
94109

test/config-flags-log-level.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,42 @@ describe('config: flag.logLevel', function() {
185185
});
186186
});
187187
});
188+
189+
describe('Overridden by cli flag: -L/-LL/-LLL', function() {
190+
it('Should not output info log by -L', function(done) {
191+
var gulp = runner({ verbose: false })
192+
.basedir(path.join(__dirname, 'fixtures/config/flags/logLevel/LLL'))
193+
.gulp;
194+
195+
var packageJsonPath = path.resolve(__dirname, 'fixtures/packages',
196+
'valid-package.json');
197+
198+
gulp('-L', '--verify', packageJsonPath)
199+
.run(function(err, stdout, stderr) {
200+
expect(err).toEqual(null);
201+
expect(stdout).toEqual('');
202+
expect(stderr).toEqual('');
203+
done(err);
204+
});
205+
});
206+
207+
it('Should output info log by -LLL', function(done) {
208+
var gulp = runner({ verbose: false })
209+
.basedir(path.join(__dirname, 'fixtures/config/flags/logLevel/L'))
210+
.gulp;
211+
212+
var packageJsonPath = path.resolve(__dirname, 'fixtures/packages',
213+
'valid-package.json');
214+
215+
gulp('-LLL', '--verify', packageJsonPath)
216+
.run(function(err, stdout, stderr) {
217+
expect(err).toEqual(null);
218+
expect(eraseTime(stdout)).toEqual(
219+
'Verifying plugins in ' + packageJsonPath + '\n' +
220+
'There are no blacklisted plugins in this project\n');
221+
expect(stderr).toEqual('');
222+
done(err);
223+
});
224+
});
225+
});
188226
});

test/config-flags-series.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,47 @@ describe('config: flags.series', function() {
5858
}
5959
});
6060

61+
it('Should overridden by cli flag: --series', function(done) {
62+
runner
63+
.chdir('flags/series/f')
64+
.gulp('--series', 'task1 task2')
65+
.run(cb);
66+
67+
function cb(err, stdout, stderr) {
68+
expect(err).toEqual(null);
69+
expect(stderr).toEqual('');
70+
71+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
72+
expect(stdout).toEqual(
73+
'Starting \'task1\'...\n' +
74+
'Finished \'task1\' after ?\n' +
75+
'Starting \'task2\'...\n' +
76+
'Finished \'task2\' after ?\n' +
77+
''
78+
);
79+
done();
80+
}
81+
});
82+
83+
it('Should overridden by cli flag: --no-series', function(done) {
84+
runner
85+
.chdir('flags/series/t')
86+
.gulp('--no-series', 'task1 task2')
87+
.run(cb);
88+
89+
function cb(err, stdout, stderr) {
90+
expect(err).toEqual(null);
91+
expect(stderr).toEqual('');
92+
93+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
94+
expect(stdout).toEqual(
95+
'Starting \'task1\'...\n' +
96+
'Starting \'task2\'...\n' +
97+
'Finished \'task2\' after ?\n' +
98+
'Finished \'task1\' after ?\n' +
99+
''
100+
);
101+
done();
102+
}
103+
});
61104
});

test/config-flags-silent.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,37 @@ describe('config: flags.silent', function() {
4646
}
4747
});
4848

49+
it('Should overridden by cli flag: --silent', function(done) {
50+
runner
51+
.chdir('flags/silent/f')
52+
.gulp('--silent')
53+
.run(cb);
54+
55+
function cb(err, stdout, stderr) {
56+
expect(err).toEqual(null);
57+
expect(stderr).toEqual('');
58+
expect(stdout).toEqual('');
59+
done(err);
60+
}
61+
});
62+
63+
it('Should overridden by cli flag: --no-silent', function(done) {
64+
runner
65+
.chdir('flags/silent/t')
66+
.gulp('--no-silent')
67+
.run(cb);
68+
69+
function cb(err, stdout, stderr) {
70+
expect(err).toEqual(null);
71+
expect(stderr).toEqual('');
72+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
73+
expect(stdout).toEqual(
74+
'Starting \'default\'...\n' +
75+
'Finished \'default\' after ?\n' +
76+
''
77+
);
78+
done(err);
79+
}
80+
});
81+
4982
});

0 commit comments

Comments
 (0)