Crun.js
Crun is a Node.js module which provides methods to map system commands to JavaScript functions, to allow better reusability of system command calls. Function parameters represent argument strings which are interpolated via #{…} symbols with arbitrary names.
A ChildProcess object is returned when calling commands without providing a callback function. If a callback function is provided, there is no return value and the results are processed through the callback.
Install
Install the module with: npm install crun
Example
var crun = ; // generate commandsvar commands = ls: 'ls -al' cp: 'cp -Rf #{src} #{dst}' tail: 'tail -f #{log}';crun; // run commands child_process.execcruntest; cruntest; // run with no console loggingcruntest; //run commands child_process.spawnvar tail = cruntest;tailstdout; tailstderr; tail;
Generated Functions
A function generated by crun.generate
needs to be passed the same number of parameters as the number of #{…} symbols in the command string. For example, the cp
command in the example accepts two parameters, src and dst. The last two parameters for all generated functions are the options
object and the callback function, which are both optional. If the callback function isn't defined, a function call returns a ChildProcess object as if child_process.spawn()
were called.
The options object allows controlling specific execution behavior, via set properties of the object (see example above):
silent
: false by default, set to true to turn off console logging
The callback function consists of two parameters: the error object and the result object. The result object has the following properties:
command
: the command with the #{} placeholders replaced with passed in valuesstdout
: contents of the standard output stream of the commandstderr
: contents of the standard error stream of the commandstdoutt
: trimmed stdout - stdout without leading or trailing whitespacestderrt
: trimmed stderr - stderr without leading or trailing whitespace