forked from mysticatea/npm-run-all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tasks.js
177 lines (156 loc) · 5.01 KB
/
run-tasks.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @module run-tasks-in-parallel
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const MemoryStream = require("memorystream")
const NpmRunAllError = require("./npm-run-all-error")
const runTask = require("./run-task")
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Remove the given value from the array.
* @template T
* @param {T[]} array - The array to remove.
* @param {T} x - The item to be removed.
* @returns {void}
*/
function remove(array, x) {
const index = array.indexOf(x)
if (index !== -1) {
array.splice(index, 1)
}
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Run npm-scripts of given names in parallel.
*
* If a npm-script exited with a non-zero code, this aborts other all npm-scripts.
*
* @param {string} tasks - A list of npm-script name to run in parallel.
* @param {object} options - An option object.
* @returns {Promise} A promise object which becomes fullfilled when all npm-scripts are completed.
* @private
*/
module.exports = function runTasks(tasks, options) {
return new Promise((resolve, reject) => {
if (tasks.length === 0) {
resolve([])
return
}
const results = tasks.map(task => ({ name: task, code: undefined }))
const queue = tasks.map((task, index) => ({ name: task, index }))
const promises = []
let error = null
let aborted = false
/**
* Done.
* @returns {void}
*/
function done() {
if (error == null) {
resolve(results)
}
else {
reject(error)
}
}
/**
* Aborts all tasks.
* @returns {void}
*/
function abort() {
if (aborted) {
return
}
aborted = true
if (promises.length === 0) {
done()
}
else {
for (const p of promises) {
p.abort()
}
Promise.all(promises).then(done, reject)
}
}
/**
* Runs a next task.
* @returns {void}
*/
function next() {
if (aborted) {
return
}
if (queue.length === 0) {
if (promises.length === 0) {
done()
}
return
}
const originalOutputStream = options.stdout
const optionsClone = Object.assign({}, options)
const writer = new MemoryStream(null, {
readable: false,
})
if (options.aggregateOutput) {
optionsClone.stdout = writer
}
const task = queue.shift()
const promise = runTask(task.name, optionsClone)
promises.push(promise)
promise.then(
(result) => {
remove(promises, promise)
if (aborted) {
return
}
if (options.aggregateOutput) {
originalOutputStream.write(writer.toString())
}
// Save the result.
results[task.index].code = result.code
// Aborts all tasks if it's an error.
if (result.code) {
error = new NpmRunAllError(result, results)
if (!options.continueOnError) {
abort()
return
}
}
// Aborts all tasks if options.race is true.
if (options.race && !result.code) {
abort()
return
}
// Call the next task.
next()
},
(thisError) => {
remove(promises, promise)
if (!options.continueOnError || options.race) {
error = thisError
abort()
return
}
next()
}
)
}
const max = options.maxParallel
const end = (typeof max === "number" && max > 0)
? Math.min(tasks.length, max)
: tasks.length
for (let i = 0; i < end; ++i) {
next()
}
})
}