forked from nospaceships/node-raw-socket
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocessExecSync.js
43 lines (43 loc) · 1.05 KB
/
processExecSync.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
module.exports = {
execSync: function(command, options) {
var child, error, fs, timeout, tmpdir;
console.log("run execSync");
fs = require('fs');
options = options || {};
timeout = Date.now() + options.timeout;
tmpdir = '/tmp/processExecSync.' + Date.now() + Math.random();
fs.mkdirSync(tmpdir);
command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir +
'/stderr); echo $? > ' + tmpdir + '/status';
child = require('child_process').exec(command, options, function () {
return;
});
while (true) {
try {
fs.readFileSync(tmpdir + '/status');
break;
} catch (ignore) {
}
if (Date.now() > timeout) {
error = child;
break;
}
}
['stdout', 'stderr', 'status'].forEach(function (file) {
child[file] = fs.readFileSync(tmpdir + '/' + file, options.encoding);
fs.unlinkSync(tmpdir + '/' + file);
});
child.status = Number(child.status);
if (child.status !== 0) {
error = child;
}
try {
fs.rmdirSync(tmpdir);
} catch (ignore) {
}
if (error) {
throw error;
}
return child.stdout;
}
}