From f2b16350968ccabf3711dde1f14f007f884369bb Mon Sep 17 00:00:00 2001 From: Phil DeJarnett Date: Mon, 24 Jul 2017 18:35:55 -0400 Subject: [PATCH] Added options, code cleanup Closes #92 --- .eslintrc | 31 ++++++++++++ CHANGELOG.md | 27 ++++++++-- README.md | 53 +++++++++++++------- index.js | 121 +++++++++++++++++++++++++++------------------ test/main.js | 39 +++++++++++---- test/simpleTask.js | 58 +++++++++++----------- test/submodule.js | 8 +-- 7 files changed, 225 insertions(+), 112 deletions(-) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..ccde9a4 --- /dev/null +++ b/.eslintrc @@ -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": {} +} diff --git a/CHANGELOG.md b/CHANGELOG.md index 50937c0..d397940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,15 @@ 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 @@ -12,12 +20,25 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. 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. \ No newline at end of file diff --git a/README.md b/README.md index 3a994e5..a01876c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) @@ -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 diff --git a/index.js b/index.js index fc70545..56bb483 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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) { @@ -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); @@ -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, +}; diff --git a/test/main.js b/test/main.js index 520c827..bb7323a 100644 --- a/test/main.js +++ b/test/main.js @@ -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); @@ -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); + }); + }); + }); \ No newline at end of file diff --git a/test/simpleTask.js b/test/simpleTask.js index 37af8ec..efc452f 100644 --- a/test/simpleTask.js +++ b/test/simpleTask.js @@ -1,35 +1,35 @@ -var runCounter = 0, - getCounter = function() { - runCounter++; - return runCounter; - }, - simpleTask = function() { - var task = function(cb) { - if(task.shouldPause) { - task.cb = cb; - } else { - task.counter = getCounter(); - cb(); - } - }; +var runCounter = 0; +var getCounter = function() { + runCounter++; + return runCounter; +}; +var simpleTask = function() { + var task = function(cb) { + if(task.shouldPause) { + task.cb = cb; + } else { + task.counter = getCounter(); + cb(); + } + }; + task.shouldPause = false; + task.counter = -1; + //noinspection ReservedWordAsName + task.continue = function(err) { + if(task.cb) { + task.counter = getCounter(); + var cb = task.cb; + delete task.cb; + cb(err); + } + }; + task.reset = function() { task.shouldPause = false; task.counter = -1; - //noinspection ReservedWordAsName - task.continue = function(err) { - if(task.cb) { - task.counter = getCounter(); - var cb = task.cb; - delete task.cb; - cb(err); - } - }; - task.reset = function() { - task.shouldPause = false; - task.counter = -1; - delete task.cb; - }; - return task; + delete task.cb; }; + return task; +}; simpleTask.resetRunCounter = function() { runCounter = 0; diff --git a/test/submodule.js b/test/submodule.js index eb14b0a..da053d8 100644 --- a/test/submodule.js +++ b/test/submodule.js @@ -1,7 +1,7 @@ -var _gulp = require('gulp'), - gulp = new _gulp.Gulp(), - simpleTask = require('./simpleTask'), - runSequence = require('../').use(gulp); +var _gulp = require('gulp'); +var gulp = new _gulp.Gulp(); +var simpleTask = require('./simpleTask'); +var runSequence = require('../').use(gulp); var subtask = simpleTask();