-
Notifications
You must be signed in to change notification settings - Fork 61
/
index.js
160 lines (136 loc) · 4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*jshint node:true */
//2018-01-02 sc mod: gulp-util deprecation
//2018-01-02 sc mod: require plugin-error to replace gutil.PluginError
//2018-01-02 sc mod: require fancy-log to replace gutil.log
"use strict";
var colors = require('chalk');
//sc mod start
//var gutil = require('gulp-util');
var pulginError = require('plugin-error');
var log = require('fancy-log');
//sc mod stop
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";
var 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 filterArray(arr) {
return arr.filter(function(t) { return !!t; });
}
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;
});
var callBack = typeof taskSets[taskSets.length - 1] === 'function' ? taskSets.pop() : false;
var currentTaskSet;
var finished;
if(options().ignoreUndefinedTasks) {
// ignore missing tasks
taskSets = filterArray(taskSets)
.map(function(t) {
if(Array.isArray(t)) {
return filterArray(t);
} else {
return t;
}
});
}
function finish(e) {
if(finished) return;
finished = true;
gulp.removeListener('task_stop', onTaskEnd);
gulp.removeListener('task_err', onError);
gulp.removeListener('err', onGulpError);
var error;
if(e && e.err) {
//sc mod start
//error = new gutil.PluginError('run-sequence(' + e.task + ')', e.err, { showStack: options().showErrorStackTrace });
error = new pulginError('run-sequence(' + e.task + ')', e.err, { showStack: options().showErrorStackTrace });
//sc mod stop
}
if(callBack) {
callBack(error);
} else if(error) {
//sc mod start
//gutil.log(colors.red(error.toString()));
log(colors.red(error.toString()));
//sc mod stop
}
}
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 onGulpError(e) {
// In the case that you call gulp.stop after a successful run,
// we will not receive a task_err or task_stop event. This callback
// will finish the run sequence execution in case of an 'orchestration aborted'
// even coming from gulp's global error handler. That event is fired in when
// gulp.stop is called.
if(e.message === 'orchestration aborted') {
finish(e);
}
}
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);
gulp.on('task_stop', onTaskEnd);
gulp.on('task_err', onError);
gulp.on('err', onGulpError);
runNextSet();
}
module.exports = runSequence.bind(null, undefined);
module.exports.use = function(gulp) {
return runSequence.bind(null, gulp);
};
module.exports.options = {
showErrorStackTrace: true,
ignoreUndefinedTasks: false,
};