https://github.com/nfour/unishell
https://github.com/nfour/unishell
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nfour/unishell
- Owner: nfour
- Created: 2019-03-20T07:02:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T18:13:37.000Z (over 3 years ago)
- Last Synced: 2025-08-31T12:11:11.834Z (11 months ago)
- Language: TypeScript
- Size: 747 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Unishell
- [Why](#why)
- [Examples](#examples)
## Why
Got tired of having to pick and choose which library to interact with the CLI. This attempts to unify it.
**Goals**:
- Be as terse and as close to a bash script as reasonable
- Normalize local and remote CLI interactions
**Usage examples**:
- Regular CLI interaction with a terser interface
- Deployment scripts for remote SSH targets
## Examples
```ts
import { Unishell } from 'unishell';
const unishell = new Unishell({ /* global options */ });
/** Produces a `ssh2` powered client */
const ssh = unishell.ssh({
host: 'localhost',
user: 'foo',
identity: '',
});
/** Produces a local `execa` powered client */
const local = unishell.local({
cwd: __dirname,
});
await ssh.connect();
const r1 = await ssh.exec('echo foo');
r1; // "foo"
const r2 = await ssh.exec('echo foo').details();
r2.output; // "foo"
await ssh.exec('echo foo').pipe({ stdout: process.stdout });
const r3 = ssh.exec('echo foo').pipe(process);
r3.on;
const r4 = await r3;
r4;
const r5 = await ssh.exec('notARealCommand');
r5; // "command not found: notARealCommand"
try {
await ssh.exec('notARealCommand').reject().details();
} catch (r6) {
r6;
// { stdout: "", stderr: "command not found: notARealCommand", exitCode: 127, ... }
}
const r7 = await ssh.shell(async (exec) => {
exec('').reject;
exec('').details;
exec('').pipe;
exec('').then;
});
r7;
const r8 = await ssh.exec('echo foo').reject(); // doesnt actually reject, as `echo` wont error
r8; // "foo"
await ssh.shell(async (exec) => {
await exec('echo foo');
})
.pipe(process);
const sessionStream = ssh.shell(async (exec) => {
await exec('echo foo');
await exec('exit');
});
/** Piping the parent node CLI input into the ssh session's stdin */
process.stdin.pipe(sessionStream.stdin);
/** Piping the ssh session's stdout and stderr into the parent node process */
sessionStream.pipe(process);
await sessionStream; // Shell session is over
await local.connect();
/** Run commands locally */
await local.shell(async (exec) => {
const r10 = await exec('ls -lsah').details();
r10.cmd;
});
/** Access the `ssh2` Client to do advanced operations */
const ssh2 = ssh.client();
ssh2.forwardIn;
```