forked from OverZealous/run-sequence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (84 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*jshint node:true */
"use strict";
var colors = require('chalk');
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);
if(!isTask && !isArray) {
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).");
}
if(skipArrays && isTask) {
if(foundTasks[t]) {
throw new Error("Task "+t+" is listed more than once. This is probably a typo.");
}
foundTasks[t] = true;
}
if(isArray) {
if(t.length === 0) {
throw new Error("An empty array was provided as a task set");
}
verifyTaskSets(gulp, t, true, foundTasks);
}
});
}
function runSequence(gulp) {
// load gulp directly when no external was passed
if(gulp === undefined) {
gulp = require('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);
if(callBack) {
callBack(e && e.err ? e.err : undefined);
} else if(e && e.err) {
console.log(colors.red('Error running task sequence:'), e.err);
}
},
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();
}
},
runNextSet = function() {
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);
gulp.on('task_stop', onTaskEnd);
gulp.on('task_err', onError);
runNextSet();
}
module.exports = runSequence.bind(null, undefined);
module.exports.use = function(gulp) {
return runSequence.bind(null, gulp);
};