-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
56 lines (50 loc) · 1.31 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
'use strict';
const spawn = require('child_process').spawn;
const gulpMultiProcess = function(tasks, cb, cpusRespective) {
var code = 0;
const createWorker = function(onExit, taskName) {
var args = process.execArgv.concat([process.argv[1], taskName]);
var worker;
process.argv.forEach(function (val) {
if(val[0] === '-' && val !== '--gulpfile') {
args.push(val);
}
});
worker = spawn(process.execPath, args , { stdio: 'inherit' });
worker.on('exit', onExit);
};
if (!cpusRespective) {
var completed = 0;
let each = createWorker.bind(this, function (workerCode) {
if(workerCode !== 0) {
code = workerCode;
}
completed++;
if(completed === tasks.length) {
cb(code);
}
});
tasks.forEach(each);
} else {
const cpusNumber = require('os').cpus().length;
const queue = require('async/queue');
let q = queue(function (taskName, callback) {
createWorker(
function (workerCode) {
if(workerCode !== 0) {
code = workerCode;
}
callback();
},
taskName
);
}, cpusNumber);
tasks.forEach(function (task) {
q.push(task)
});
q.drain(function () {
cb(code);
});
}
};
module.exports = gulpMultiProcess;