Skip to content

Instantly share code, notes, and snippets.

@hisui
Created December 6, 2017 03:49
Show Gist options
  • Save hisui/db1b29e1d5667b38d2c94a59fa7703d3 to your computer and use it in GitHub Desktop.
Save hisui/db1b29e1d5667b38d2c94a59fa7703d3 to your computer and use it in GitHub Desktop.
import * as Rx from "rxjs";
import * as CP from "child_process";
function spawn(cmd: string, args: string[] = []): Rx.Observable<ProcessOutput> {
return Rx.Observable.create(observer => {
let process = CP.spawn(cmd, args);
process.addListener("error", err => {
observer.error(err);
});
process.addListener("exit", code => {
observer.next({ kind: "exit", code });
observer.complete();
});
process.stderr.addListener("data", data => observer.next({ kind: "stderr", data }));
process.stdout.addListener("data", data => observer.next({ kind: "stdout", data }));
return new Rx.Subscriber(() => {
process.kill();
});
});
}
type ProcessOutput
= {
kind: "exit",
code: number
}
| {
kind: "stderr",
data: any
}
| {
kind: "stdout",
data: any
}
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment