1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

Add Process.output (#1235)

This commit is contained in:
DanSnow 2018-12-01 02:44:05 +08:00 committed by Ryan Dahl
parent e749b37b7c
commit d43a4be0d2
2 changed files with 30 additions and 2 deletions

View file

@ -2,9 +2,11 @@
import * as dispatch from "./dispatch";
import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/msg_generated";
import { assert, unreachable } from "./util";
import { close, File } from "./files";
import { File, close } from "./files";
import { ReadCloser, WriteCloser } from "./io";
import { readAll } from "./buffer";
import { assert, unreachable } from "./util";
/** How to handle subsubprocess stdio.
*
@ -59,6 +61,21 @@ export class Process {
return await runStatus(this.rid);
}
/** Buffer the stdout and return it as Uint8Array after EOF.
* You must have set stdout to "piped" in when creating the process.
* This calls close() on stdout after its done.
*/
async output(): Promise<Uint8Array> {
if (!this.stdout) {
throw new Error("Process.output: stdout is undefined");
}
try {
return await readAll(this.stdout);
} finally {
this.stdout.close();
}
}
close(): void {
close(this.rid);
}

View file

@ -176,3 +176,14 @@ testPerm({ run: true }, async function runStderrPiped() {
assertEqual(status.signal, undefined);
p.close();
});
testPerm({ run: true }, async function runOutput() {
const p = run({
args: ["python", "-c", "import sys; sys.stdout.write('hello')"],
stdout: "piped"
});
const output = await p.output();
const s = new TextDecoder().decode(output);
assertEqual(s, "hello");
p.close();
});