Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created March 14, 2014 21:26
Show Gist options
  • Save Raynos/9557330 to your computer and use it in GitHub Desktop.
Save Raynos/9557330 to your computer and use it in GitHub Desktop.

exec-callback

Like child_process.exec but uses a node style callback

Example

var exec = require('exec-callback');

exec('git log', function (err, out) {
    // could be a 'git is not installed' error
    // could be a CWD is not a git project.
    if (err) {
        // err.message is either STDERR from process
        // or a lower level error
        return console.error(err);
    }


    console.log('out', out)
})

Motivation

Running child_process.exec has a strange (err, stdout, stderr) interface. This module normalizes it to be based on (err, value). The stderr has been co-erced into the err value.

Most of the time anything written to stderr is an actual error and you can bubble them upwards in the simple cases.

This module makes that usage simpler

var childProcess = require('child_process');
function exec(cmd, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = opts || {};
opts.maxBuffer = opts.maxBuffer || 2000 * 1024;
childProcess.exec(cmd, opts, function (err, stdout, stderr) {
if (err) {
return cb(err);
}
if (stderr && !opts.silent) {
return cb(new Error(stderr));
}
cb(null, stdout);
});
}
module.exports = exec;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment