shell-args
Version:
Parsing and quoting for shell command lines that supports both bash and windows styles of quoting.
57 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function bashShellQuote(args) {
return args.map((a) => {
function contains(str) {
for (let i = 0; i < str.length; i++) {
if (a.includes(str.charAt(i))) {
return true;
}
}
return false;
}
if (!contains("\"' \\()")) {
return a;
}
if (!contains("'")) {
return `'${a}'`;
}
return "\"" + a.replace(/\\/g, "\\\\").replace(/[()"\n]/g, "\\$&") + "\"";
}).join(" ");
}
exports.bashShellQuote = bashShellQuote;
function winShellQuote(args) {
return args.map((a) => {
function contains(str) {
for (let i = 0; i < str.length; i++) {
if (a.includes(str.charAt(i))) {
return true;
}
}
return false;
}
if (!contains("\"' \\")) {
return a;
}
let escaped = a.replace(/\\*"|\\+/g, (match) => {
if (!match.endsWith("\\")) {
let count = match.length - 1;
return "\\".repeat(count * 2 + 1) + "\"";
}
return match;
});
if (contains(" ")) {
return "\"" + escaped + "\"";
}
return escaped;
}).join(" ");
}
exports.winShellQuote = winShellQuote;
function shellQuote(args) {
if (process.platform === "win32") {
return winShellQuote(args);
}
return bashShellQuote(args);
}
exports.shellQuote = shellQuote;
//# sourceMappingURL=quote.js.map