https://github.com/davidar/nodejs-sh
Call any program as if it were a Node.js function
https://github.com/davidar/nodejs-sh
Last synced: 5 months ago
JSON representation
Call any program as if it were a Node.js function
- Host: GitHub
- URL: https://github.com/davidar/nodejs-sh
- Owner: davidar
- License: mit
- Created: 2018-04-19T01:43:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-11T00:32:54.000Z (over 4 years ago)
- Last Synced: 2025-10-22T21:59:17.916Z (8 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/nodejs-sh
- Size: 114 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nodejs-sh
This is a Node.js port of the [sh](https://amoffat.github.io/sh/) Python library, that allows you to call any program as if it were a function:
```js
const {ifconfig} = require('nodejs-sh')
let output = await ifconfig('eth0').toString()
console.log(output)
```
Output:
```
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: ffff::ffff:ffff:ffff:fff/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0 GB) TX bytes:0 (0 GB)
```
## Installation
```sh
npm install nodejs-sh
```
## Quick Reference
### Passing Arguments
```js
const {ls} = require('nodejs-sh')
ls('-l', '/tmp', '--color=never').pipe(process.stdout)
```
### Non-zero Exit Codes
```js
const {ls} = require('nodejs-sh')
try {
await ls('/doesnt/exist')
} catch (exitCode) {
if (exitCode === 2) console.log("directory doesn't exist")
}
```
### Redirection
```js
const fs = require('fs')
const {ls} = require('nodejs-sh')
let file = fs.createWriteStream('file.txt')
ls('test/').pipe(file).on('finish', () => {
let output = fs.readFileSync('file.txt', 'utf8')
})
```
### Piping
```js
const {ls, wc} = require('nodejs-sh')
let output = await ls('-1').pipe(wc('-l')).toString()
```
### Background Processes
```js
const {find} = require('nodejs-sh')
let p = find('-name', 'index.js')
// ... do other things ...
await p
```
### TypeScript
```ts
import * as sh from "nodejs-sh";
const output = await sh.ls("-1").pipe(sh.wc("-l")).toString();
```