-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
64 lines (57 loc) · 1.54 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
var cSpawn = require('child_process').spawn;
var os = require('os').type();
exports = module.exports = spawn;
function spawn(command, args, options) {
if (os === 'Windows_NT') {
command = command.replace(/\//g, '\\');
if (command === 'rm') {
command = 'rmdir';
if (args[0] === '-rf' || args[0] == '-fr') {
args[0] = '/q';
args.unshift('/s');
}
if (args[0] === '-f') {
args[0] = '/q';
}
if (args[0] === '-r') {
args[0] = '/s';
}
}
args = args || [];
options = options || {};
var match, matchA;
if (matchA = /((?:[A-Z_]+\=[^ \=]+ )+)?([^\r\n]+)/.exec(command)) {
try {
var file = require('fs').readFileSync(matchA[2], 'utf8');
if (match = /\#\!\/usr\/bin\/env ([^\r\n]+)/.exec(file)) {
args.unshift(matchA[2]);
command = (matchA[1] || '') + match[1];
}
} catch (ex) { }
}
if (match = /((?:[A-Z_]+\=[^ \=]+ )+)([^\r\n]+)/.exec(command)) {
command = match[2];
options.env = options.env || shallowClone(process.env);
var env = match[1].split(' ');
env.forEach(function (v) {
v = v.split('=');
if (v.length === 2) {
options.env[v[0]] = v[1];
}
});
}
args.unshift(command);
args.unshift('/c');
args.unshift('/d');
command = 'cmd';
}
return cSpawn(command, args, options);
}
function shallowClone(obj) {
var out = {};
Object.keys(obj)
.forEach(function (key) {
out[key] = obj[key];
});
return out;
}