an easy, or better, an easier way to spawn processes and get the result
Take a sip of everything you need from your OS mug
The easiest way to spawn a process and read its outcome.
npm install tea-spawn will do the magic, an optional
-g is suggested to reuse anytime the library.
var TeaSpawn = require'tea-spawn.js';// python as runtime interpretervar python = 'python';pythonsend'print("Hello World")'console.logoutput; // Hello Worldsend'print(123)'console.logoutput; // 123;// python filevar python = 'python' 'test/test.py';pythonsend1 2 3console.logoutput; // 1\n2\n3;
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
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]):objectevery call to this method will create a new spawned process with the same initial configuration. If the first argument is
nullthe process will be executed as it is. If the first argument is
stringit will be written in the spawned process stdin. If the first argument is a
Arrayit will be used as extra arguments and no content will be written to the stdin. The second optional argument, if present, will receive
outputparameters. Error is either a buffered error or the process exit number. If
0means everything was fine.
outputis the content produced by that call or
nullif none. The method returns the instance object itself.
TeaSpawn#kill(void):objectIt kills every process previously started with
.send()and returns the instance object.
TeaSpawn#log(error:Number|StringBuffer, output:null|StringBuffer):objectThis is simply an utility method handy for runtime operations such
p.send(someArg, p.log). It will log the error or the output as they come, that's pretty much it.
// curl (grab a page output)var curl ='curl''-L' '-s' // arguments used per each .send() call;// call send with extra argumentscurlsend'' curl.log;curlsend'' curl.log;// detailed list of filesvar ls = 'ls' '-la';lssend'./'var result = ;outsplit/\r\n|\r|\n/slice1 -1forEachrowToObject result;console.logresult;;var cols = rowsplit/ +/;thispushpermissions: colsshiftlinks: colsshiftowner: colsshiftgroup: colsshiftsize: colsshiftname: colspopmdate: colsjoin' ';
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!