Package Exports
- pshell
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pshell) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
pshell
Provides a simple Promise-based interface for running shell commands.
Basic usage
var shell = require("pshell");
shell("node --version").then(res => {
console.log("exit code:", res.code);
});
/****** console output *******
/bin/sh -c node --version
v4.5.0
exit code: 0
******************************/
Why?
Writing a shell script in JavaScript is:
- Easier than bash script to most developers.
- More portable (don't let Windows users behind).
- More powerful in managing child processes in asynchronous way.
More details
Don't echo the commands.
var shell = require("pshell");
shell("node --version", {echoCommand: false}).then(res => {
console.log("exit code:", res.code);
});
/****** console output *******
v4.5.0
exit code: 0
******************************/
Capture the output as a string.
var shell = require("pshell");
shell("node --version", {echoCommand: false, captureOutput: true}).then(res => {
console.log("stdout:", JSON.stringify(res.stdout));
console.log("exit code:", res.code);
});
/****** console output *******
stdout: "v4.5.0\n"
exit code: 0
******************************/
Configure the global options so you don't need to specify the same options everytime.
var shell = require("pshell");
shell.options.echoCommand = false;
shell.options.captureOutput = true;
Promise.all([
shell("node --version"),
shell("npm --version")
]).then(res => {
process.stdout.write("Node version: " + res[0].stdout);
process.stdout.write("NPM version: " + res[1].stdout);
});
/****** console output *******
Node version: v4.5.0
NPM version: 3.10.6
******************************/
You can get a pre-configured version of
shell
function by calling thecontext
API. This is a good way to avoid modifying the global options. Otherpshell
users in the same process won't be affected.
var shell = require("pshell").context({echoCommand: false, captureOutput: true});
Promise.all([
shell("node --version"),
shell("npm --version")
]).then(res => {
process.stdout.write("Node version: " + res[0].stdout);
process.stdout.write("NPM version: " + res[1].stdout);
});
/****** console output *******
Node version: v4.5.0
NPM version: 3.10.6
******************************/
A non-zero exit code rejects the promise by default.
var shell = require("pshell").context({echoCommand: false});
shell("node -e 'process.exit(1)'").then(res => {
console.log("exit code:", res.code);
}).catch(err => {
console.error("error occurred:", err);
});
/****** console output *******
error occurred: [Error: Process 26546 exited with code 1]
******************************/
Set
ignoreError
totrue
if you want to get the promise resolved instead.
var shell = require("pshell").context({echoCommand: false, ignoreError: true});
shell("node -e 'process.exit(1)'").then(res => {
console.log("exit code:", res.code);
}).catch(err => {
console.error("error occurred:", err);
});
/****** console output *******
exit code: 1
******************************/
API
shell(command[, options])
Executes the command
string using the system's default shell ('/bin/sh'
on Unix, 'cmd.exe'
on Windows). An optional options
object can be given to override the base options for this session.
Returns a promise that will be resolved with the result object when the command execution completes.
shell.exec(command[, options])
Same as shell()
but returns an object {childProcess, promise}
instead of a promise. You can access underlying ChildProcess
object for advanced manipulation of a child process.
Options
Options
Result object
Result
License
MIT