Fix escaping of ebook-convert options

This commit is contained in:
Samy Pessé
2016-05-02 15:09:09 +02:00
parent fa6fcffbe1
commit de47db2b66
+27 -8
View File
@@ -1,3 +1,4 @@
var is = require('is');
var childProcess = require('child_process');
var spawn = require('spawn-cmd').spawn;
var Promise = require('./promise');
@@ -54,12 +55,29 @@ function spawnCmd(command, args, options) {
return d.promise;
}
// Transform an option object to a command line string
function escapeShellArg(s) {
s = s.replace(/"/g, '\\"');
return '"' + s + '"';
/**
Transform an option object to a command line string
@param {String|number} value
@param {String}
*/
function escapeShellArg(value) {
if (is.number(value)) {
return value;
}
value = String(value);
value = value.replace(/"/g, '\\"');
return '"' + value + '"';
}
/**
Transform a map of options into a command line arguments string
@param {Object} options
@return {String}
*/
function optionsToShellArgs(options) {
var result = [];
@@ -69,11 +87,12 @@ function optionsToShellArgs(options) {
if (value === null || value === undefined || value === false) {
continue;
}
if (value === true) {
result.push(key);
}
result.push(key + '=' + escapeShellArg(value));
if (is.bool(value)) {
result.push(key);
} else {
result.push(key + '=' + escapeShellArg(value));
}
}
return result.join(' ');