-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (94 loc) · 2.57 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
var util = require('util'),
child_process = require('child_process');
// Get access to the ChildProcess constructor
var ChildProcess = child_process.spawn('echo', ['> /dev/null']).constructor;
var Child = function() {
ChildProcess.call(this);
};
util.inherits(Child, ChildProcess);
Child.prototype.spawn = function(file, args, options) {
// Create the new process
var c = new Child();
// Pass error event through
this.on('error', function(err) {
c.emit('error', err);
});
// Spawn on close of previous
this.on('close', function() {
_spawn(c, file, args, options);
});
return c;
};
Child.prototype.exec = function() {
};
Child.prototype.execFile = function() {
};
function _spawn(child/*, file, args, options*/) {
var opts = normalizeSpawnArguments.apply(null, Array.prototype.slice.call(arguments, 1));
var file = opts.file;
var args = opts.args;
var options = opts.options;
var envPairs = opts.envPairs;
// Spawn the process
ChildProcess.prototype.spawn.call(child, {
file: file,
args: args,
cwd: options ? options.cwd : null,
windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments),
detached: !!(options && options.detached),
envPairs: envPairs,
stdio: options ? options.stdio : null,
uid: options ? options.uid : null,
gid: options ? options.gid : null
});
return child;
}
function normalizeSpawnArguments(file/*, args, options*/) {
var args, options;
if (Array.isArray(arguments[1])) {
args = arguments[1].slice(0);
options = arguments[2];
} else if (arguments[1] && !Array.isArray(arguments[1])) {
throw new TypeError('Incorrect value of args option');
} else {
args = [];
options = arguments[1];
}
args.unshift(file);
var env = (options && options.env ? options.env : null) || process.env;
var envPairs = [];
for (var key in env) {
envPairs.push(key + '=' + env[key]);
}
if (options && options.customFds && !options.stdio) {
options.stdio = options.customFds.map(function(fd) {
return fd === -1 ? 'pipe' : fd;
});
}
return {
file: file,
args: args,
options: options,
envPairs: envPairs
};
}
function decorate(child) {
child.spawn = Child.prototype.spawn.bind(child);
child.exec = Child.prototype.exec.bind(child);
child.execFile = Child.prototype.execFile.bind(child);
}
module.exports = {
_ChildProcess: ChildProcess,
ChildProcess: Child,
spawn: function(file, args, options) {
var c = child_process.spawn.apply(null, arguments);
decorate(c);
return c;
},
exec: function() {},
execFile: function() {
var c = child_process.execFile.apply(null, arguments);
decorate(c);
return c;
}
};