async-exec-cmd
Simple, fast, flexible and cross-platform async executing commands (with node-cross-spawn).
Install
npm i async-exec-cmd --save
npm test
API
For more use-cases see the tests
asyncExecCmd
Async execute command via spawn. All arguments are rebuilt, merged, structured, normalized and after all passed to cross-spawn, which actually is Node's
spawn
.
<cmd>
{String} Command/program to execute. You can pass subcommands, flags and arguments separated with space[args]
{Array} arguments that will be arr-union with the given incmd
. You can giveopts
object here instead ofargs
[opts]
{Object} pass options to spawn and github-short-url-regex. You can givecb
function here instead ofopts
<cb>
{Function} node-style callback function that will handleerr
{Error} error if exists (instanceof Error
), ornull
. It have some extra props:command
{String} thecmd
plusargs
which was tried to executemessage
{String} some useful messagebuffer
{Buffer} representation of the errorstatus
{Number|String}stack
usual ... stack trace
res
{String} representation of response for the executed command/program- notice when
opts.stdio: 'inherit'
, res is empty string''
- notice when
err
, it isundefined
- notice when
code
{Number|String} e.g.0
,1
,-2
,128
,'ENOENT'
, etc.. Process exit status code of the executionbuffer
{Buffer} buffer equivalent of response, e.g.<Buffer 74 75 6e 6e...>
- notice when
err
, it isundefined
- but notice you can find it again in
err.buffer
- notice when
returns
{Stream} child_process.spawn
Example:
var asyncExecCmd = var child =
Possible signatures (will work)
these examples should work without problems
var cmd = { if err console return console} /** * Try all these commands separatly or run the tests * they cover all situations */ //=> res and buffer are undefined //=> err Error object, res and buffer are undefined, //=> err undefined, code 0, res === 'unbuild bluebird@2.9.3' //=> err undefined, code 0, res === 'unbuild bluebird@2.9.3' //=> err undefined, code 0, res === '2.9.0' //=> err undefined, code 0, res === 'bluebird@2.9.3 node_modules/bluebird' //=> err undefined, code 0, res === 'unbuild bluebird@2.9.3' //=> will directly outputs: 2.9.0//=> err undefined, code 0, res === ''
Impossible signatures (will throws/errors)
these examples should not work
//=> first argument cant be function //=> should have `callback` (non empty callback) //=> should have `callback` (non empty callback) //=> should have `callback` (non empty callback) //=> expect `cmd` be string //=> expect `cmd` be string //=> expect `cmd` be string
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.