tea-spawn
Take a sip of everything you need from your OS mug
What
The easiest way to spawn a process and read its outcome.
How
The classic npm install tea-spawn
will do the magic, an optional -g
is suggested to reuse anytime the library.
var TeaSpawn = ; // python as runtime interpretervar python = 'python';python ; // python filevar python = 'python' 'test/test.py';python;
In latter example the second argument is used as partial application.
This means that every python.send([arg0, arg1, argN])
will be concatenated to the list of arguments producing this call python test/test.py arg0 arg1 argN
with proper shell arguments escape provided by spawn
module.
API
-
TeaSpawn(executable:string[, argument:string|arguments:Array[, env:Object]])
The second argument can be either a string or an array with 0, one, or more entries and will be used as partial arguments per each call to the send method. -
TeaSpawn#send(null|content:string|arguments:Array[, callback:Function]):object
every call to this method will create a new spawned process with the same initial configuration. If the first argument isnull
the process will be executed as it is. If the first argument isstring
it will be written in the spawned process stdin. If the first argument is aArray
it will be used as extra arguments and no content will be written to the stdin. The second optional argument, if present, will receiveerror
andoutput
parameters. Error is either a buffered error or the process exit number. If0
means everything was fine.output
is the content produced by that call ornull
if none. The method returns the instance object itself. -
TeaSpawn#kill(void):object
It kills every process previously started with.send()
and returns the instance object. -
TeaSpawn#log(error:Number|StringBuffer, output:null|StringBuffer):object
This is simply an utility method handy for runtime operations suchp.send(someArg, p.log)
. It will log the error or the output as they come, that's pretty much it.
More Examples
// curl (grab a page output)var curl = 'curl' '-L' '-s' // arguments used per each .send() call; // call send with extra argumentscurl;curl; // detailed list of filesvar ls = 'ls' '-la';ls; { var cols = row; this;}
Why
Sometimes there's no module to do what you need to ... spawn could become really handy as utility/tool for any task you might need.
It is also possible to make interoperation easy between programming languages such Python or Java, having stateless programs that can do something per each call without needing a full duplex channel, just the standard output.
As summary, if performance is not such big concern keep it simple and go for it!