Skip to content

Commit 4f75f10

Browse files
erikkempermanphated
authored andcommitted
New: Add flag & config option for running tasks in series (closes #93)
1 parent 9df91cc commit 4f75f10

File tree

17 files changed

+191
-10
lines changed

17 files changed

+191
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Supported configurations properties:
111111
| flags.tasksDepth | Set default depth of task dependency tree. |
112112
| flags.gulpfile | Set a default gulpfile |
113113
| flags.silent | Silence logging by default |
114+
| flags.series | Run tasks given on the CLI in series (the default is parallel) |
114115

115116
## Flags
116117

@@ -207,6 +208,11 @@ __Some flags only work with gulp 4 and will be ignored when invoked against gulp
207208
<td></td>
208209
<td>Continue execution of tasks upon failure.</td>
209210
</tr>
211+
<tr>
212+
<td>--series</td>
213+
<td></td>
214+
<td>Run tasks given on the CLI in series (the default is parallel).</td>
215+
</tr>
210216
<tr>
211217
<td>--log-level</td>
212218
<td>-L</td>

lib/shared/cliOptions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ module.exports = {
9999
desc: ansi.gray(
100100
'Continue execution of tasks upon failure.'),
101101
},
102+
series: {
103+
type: 'boolean',
104+
desc: ansi.gray(
105+
'Run tasks given on the CLI in series (the default is parallel).'),
106+
},
102107
'log-level': {
103108
alias: 'L',
104109
// Type isn't needed because count acts as a boolean

lib/shared/config/cli-flags.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var copyProps = require('copy-props');
55
var fromTo = {
66
'flags.silent': 'silent',
77
'flags.continue': 'continue',
8+
'flags.series': 'series',
89
'flags.logLevel': 'logLevel',
910
'flags.compactTasks': 'compactTasks',
1011
'flags.tasksDepth': 'tasksDepth',

lib/versioned/^4.0.0-alpha.1/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function execute(opts, env, config) {
7474
}
7575
try {
7676
log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
77-
gulpInst.parallel(toRun)(function(err) {
77+
var runMethod = opts.series ? 'series' : 'parallel';
78+
gulpInst[runMethod](toRun)(function(err) {
7879
if (err) {
7980
exit(1);
8081
}

lib/versioned/^4.0.0-alpha.2/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function execute(opts, env, config) {
7474
}
7575
try {
7676
log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
77-
gulpInst.parallel(toRun)(function(err) {
77+
var runMethod = opts.series ? 'series' : 'parallel';
78+
gulpInst[runMethod](toRun)(function(err) {
7879
if (err) {
7980
exit(1);
8081
}

lib/versioned/^4.0.0/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function execute(opts, env, config) {
7474
}
7575
try {
7676
log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
77-
gulpInst.parallel(toRun)(function(err) {
77+
var runMethod = opts.series ? 'series' : 'parallel';
78+
gulpInst[runMethod](toRun)(function(err) {
7879
if (err) {
7980
exit(1);
8081
}

test/config-flags-series.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
var path = require('path');
5+
6+
var skipLines = require('gulp-test-tools').skipLines;
7+
var eraseTime = require('gulp-test-tools').eraseTime;
8+
var eraseLapse = require('gulp-test-tools').eraseLapse;
9+
var runner = require('gulp-test-tools').gulpRunner;
10+
11+
var fixturesDir = path.join(__dirname, 'fixtures/config');
12+
var runner = require('gulp-test-tools').gulpRunner({ verbose: false }).basedir(fixturesDir);
13+
14+
describe('config: flags.series', function() {
15+
16+
it('Should run in series if `flags.series` is true in .gulp.*',
17+
function(done) {
18+
runner
19+
.chdir('flags/series/t')
20+
.gulp('task1 task2')
21+
.run(cb);
22+
23+
function cb(err, stdout, stderr) {
24+
expect(err).toEqual(null);
25+
expect(stderr).toEqual('');
26+
27+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
28+
expect(stdout).toEqual(
29+
'Starting \'task1\'...\n' +
30+
'Finished \'task1\' after ?\n' +
31+
'Starting \'task2\'...\n' +
32+
'Finished \'task2\' after ?\n' +
33+
''
34+
);
35+
done();
36+
}
37+
});
38+
39+
it('Should run in parallel if `flags.series` is false in .gulp.*',
40+
function(done) {
41+
runner
42+
.chdir('flags/series/f')
43+
.gulp('task1 task2')
44+
.run(cb);
45+
46+
function cb(err, stdout, stderr) {
47+
expect(err).toEqual(null);
48+
expect(stderr).toEqual('');
49+
50+
stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
51+
expect(stdout).toEqual(
52+
'Starting \'task1\'...\n' +
53+
'Starting \'task2\'...\n' +
54+
'Finished \'task2\' after ?\n' +
55+
'Finished \'task1\' after ?\n' +
56+
''
57+
);
58+
done();
59+
}
60+
});
61+
62+
});

test/expected/flags-help.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Options:
3131
colors, even when color support is detected. [boolean]
3232
--silent, -S Suppress all gulp logging. [boolean]
3333
--continue Continue execution of tasks upon failure. [boolean]
34+
--series Run tasks given on the CLI in series (the default is
35+
parallel). [boolean]
3436
--log-level, -L Set the loglevel. -L for least verbose and -LLLL for
3537
most verbose. -LLL is default. [count]
3638

test/expected/flags-tasks-json.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"label":"gulp-cli/test/fixtures/gulpfiles","nodes":[{"label":"test1","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"noop","type":"function","nodes":[]}]}]},{"label":"test2","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"test1","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"noop","type":"function","nodes":[]}]}]},{"label":"noop","type":"function","nodes":[]}]}]},{"label":"test3","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"described","type":"function","nodes":[]}]}]},{"label":"test4","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"errorFunction","type":"function","nodes":[]},{"label":"anon","type":"function","nodes":[]}]}]},{"label":"default","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"test1","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"noop","type":"function","nodes":[]}]}]},{"label":"test3","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"described","type":"function","nodes":[]}]}]},{"label":"noop","type":"function","nodes":[]}]}]}]}
1+
{"label":"gulp-cli/test/fixtures/gulpfiles","nodes":[{"label":"test1","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"noop","type":"function","nodes":[]}]}]},{"label":"test2","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"test1","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"noop","type":"function","nodes":[]}]}]},{"label":"noop","type":"function","nodes":[]}]}]},{"label":"test3","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"described","type":"function","nodes":[]}]}]},{"label":"test4","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"errorFunction","type":"function","nodes":[]},{"label":"anon","type":"function","nodes":[]}]}]},{"label":"test5","nodes":[],"type":"task"},{"label":"test6","nodes":[],"type":"task"},{"label":"default","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"test1","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"noop","type":"function","nodes":[]}]}]},{"label":"test3","type":"task","nodes":[{"label":"<series>","type":"function","branch":true,"nodes":[{"label":"described","type":"function","nodes":[]}]}]},{"label":"noop","type":"function","nodes":[]}]}]}]}

test/expected/flags-tasks-simple.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ test1
22
test2
33
test3
44
test4
5+
test5
6+
test6
57
default

test/expected/flags-tasks.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ gulp-cli/test/fixtures/gulpfiles
2020
├─┬ test3
2121
│ └─┬ <series>
2222
│ └── described
23-
└─┬ test4
24-
└─┬ <series>
25-
├── errorFunction
26-
└── anon
23+
├─┬ test4
24+
│ └─┬ <series>
25+
│ ├── errorFunction
26+
│ └── anon
27+
├── test5
28+
└── test6
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"flags": {
3+
"series": false
4+
}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var gulp = require('gulp');
4+
5+
function noop(cb) {
6+
cb();
7+
}
8+
9+
function delayed(cb) {
10+
setTimeout(cb, 100);
11+
}
12+
13+
gulp.task('task1', delayed);
14+
gulp.task('task2', noop);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"flags": {
3+
"series": true
4+
}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var gulp = require('gulp');
4+
5+
function noop(cb) {
6+
cb();
7+
}
8+
9+
function delayed(cb) {
10+
setTimeout(cb, 100);
11+
}
12+
13+
gulp.task('task1', delayed);
14+
gulp.task('task2', noop);

test/fixtures/gulpfiles/gulpfile.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@
33
var gulp = require('gulp');
44

55
function noop(cb) {
6-
return cb();
6+
cb();
77
}
8+
89
function described(cb) {
910
cb();
1011
}
12+
13+
function delayed(cb) {
14+
setTimeout(cb, 100);
15+
}
16+
1117
function errorFunction() {
1218
throw new Error('Error!');
1319
}
1420
function anon(cb) {
15-
return cb();
21+
cb();
1622
}
1723
described.description = 'description';
1824

1925
gulp.task('test1', gulp.series(noop));
2026
gulp.task('test2', gulp.series('test1', noop));
2127
gulp.task('test3', gulp.series(described));
2228
gulp.task('test4', gulp.series(errorFunction, anon));
29+
gulp.task('test5', delayed);
30+
gulp.task('test6', noop);
2331

2432
gulp.task('default', gulp.series('test1', 'test3', noop));

test/flags-series.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
var runner = require('gulp-test-tools').gulpRunner;
5+
var eraseTime = require('gulp-test-tools').eraseTime;
6+
var eraseLapse = require('gulp-test-tools').eraseLapse;
7+
var skipLines = require('gulp-test-tools').skipLines;
8+
9+
describe('flag: --series', function() {
10+
11+
it('runs tasks in series when flag is set', function(done) {
12+
runner({ verbose: false })
13+
.gulp('test5 test6', '--series', '--cwd ./test/fixtures/gulpfiles')
14+
.run(cb);
15+
16+
function cb(err, stdout, stderr) {
17+
expect(err).toEqual(null);
18+
expect(stderr).toEqual('');
19+
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
20+
expect(stdout).toEqual(
21+
'Starting \'test5\'...\n' +
22+
'Finished \'test5\' after ?\n' +
23+
'Starting \'test6\'...\n' +
24+
'Finished \'test6\' after ?\n' +
25+
''
26+
);
27+
done();
28+
}
29+
});
30+
31+
it('runs tasks in parallel when flag is not set', function(done) {
32+
runner({ verbose: false })
33+
.gulp('test5 test6', '--cwd ./test/fixtures/gulpfiles')
34+
.run(cb);
35+
36+
function cb(err, stdout, stderr) {
37+
expect(err).toEqual(null);
38+
expect(stderr).toEqual('');
39+
expect(stdout).toNotMatch('Starting \'anon\'');
40+
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
41+
expect(stdout).toEqual(
42+
'Starting \'test5\'...\n' +
43+
'Starting \'test6\'...\n' +
44+
'Finished \'test6\' after ?\n' +
45+
'Finished \'test5\' after ?\n' +
46+
''
47+
);
48+
done();
49+
}
50+
});
51+
52+
});

0 commit comments

Comments
 (0)