Skip to content

Commit

Permalink
Added options, code cleanup
Browse files Browse the repository at this point in the history
Closes #92
  • Loading branch information
OverZealous committed Jul 24, 2017
1 parent 578d017 commit f2b1635
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 112 deletions.
31 changes: 31 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"env": {
"node": true
},
"plugins": [],
"extends": ["eslint:recommended"],
"rules": {
"camelcase": 2,
"comma-dangle": 0,
"comma-spacing": 0,
"curly": 0,
"eqeqeq": 2,
"key-spacing": 0,
"new-cap": [ 2, { "capIsNew": false } ],
"no-empty": ["error", { "allowEmptyCatch": true }],
"no-extra-semi": 0,
"no-extra-strict": 0,
"no-irregular-whitespace": 2,
"no-new": 2,
"no-shadow": 0,
"no-undef": 2,
"no-underscore-dangle": 0,
"no-use-before-define": [ 2, "nofunc" ],
"quotes": 0,
"semi": 0,
"strict": 0,
"wrap-iife": [ 2, "inside" ],
"yoda": 0
},
"globals": {}
}
27 changes: 24 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,41 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2017-07-30
## [2.1.0](https://github.com/OverZealous/run-sequence/releases/tag/v2.1.0) - 2017-07-24

### Changed

- Added options object
- Added option for reduced stack trace reporting
- Added option to ignore falsey task names

## [2.0.0](https://github.com/OverZealous/run-sequence/releases/tag/v2.0.0) - 2017-06-30

### Changed

- Specified version numbers for all dependencies, due to Chalk dropping support for older Node versions

This may be a **breaking change** if you depend on a newer release of any dependency, so you can continue using 1.2.2 in that case.

## [1.2.2] - 2016-06-29
## [1.2.2](https://github.com/OverZealous/run-sequence/releases/tag/v1.2.2) - 2016-06-29

### Changed

- Now passes the error back to GulpUtil.PluginError

## [Older]
## [1.0.0](https://github.com/OverZealous/run-sequence/releases/tag/v1.0.0) - ???

### Possible Breaking Change in version 1.0.0

In version 1.0 I've added a check that prevents the same task from showing up within any sequence. This is to help reduce typo errors, as well as prevent the [silent exit bug when the same task occurred twice in a parallel sequence](https://github.com/OverZealous/run-sequence/issues/13). The sequence will now fail immediately during the validation stage.

If this breaking change affects you, you'll need to take one of several actions:

1. Remove duplicate tasks if they are a mistake.
2. Filter unneeded duplicate tasks before passing them to `run-sequence`.
3. Rewrite your tasks or wrap your tasks within functions that can be called multiple times if for some reason you rely on this functionality.
4. Continue using `run-sequence` version 0.3.7 if it was working for you.

## Older

I'm not going to go through the old history at this point.
53 changes: 34 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@

Runs a sequence of gulp tasks in the specified order. This function is designed to solve the situation where you have defined run-order, but choose not to or cannot use dependencies.

> ### Please Note
> ### Is your company hiring Node developers?
>
> This is intended to be a temporary solution until the release of [gulp 4.0](https://github.com/gulpjs/gulp/tree/4.0) which has support for defining task dependencies in [series](https://github.com/gulpjs/gulp/blob/4.0/docs/API.md#gulpseriestasks) or in [parallel](https://github.com/gulpjs/gulp/blob/4.0/docs/API.md#gulpparalleltasks).
>
> Be aware that this solution is a hack, and may stop working with a future update to gulp.
Each argument to `run-sequence` is run in order. This works by listening to the `task_stop` and `task_err` events, and keeping track of which tasks have been completed. You can still run some of the tasks in parallel, by providing an array of task names for one or more of the arguments.

If the final argument is a function, it will be used as a callback after all the functions are either finished or an error has occurred.
> If you are hiring developers, you can support this project and future open source work by checking out our company, [Qualified.io](https://www.qualified.io/?utm_source=run-sequence).
>
> Qualified is a service for online skills-assessment that can help you easily vet developers across a wide range of real-world programming skills.
>
> Please help support this project, and [sign up for a free trial](https://www.qualified.io/?utm_source=run-sequence).
## Possible Breaking Change in version 1.0.0

In version 1.0 I've added a check that prevents the same task from showing up within any sequence. This is to help reduce typo errors, as well as prevent the [silent exit bug when the same task occurred twice in a parallel sequence](https://github.com/OverZealous/run-sequence/issues/13). The sequence will now fail immediately during the validation stage.

If this breaking change affects you, you'll need to take one of several actions:
Each argument to `run-sequence` is run in order. This works by listening to the `task_stop` and `task_err` events, and keeping track of which tasks have been completed. You can still run some of the tasks in parallel, by providing an array of task names for one or more of the arguments.

1. Remove duplicate tasks if they are a mistake.
2. Filter unneeded duplicate tasks before passing them to `run-sequence`.
3. Rewrite your tasks or wrap your tasks within functions that can be called multiple times if for some reason you rely on this functionality.
4. Continue using `run-sequence` version 0.3.7 if it was working for you.
If the final argument is a function, it will be used as a callback after all the functions are either finished or an error has occurred.

[I welcome feedback](https://github.com/OverZealous/run-sequence/issues) if this change is a problem for your setup!
> **Please Note**
>
> This was intended to be a temporary solution until the release of gulp 4.0 which should have support for defining task dependencies similarly.
>
> Given that Gulp 4 appears to never be fully released, take that for what you will. Be aware that this solution is a hack, and may stop working with a future update to gulp.
## Usage

Expand Down Expand Up @@ -99,6 +96,27 @@ var gulp = require('gulp'), // might be a different instance than the toplevel o
runSequence('subtask1', 'subtask2');
```

## Options

There are a few global options you can configure on the `runSequence` function.

Please note these are **global to the module**, and once set will affect every use of `runSequence`.

Usage:

```js
var runSequence = require('run-sequence');
runSequence.options.ignoreUndefinedTasks = true;
gulp.task('task', function(cb) {
runSequence('foo', null, 'bar'); // no longer errors on `null`
})
```

- `showErrorStackTrace`: When set to `false`, this suppresses the full stack trace from errors captured during a sequence.
- `ignoreUndefinedTasks`: When set to `true`, this enables you to pass falsey values in which will be stripped from the task set before validation and sequencing.



## LICENSE

[MIT License](http://en.wikipedia.org/wiki/MIT_License)
Expand All @@ -109,6 +127,3 @@ var gulp = require('gulp'), // might be a different instance than the toplevel o

[travis-url]: http://travis-ci.org/OverZealous/run-sequence
[travis-image]: https://secure.travis-ci.org/OverZealous/run-sequence.png?branch=master

[gratipay-url]: https://www.gratipay.com/OverZealous/
[gratipay-image]: https://img.shields.io/gratipay/OverZealous.svg
121 changes: 73 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
var colors = require('chalk');
var gutil = require('gulp-util');

function options() { return module.exports.options }

function verifyTaskSets(gulp, taskSets, skipArrays) {
if(taskSets.length === 0) {
throw new Error('No tasks were provided to run-sequence');
}

var foundTasks = {};
taskSets.forEach(function(t) {
var isTask = typeof t === "string",
isArray = !skipArrays && Array.isArray(t);
var isTask = typeof t === "string";
var isArray = !skipArrays && Array.isArray(t);
if(!isTask && !isArray) {
throw new Error("Task "+t+" is not a valid task string.");
throw new Error("Task " + t + " is not a valid task string.");
}
if(isTask && !gulp.hasTask(t)) {
throw new Error("Task "+t+" is not configured as a task on gulp. If this is a submodule, you may need to use require('run-sequence').use(gulp).");
throw new Error("Task " + t + " is not configured as a task on gulp. If this is a submodule, you may need to use require('run-sequence').use(gulp).");
}
if(skipArrays && isTask) {
if(foundTasks[t]) {
throw new Error("Task "+t+" is listed more than once. This is probably a typo.");
throw new Error("Task " + t + " is listed more than once. This is probably a typo.");
}
foundTasks[t] = true;
}
Expand All @@ -34,6 +37,10 @@ function verifyTaskSets(gulp, taskSets, skipArrays) {
});
}

function filterArray(arr) {
return arr.filter(function(t) { return !!t; });
}

function runSequence(gulp) {
// load gulp directly when no external was passed
if(gulp === undefined) {
Expand All @@ -42,52 +49,65 @@ function runSequence(gulp) {

// Slice and dice the input to prevent modification of parallel arrays.
var taskSets = Array.prototype.slice.call(arguments, 1).map(function(task) {
return Array.isArray(task) ? task.slice() : task;
}),
callBack = typeof taskSets[taskSets.length-1] === 'function' ? taskSets.pop() : false,
currentTaskSet,

finish = function(e) {
gulp.removeListener('task_stop', onTaskEnd);
gulp.removeListener('task_err', onError);

var error;
if (e && e.err) {
error = new gutil.PluginError('run-sequence(' + e.task + ')', e.err, {showStack: true});
}

if(callBack) {
callBack(error);
} else if(error) {
gutil.log(colors.red(error.toString()));
}
},

onError = function(err) {
finish(err);
},
onTaskEnd = function(event) {
var idx = currentTaskSet.indexOf(event.task);
if(idx > -1) {
currentTaskSet.splice(idx,1);
}
if(currentTaskSet.length === 0) {
runNextSet();
}
},
return Array.isArray(task) ? task.slice() : task;
});
var callBack = typeof taskSets[taskSets.length - 1] === 'function' ? taskSets.pop() : false;
var currentTaskSet;

runNextSet = function() {
if(taskSets.length) {
var command = taskSets.shift();
if(!Array.isArray(command)) {
command = [command];
if(options().ignoreUndefinedTasks) {
// ignore missing tasks
taskSets = filterArray(taskSets)
.map(function(t) {
if(Array.isArray(t)) {
return filterArray(t);
} else {
return t;
}
currentTaskSet = command;
gulp.start.apply(gulp, command);
} else {
finish();
});
}

function finish(e) {
gulp.removeListener('task_stop', onTaskEnd);
gulp.removeListener('task_err', onError);

var error;
if(e && e.err) {
error = new gutil.PluginError('run-sequence(' + e.task + ')', e.err, { showStack: options().showErrorStackTrace });
}

if(callBack) {
callBack(error);
} else if(error) {
gutil.log(colors.red(error.toString()));
}
}

function onError(err) {
finish(err);
}

function onTaskEnd(event) {
var idx = currentTaskSet.indexOf(event.task);
if(idx > -1) {
currentTaskSet.splice(idx, 1);
}
if(currentTaskSet.length === 0) {
runNextSet();
}
}

function runNextSet() {
if(taskSets.length) {
var command = taskSets.shift();
if(!Array.isArray(command)) {
command = [command];
}
};
currentTaskSet = command;
gulp.start.apply(gulp, command);
} else {
finish();
}
}

verifyTaskSets(gulp, taskSets);

Expand All @@ -101,3 +121,8 @@ module.exports = runSequence.bind(null, undefined);
module.exports.use = function(gulp) {
return runSequence.bind(null, gulp);
};

module.exports.options = {
showErrorStackTrace: true,
ignoreUndefinedTasks: false,
};
39 changes: 30 additions & 9 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
/* global describe, it, beforeEach */
'use strict';

var runSequence = require('../'),
gulp = require('gulp'),
should = require('should'),
simpleTask = require('./simpleTask'),
submodule = require('./submodule');
var runSequence = require('../');
var gulp = require('gulp');
var should = require('should');
var simpleTask = require('./simpleTask');
var submodule = require('./submodule');
require('mocha');

function extend(copy, obj) {
copy = copy || {};
Object.keys(obj).forEach(function(k) {
copy[k] = obj[k];
});
return copy;
}

describe('runSequence', function() {
var task1 = simpleTask(),
task2 = simpleTask(),
task3 = simpleTask(),
task4 = simpleTask();
var task1 = simpleTask();
var task2 = simpleTask();
var task3 = simpleTask();
var task4 = simpleTask();

gulp.task('task1', task1);
gulp.task('task2', task2);
Expand Down Expand Up @@ -254,4 +262,17 @@ describe('runSequence', function() {
})
});

describe('Options', function() {
var defaultOptions = extend({}, runSequence.options);
afterEach(function() {
extend(runSequence.options, defaultOptions);
});
it('should ignore empty errors when configured', function() {
runSequence.options.ignoreUndefinedTasks = true;
runSequence('task1', null, undefined, 'task2');
task1.counter.should.eql(1);
task2.counter.should.eql(2);
});
});

});
Loading

0 comments on commit f2b1635

Please sign in to comment.