node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »

promise-exec

promise-exec

A simple wrap for child_process.exec which returns promise

Installation

$ npm install promise-exec

API

exec

It's a promise version of child_process.exec.

var exec = require('promise-exec');
 
exec('ls -al')
  .then(function(result) {
    console.log(result);
  })
  .catch(function(err) {
    console.error(err);
  });

exports.wrap

It's a sugar for subsequent thenable calls that makes code simpler and more elegant.

var exec = require('promise-exec').wrap;
 
Promise.resolve()
  .then(exec('git add .'))
  .then(exec('git commit -m "fix xxx"'))
  .then(exec('git push origin master'))
  .catch(function(err) {
    console.error(err);
  });

exports.execFile

Promise version of executing a shell script file, just like sh test.sh.

test.sh:

touch index.txt
echo "hello" > index.txt
# comments will be ingored
cat index.txt

index.js:

var fs = require('fs');
var path = require('path');
var execFile = require('promise-exec').execFile;
var file = path.join(__dirname, './test.sh');
 
execFile(file)
  .then(function() {
    var txt = path.join(__dirname, 'index.txt');
    console.log(fs.existsSync(txt));  // true 
 
    var content = fs.readFileSync(txt, { encoding: 'utf8' });
    console.log(content);  // hello\n 
  })
  .catch(function(err) {
    console.error(err);
  });