/
index.js
134 lines (132 loc) · 3.3 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
var async = require('async');
var spawn = require('child_process').spawn;
function trySpawn(cmd, cb) {
var child;
try {
child = spawn(cmd.spawnOptions[0], cmd.spawnOptions[1], cmd.spawnOptions[2]);
}
catch (e) {
cb(e, null);
return;
}
cb(null, child);
}
var empty = Object.create(null);
exports.createQueue = function (options) {
options = options || {};
var concurrency = options.concurrency == null
? 1
: Number(options.concurrency);
if (isNaN(concurrency)) {
concurrency = 1;
}
var interposers = options.interposers || empty;
var spawner = options.spawner || trySpawn;
var queue = async.queue(onCmd, concurrency);
return createAPI(interposers, spawner, queue);
}
function createAPI(interposers, spawner, queue) {
var API = {
wrap: function (wrap_interposers) {
var new_interposers = Object.create(null);
Object.keys(wrap_interposers).forEach(function (name) {
if (!interposers[name]) {
new_interposers[name] = wrap_interposers[name];
}
else {
new_interposers[name] = function (val, next) {
wrap_interposers[name](val, function (err, altered) {
if (err) {
next(err);
}
else {
interposers[name](altered, next);
}
});
}
}
});
return createAPI(new_interposers, spawner, queue);
},
push: function (cmd, cb) {
queue.push({
spawner: spawner,
interposers: interposers,
cmd: cmd
}, cb);
}
}
return API;
}
function onCmd(task, callback) {
var spawner = task.spawner;
var interposers = task.interposers;
var cmd = task.cmd;
var exited = false;
function next(err) {
if (exited) {
return;
}
exited = true;
callback(err);
}
function next_if_err_or_(fn) {
return function (err, val) {
if (err) next(err);
else fn(val);
}
}
function assessCmd() {
if (!cmd.spawnOptions) {
next(new Error('missing spawnOptions'));
}
else if (typeof interposers.parameters === 'function') {
interposers.parameters(cmd, next_if_err_or_(spawnCmd));
}
else {
spawnCmd(cmd);
}
}
function spawnCmd(cmd) {
spawner(cmd, next_if_err_or_(handleProc));
}
function handleProc(child) {
if (!child) {
next(new Error('spawn did not create a child'));
}
else {
child.on('error', next);
if (typeof interposers.child === 'function') {
interposers.child(child, next_if_err_or_(waitOnProc));
}
else {
waitOnProc(child);
}
}
}
function waitOnProc(child) {
if (!child) {
next(new Error('specified interposer did not provide a child'));
}
if (child.killed || child.exitCode != null || child.signalCode != null) {
cleanup();
}
else {
child.removeListener('error', next);
child.on('exit', cleanup);
child.on('error', cleanup);
}
function cleanup() {
if (child.exitCode) {
next(new Error('process exited with code: ' + child.exitCode));
}
else if (child.signalCode) {
next(new Error('process exited with signal: ' + child.signalCode));
}
else {
next(null);
}
}
}
assessCmd(cmd);
}