spawn-sync.js
2.36 KB
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
'use strict';
var path = require('path');
var fs = require('fs');
var tmpdir = require('os').tmpdir || require('os-shim').tmpdir;
var cp = require('child_process');
var sleep;
var JSON = require('./json-buffer');
try {
sleep = require('try-thread-sleep');
} catch (ex) {
console.warn('Native thread-sleep not available.');
console.warn('This will result in much slower performance, but it will still work.');
console.warn('You should re-install spawn-sync or upgrade to the lastest version of node if possible.');
console.warn('Check ' + path.resolve(__dirname, '../error.log') + ' for more details');
sleep = function () {};
}
var temp = path.normalize(path.join(tmpdir(), 'spawn-sync'));
function randomFileName(name) {
function randomHash(count) {
if (count === 1)
return parseInt(16*Math.random(), 10).toString(16);
else {
var hash = '';
for (var i=0; i<count; i++)
hash += randomHash(1);
return hash;
}
}
return temp + '_' + name + '_' + String(process.pid) + randomHash(20);
}
function unlink(filename) {
try {
fs.unlinkSync(filename);
} catch (ex) {
if (ex.code !== 'ENOENT') throw ex;
}
}
function tryUnlink(filename) {
// doesn't matter too much if we fail to delete temp files
try {
fs.unlinkSync(filename);
} catch(e) {}
}
function invoke(cmd) {
// location to keep flag for busy waiting fallback
var finished = randomFileName("finished");
unlink(finished);
if (process.platform === 'win32') {
cmd = cmd + '& echo "finished" > ' + finished;
} else {
cmd = cmd + '; echo "finished" > ' + finished;
}
cp.exec(cmd);
while (!fs.existsSync(finished)) {
// busy wait
sleep(200);
}
tryUnlink(finished);
return 0;
}
module.exports = spawnSyncFallback;
function spawnSyncFallback(cmd, commandArgs, options) {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// node.js script to run the command
var worker = path.normalize(__dirname + '/worker.js');
// location to store arguments
var input = randomFileName('input');
var output = randomFileName('output');
unlink(output);
fs.writeFileSync(input, JSON.stringify(args));
invoke('"' + process.execPath + '" "' + worker + '" "' + input + '" "' + output + '"');
var res = JSON.parse(fs.readFileSync(output, 'utf8'));
tryUnlink(input);tryUnlink(output);
return res;
}