Skip to content

luckydrq/promise-exec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-exec

NPM version build status Test coverage

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);
  });

Lisence

MIT

About

A simple wrap for child_process.exec which returns promise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published