forked from boazy/any-shell-escape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell-escape.js
49 lines (43 loc) · 1020 Bytes
/
shell-escape.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
var util = require('util'),
winCmd = /^win/.test(process.platform),
escapePath;
function escapePathSh(path) {
if (!/^[A-Za-z0-9_\/-]+$/.test(path))
return ("'" + path.replace(/'/g, "'\"'\"'") + "'").replace(/''/g, '');
else
return path;
}
function escapePathWin(path) {
if (!/^[A-Za-z0-9_\/-]+$/.test(path))
return '"' + path.replace(/"/g, '""') + '"';
else
return path;
}
if (winCmd) {
escapePath = escapePathWin;
} else {
escapePath = escapePathSh;
}
module.exports = function(stringOrArray) {
var ret = [];
if (typeof(stringOrArray) == 'string') {
return escapePath(stringOrArray);
} else {
stringOrArray.forEach(function(member) {
ret.push(escapePath(member));
});
return ret.join(' ');
}
};
if (winCmd) {
// Cannot escape messages on windows
module.exports.msg = function(x) {
if (typeof(x) == 'string') {
return x;
} else {
return x.join(' ');
}
};
} else {
module.exports.msg = module.exports;
}