lazy-command.js
v1.0.1
Published
Rust-like Command wrapper over node.js child_process
Readme
lazy-command.js
Rust-like Command wrapper over node.js child_process
It provides simple & unified builder for Node.js child_process
Overview
Defaults
- Inherits parent's environment.
- Inherits parent's current directory.
Execution methods
- spawn - Equal to
child_process.spawnorchild_process.spawnSync. - exec - Equal to
child_process.execorchild_process.execSync. - execFile - Equal to
child_process.execFileorchild_process.execFileSync. - output - Equal to
child_process.spawnSyncbut returns only status, stdout and stderr. - status - Equal to
child_process.execSyncbut returns only status.
Sync vs Async
By default Command executes asynchronously.
In order to configure command for synchronous execution you need to call method sync
For example to perform exec synchronously:
const status = require('lazy-command.js')('git -status').sync().exec()For asynchronous version you need to omit sync.
const status = require('lazy-command.js')('git -status').exec()Async callback
Setting of callback is optional but method callback can be used to provide it.
The signature of callback is the same as for any corresponding child_process functions.
For example:
const cb = (err, stdout, stderr) => {
console.log("Err=%s | stdout=%s | stderr=%s", err, stdout, stderr);
};
const status = require('lazy-command.js')('git -status').callback(cb).exec()Examples
Collect all output of command
const Command = require('lazy-command.js');
const result = Command("git -status").encoding('utf-8').output();
console.log("status=% | stdout=%s | stderr=%s",
result.status, result.stdout, result.stderr);Just get status code
const Command = require('lazy-command.js');
const result = Command("git -status").stdio_ignore().status();;
console.log("Status=%s", result);Let's make lot of configuration
const Command = require('lazy-command.js');
const result = Command("git").stdio_ignore()
.arg("-status")
.current_dir("lib/");
.timeout(1000)
.status();;
console.log("Status=%s", result);