diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:04:18 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:04:18 +0200 |
commit | a14ca3e268e95a7eab59fb205b41da7331d57631 (patch) | |
tree | 9c84b2cbd561345335fca3e26af961b2ea23d8ec /lib/utils/command.js | |
parent | 9c071dade573aa6990878006f83c89b6065a1395 (diff) | |
download | gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.zip gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.tar.gz gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.tar.bz2 |
Switch to lerna
Diffstat (limited to 'lib/utils/command.js')
-rw-r--r-- | lib/utils/command.js | 118 |
1 files changed, 0 insertions, 118 deletions
diff --git a/lib/utils/command.js b/lib/utils/command.js deleted file mode 100644 index 90a556e..0000000 --- a/lib/utils/command.js +++ /dev/null @@ -1,118 +0,0 @@ -var is = require('is'); -var childProcess = require('child_process'); -var spawn = require('spawn-cmd').spawn; -var Promise = require('./promise'); - -/** - Execute a command - - @param {String} command - @param {Object} options - @return {Promise} -*/ -function exec(command, options) { - var d = Promise.defer(); - - var child = childProcess.exec(command, options, function(err, stdout, stderr) { - if (!err) { - return d.resolve(); - } - - err.message = stdout.toString('utf8') + stderr.toString('utf8'); - d.reject(err); - }); - - child.stdout.on('data', function (data) { - d.notify(data); - }); - - child.stderr.on('data', function (data) { - d.notify(data); - }); - - return d.promise; -} - -/** - Spawn an executable - - @param {String} command - @param {Array} args - @param {Object} options - @return {Promise} -*/ -function spawnCmd(command, args, options) { - var d = Promise.defer(); - var child = spawn(command, args, options); - - child.on('error', function(error) { - return d.reject(error); - }); - - child.stdout.on('data', function (data) { - d.notify(data); - }); - - child.stderr.on('data', function (data) { - d.notify(data); - }); - - 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 - - @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 = []; - - for (var key in options) { - var value = options[key]; - - if (value === null || value === undefined || value === false) { - continue; - } - - if (is.bool(value)) { - result.push(key); - } else { - result.push(key + '=' + escapeShellArg(value)); - } - } - - return result.join(' '); -} - -module.exports = { - exec: exec, - spawn: spawnCmd, - optionsToShellArgs: optionsToShellArgs -}; |