Deferred based tool to run exec commands
Deferred based tool to run exec commands. Lets you use exec, execFile and spawn in a sane way.
Install the module with:
npm install deferred-exec or add it to your project's
You can also clone this repo and
npm install folderOfClonedRepo to get a branch or development copy.
All calls return a promise, which means it's easy to do stuff when they complete or fail:
var dexec = require 'deferred-exec' ;dexec 'echo "yay"'doneconsole.log stdout ; // logs "yay"failconsole.log "it didn't work :( got code:" errorcode ;;
Since they are deferreds, you can pass them around in your code:
var dexec = require 'deferred-exec' ;var command = dexec 'echo "gotcha"' ;doSomethingWithCommand command ;// meanwhile, in some other part of your applicationcommanddoneconsole.log 'just ran' command 'and got' stdout ;;
var dexec = require 'deferred-exec' ;// require and mixin lodash with _.deferredvar _ = require 'lodash' ;_mixin require 'underscore.deferred' ;var commandA = dexec 'ls /etc' ;var commandB = dexec 'echo "hi"' ;// when both commands succeed_when commandA commandBdoneconsole.log 'commandA output:' a0 ;console.log 'commandB output:' b0 ;;
Or run a file using
var dexec = require 'deferred-exec' ;dexecfile './someFile.sh'doneconsole.log 'ran' fileName 'got' stdout ;;
Or take advantage of spawning a new progress and getting its output during execution:
var dexec = require 'deferred-exec' ;dexecspawn 'cat' '/var/log/syslog'progress/* this function will get called with every piece ofdata from the returne result */done/* all done! total value's available of course */;
deferred-exec details see child_process.exec for available options
deferred-exec.file details see child_process.execFile
for available options
deferred-exec.spawn details see child_progress.spawn for available options
All methods support an options object.
deferred-exec adds two possible options to this object:
This defaults to
true, which trims the last bit of trailing while space (a new line) from the output.
false if you don't want your final output trimmed.
The default is
utf8, so if you want one of the other types supported you can specify it here.
Assuming `var dexec = require( 'deferred-exec' ):
All methods return a promise. Check out the Deferred Documentation.
.spawn utilizes the
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Copyright (c) 2012 Dan Heberden
Licensed under the MIT license.