Skip to content

Commit

Permalink
New: Add flag & config option for running tasks in series (closes #93)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkemperman authored and phated committed Dec 21, 2017
1 parent 9df91cc commit 4f75f10
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Supported configurations properties:
| flags.tasksDepth | Set default depth of task dependency tree. |
| flags.gulpfile | Set a default gulpfile |
| flags.silent | Silence logging by default |
| flags.series | Run tasks given on the CLI in series (the default is parallel) |

## Flags

Expand Down Expand Up @@ -207,6 +208,11 @@ __Some flags only work with gulp 4 and will be ignored when invoked against gulp
<td></td>
<td>Continue execution of tasks upon failure.</td>
</tr>
<tr>
<td>--series</td>
<td></td>
<td>Run tasks given on the CLI in series (the default is parallel).</td>
</tr>
<tr>
<td>--log-level</td>
<td>-L</td>
Expand Down
5 changes: 5 additions & 0 deletions lib/shared/cliOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ module.exports = {
desc: ansi.gray(
'Continue execution of tasks upon failure.'),
},
series: {
type: 'boolean',
desc: ansi.gray(
'Run tasks given on the CLI in series (the default is parallel).'),
},
'log-level': {
alias: 'L',
// Type isn't needed because count acts as a boolean
Expand Down
1 change: 1 addition & 0 deletions lib/shared/config/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var copyProps = require('copy-props');
var fromTo = {
'flags.silent': 'silent',
'flags.continue': 'continue',
'flags.series': 'series',
'flags.logLevel': 'logLevel',
'flags.compactTasks': 'compactTasks',
'flags.tasksDepth': 'tasksDepth',
Expand Down
3 changes: 2 additions & 1 deletion lib/versioned/^4.0.0-alpha.1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function execute(opts, env, config) {
}
try {
log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
gulpInst.parallel(toRun)(function(err) {
var runMethod = opts.series ? 'series' : 'parallel';
gulpInst[runMethod](toRun)(function(err) {
if (err) {
exit(1);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/versioned/^4.0.0-alpha.2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function execute(opts, env, config) {
}
try {
log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
gulpInst.parallel(toRun)(function(err) {
var runMethod = opts.series ? 'series' : 'parallel';
gulpInst[runMethod](toRun)(function(err) {
if (err) {
exit(1);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function execute(opts, env, config) {
}
try {
log.info('Using gulpfile', ansi.magenta(tildify(env.configPath)));
gulpInst.parallel(toRun)(function(err) {
var runMethod = opts.series ? 'series' : 'parallel';
gulpInst[runMethod](toRun)(function(err) {
if (err) {
exit(1);
}
Expand Down
62 changes: 62 additions & 0 deletions test/config-flags-series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

var expect = require('expect');
var path = require('path');

var skipLines = require('gulp-test-tools').skipLines;
var eraseTime = require('gulp-test-tools').eraseTime;
var eraseLapse = require('gulp-test-tools').eraseLapse;
var runner = require('gulp-test-tools').gulpRunner;

var fixturesDir = path.join(__dirname, 'fixtures/config');
var runner = require('gulp-test-tools').gulpRunner({ verbose: false }).basedir(fixturesDir);

describe('config: flags.series', function() {

it('Should run in series if `flags.series` is true in .gulp.*',
function(done) {
runner
.chdir('flags/series/t')
.gulp('task1 task2')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');

stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
expect(stdout).toEqual(
'Starting \'task1\'...\n' +
'Finished \'task1\' after ?\n' +
'Starting \'task2\'...\n' +
'Finished \'task2\' after ?\n' +
''
);
done();
}
});

it('Should run in parallel if `flags.series` is false in .gulp.*',
function(done) {
runner
.chdir('flags/series/f')
.gulp('task1 task2')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');

stdout = eraseLapse(eraseTime(skipLines(stdout, 1)));
expect(stdout).toEqual(
'Starting \'task1\'...\n' +
'Starting \'task2\'...\n' +
'Finished \'task2\' after ?\n' +
'Finished \'task1\' after ?\n' +
''
);
done();
}
});

});
2 changes: 2 additions & 0 deletions test/expected/flags-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Options:
colors, even when color support is detected. [boolean]
--silent, -S Suppress all gulp logging. [boolean]
--continue Continue execution of tasks upon failure. [boolean]
--series Run tasks given on the CLI in series (the default is
parallel). [boolean]
--log-level, -L Set the loglevel. -L for least verbose and -LLLL for
most verbose. -LLL is default. [count]

2 changes: 1 addition & 1 deletion test/expected/flags-tasks-json.json
Original file line number Diff line number Diff line change
@@ -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":[]}]}]}]}
{"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":[]}]}]}]}
2 changes: 2 additions & 0 deletions test/expected/flags-tasks-simple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ test1
test2
test3
test4
test5
test6
default
10 changes: 6 additions & 4 deletions test/expected/flags-tasks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ gulp-cli/test/fixtures/gulpfiles
├─┬ test3
│ └─┬ <series>
│ └── described
└─┬ test4
└─┬ <series>
├── errorFunction
└── anon
├─┬ test4
│ └─┬ <series>
│ ├── errorFunction
│ └── anon
├── test5
└── test6
5 changes: 5 additions & 0 deletions test/fixtures/config/flags/series/f/.gulp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"flags": {
"series": false
}
}
14 changes: 14 additions & 0 deletions test/fixtures/config/flags/series/f/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var gulp = require('gulp');

function noop(cb) {
cb();
}

function delayed(cb) {
setTimeout(cb, 100);
}

gulp.task('task1', delayed);
gulp.task('task2', noop);
5 changes: 5 additions & 0 deletions test/fixtures/config/flags/series/t/.gulp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"flags": {
"series": true
}
}
14 changes: 14 additions & 0 deletions test/fixtures/config/flags/series/t/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var gulp = require('gulp');

function noop(cb) {
cb();
}

function delayed(cb) {
setTimeout(cb, 100);
}

gulp.task('task1', delayed);
gulp.task('task2', noop);
12 changes: 10 additions & 2 deletions test/fixtures/gulpfiles/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@
var gulp = require('gulp');

function noop(cb) {
return cb();
cb();
}

function described(cb) {
cb();
}

function delayed(cb) {
setTimeout(cb, 100);
}

function errorFunction() {
throw new Error('Error!');
}
function anon(cb) {
return cb();
cb();
}
described.description = 'description';

gulp.task('test1', gulp.series(noop));
gulp.task('test2', gulp.series('test1', noop));
gulp.task('test3', gulp.series(described));
gulp.task('test4', gulp.series(errorFunction, anon));
gulp.task('test5', delayed);
gulp.task('test6', noop);

gulp.task('default', gulp.series('test1', 'test3', noop));
52 changes: 52 additions & 0 deletions test/flags-series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var expect = require('expect');
var runner = require('gulp-test-tools').gulpRunner;
var eraseTime = require('gulp-test-tools').eraseTime;
var eraseLapse = require('gulp-test-tools').eraseLapse;
var skipLines = require('gulp-test-tools').skipLines;

describe('flag: --series', function() {

it('runs tasks in series when flag is set', function(done) {
runner({ verbose: false })
.gulp('test5 test6', '--series', '--cwd ./test/fixtures/gulpfiles')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
expect(stdout).toEqual(
'Starting \'test5\'...\n' +
'Finished \'test5\' after ?\n' +
'Starting \'test6\'...\n' +
'Finished \'test6\' after ?\n' +
''
);
done();
}
});

it('runs tasks in parallel when flag is not set', function(done) {
runner({ verbose: false })
.gulp('test5 test6', '--cwd ./test/fixtures/gulpfiles')
.run(cb);

function cb(err, stdout, stderr) {
expect(err).toEqual(null);
expect(stderr).toEqual('');
expect(stdout).toNotMatch('Starting \'anon\'');
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
expect(stdout).toEqual(
'Starting \'test5\'...\n' +
'Starting \'test6\'...\n' +
'Finished \'test6\' after ?\n' +
'Finished \'test5\' after ?\n' +
''
);
done();
}
});

});

0 comments on commit 4f75f10

Please sign in to comment.