mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
var _ = require('lodash');
|
|
var childProcess = require('child_process');
|
|
var Promise = require('./promise');
|
|
|
|
// On borwser, command execution is not possible
|
|
var isAvailable = childProcess && childProcess.exec;
|
|
|
|
// Execute a command
|
|
function exec(command, options) {
|
|
if (!isAvailable) {
|
|
return Promise.reject(new Error('Command execution is not possible on this platform'));
|
|
}
|
|
|
|
return Promise.nfcall(childProcess.exec, command, options);
|
|
}
|
|
|
|
// Spawn an executable
|
|
function spawn(command, args, options) {
|
|
if (!isAvailable) {
|
|
return Promise.reject(new Error('Command execution is not possible on this platform'));
|
|
}
|
|
|
|
var d = Promise.defer();
|
|
var child = childProcess.spawn(command, args, options);
|
|
|
|
child.on('error', function(error) {
|
|
return d.reject(error);
|
|
});
|
|
|
|
child.on('close', function(code) {
|
|
if (code === 0) {
|
|
d.resolve();
|
|
} else {
|
|
d.reject(new Error('Error with command "'+command+'"'));
|
|
}
|
|
});
|
|
|
|
return d.promise;
|
|
}
|
|
|
|
// Transform an option object to a command line string
|
|
function escapeShellArg(s) {
|
|
s = s.replace(/"/g, '\\"');
|
|
return '"' + s + '"';
|
|
}
|
|
|
|
function optionsToShellArgs(options) {
|
|
return _.chain(options)
|
|
.map(function(value, key) {
|
|
if (value === null || value === undefined || value === false) return null;
|
|
if (value === true) return key;
|
|
return key + '=' + escapeShellArg(value);
|
|
})
|
|
.compact()
|
|
.value()
|
|
.join(' ');
|
|
}
|
|
|
|
module.exports = {
|
|
isAvailable: isAvailable,
|
|
exec: exec,
|
|
spawn: spawn,
|
|
optionsToShellArgs: optionsToShellArgs
|
|
};
|