/
raxx.ts
33 lines (25 loc) · 897 Bytes
/
raxx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {createReadStream} from "fs"
import {AxxConnector, AxxOptions} from "./axx"
export default function raxx(path: string, next?: AxxConnector, {record = false}: AxxOptions = {}): AxxConnector {
const stream = createReadStream(path)
if (next) stream.pipe(next.stdin)
let data = ""
if (record) stream.on("data", d => data += d)
const firstResult = new Promise<string>((resolve, reject) => {
stream.on("close", () => resolve(data))
stream.on("error", (error: Error) => reject(error))
})
const result = next
? Promise.all([firstResult, next]).then(([first, next]) => next)
: firstResult
return {
stdin: null,
stdout: stream,
then: result.then.bind(result),
catch: result.catch.bind(result)
}
}
export {raxx}
export function mraxx(cmd: string, output?: AxxConnector, options: AxxOptions = {}): AxxConnector {
return raxx(cmd, output, {...options, record: true})
}