Created
December 6, 2017 03:49
-
-
Save hisui/db1b29e1d5667b38d2c94a59fa7703d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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