Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tsertkov/exec-sh
node helper to execute shell commands
https://github.com/tsertkov/exec-sh
Last synced: 9 days ago
JSON representation
node helper to execute shell commands
- Host: GitHub
- URL: https://github.com/tsertkov/exec-sh
- Owner: tsertkov
- License: mit
- Created: 2014-06-14T01:33:04.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-02-13T08:12:15.000Z (9 months ago)
- Last Synced: 2024-10-17T12:25:39.253Z (22 days ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/exec-sh
- Size: 704 KB
- Stars: 63
- Watchers: 3
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# exec-sh
[![NPM](https://nodei.co/npm/exec-sh.png)](https://nodei.co/npm/exec-sh/)
[![NPM Downloads](https://img.shields.io/npm/dm/exec-sh.svg)](https://www.npmjs.com/package/exec-sh)> Execute shell command forwarding all stdio streams.
## Features
exec-sh is a wrapper for [`child_process.spawn`](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) with some improvements:
- Cross platform command execution:
- Windows: `cmd /C COMMAND`
- others: `sh -c COMMAND`
- Forwards all stdio streams to current terminal (by default):
- `execSh("bash")`
- `execsh("echo -n Say: && read i && echo Said:$i")`
- stdout and stderr are passed to callback when available
- `execSh("pwd", console.log)`## Showcase
```javascript
// JavaScriptexecSh("echo hello exec-sh && bash", { cwd: "/home" }, function(err){
if (err) {
console.log("Exit code: ", err.code);
}
});
``````sh
# Terminal output: interactive bash sessionhello exec-sh
bash-3.2$ pwd
/home
bash-3.2$ exit 99
exit
Exit code: 99
```## Usage
```javascript
const execSh = require("../");// run interactive bash shell
execSh("echo lorem && bash", { cwd: "/home" }, (err) => {
if (err) {
console.log("Exit code: ", err.code);
return;
}// collect streams output
const child = execSh(["bash -c id", "echo lorem >&2"], true,
(err, stdout, stderr) => {
console.log("error: ", err);
console.log("stdout: ", stdout);
console.log("stderr: ", stderr);
});
});
```## Promise Interface
```javascript
const execShPromise = require("exec-sh").promise;// run interactive bash shell
const run = async () => {
let out;try {
out = await execShPromise('pwd', true);
} catch (e) {
console.log('Error: ', e);
console.log('Stderr: ', e.stderr);
console.log('Stdout: ', e.stdout);return e;
}console.log('out: ', out.stdout, out.stderr);
}run();
```## Public API
### `execSh(command, [options], [callback])`
Execute shell command forwarding all stdio.
**Parameters:**
- `command {String|Array}` - The command to run, or array of commands
- `[options] {Object|TRUE}` - Options object passed directly to [`child_process.spawn`](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options), when `TRUE` then `{ stdio: null }` used
- `[callback] {Function}` - `callback(err, stdout, stderr)`
- `err {Error|NULL}` - Error object. Has `code` property containing last command exit code when available
- `stdout {String|NULL}` - aggregated stdout or `NULL` if not available
- `stderr {String|NULL}` - aggregated stderr or `NULL` if not available**Return Values:**
Returns [ChildProcess](http://nodejs.org/api/child_process.html#child_process_class_childprocess) object.
## Scripts
- `npm test` - run tests
- `npm run jsdoc` - build jsdoc
- `npm run dev` - run tests continuously## License
The MIT License (MIT)