Package Exports
- rxjs-shell
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 (rxjs-shell) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
rxjs-shell
rxjs operators for execute shell command with ease.
Features
- Wrap nodejs asynchronous process creation methods to rxjs Observable.
- Kill child process when unsubscribed.
- Use subject to communicate with child process.
Functions
exec(command[, options]) → Observable<{stdout: string | Buffer, stderr: string | Buffer}>
options
interface is same with nodejsexec
method
import {exec} from 'rxjs-shell';
exec('echo Hello World')
.subscribe(output => {
console.log(output.stdout.toString('utf8')); // Hello World\n
});
execFile(file[, args][, options]) → Observable<{stdout: string | Buffer, stderr: string | Buffer}>
options
interface is same with nodejsexecFile
method
import {existSync} from 'fs';
import {execFile} from 'rxjs-shell';
execFile('./touchFile.sh')
.subscribe(() => {
console.log(existSync('touched.txt')); // true
});
spawn(command[, args][, options]) → Observable<{type: 'stdout' | 'stderr', chunk: Buffer}>
spawn
emitsstdout
,stderr
's buffer from command execution.options
interface is same with nodejsspawn
method
import {spawn} from 'rxjs-shell';
spawn('git clone http://github.com/johnny-mh/rxjs-shell-operators')
.pipe(tap(chunk => process.stdout.write(String(chunk.chunk))))
.subscribe();
fork<T = any>(modulePath[, args][, options]) → Observable<T>
- same with
spawn
but have ownoptions
interface that extend nodejs'sfork
options to communicate with child process.
import {fork} from 'rxjs-shell';
import {Subject} from 'rxjs';
const send = new Subject<string>();
fork('echo.js', undefined, {send}).subscribe(msgFromChildProc => console.log(msgFromChildProc));
send.next('message to child process');
Operators
trim(encoding = 'utf8')
- trim child process output
import {exec, trim} from 'rxjs-shell';
exec('echo Hello').subscribe(output => console.log(output.stdout.toString())); // Hello\n
exec('echo Hello').pipe(trim()).subscribe(output => console.log(output.stdout.toString())); // Hello
Utility Methods
spawnEnd(spawnObservable: Observable) → Subject<{stdout: Buffer, stderr: Buffer}>
spawn
emit each buffer from child process. if you want to connect other operator to this stream. usespawnEnd
method.
import {spawn, spawnEnd} from 'rxjs-shell';
spawn('webpack', ['-p'])
.pipe(outputChunk => { /* each child process's output buffer */ })
.subscribe();
spawnEnd(spawn('webpack', ['-p']))
.pipe(webpackOutput => { /* do something */ })
.subscribe();
Error Handling
import {spawn, ShellError} from 'rxjs-shell';
spawn('git clone http://github.com/johnny-mh/rxjs-shell-operators')
.pipe(tap(chunk => process.stdout.write(String(chunk.chunk))))
.subscribe({
catch(err) {
if (!(err instanceof ShellError)) {
throw err;
}
console.log(err.stdout);
console.log(err.stderr);
console.log(err.message);
console.log(err.code);
console.log(err.originError);
}
});