Skip to content

Commit

Permalink
feat: allow passing writers to stdout and stderr builder methods (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr authored Dec 17, 2023
1 parent e6534ae commit fc2ea78
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
20 changes: 20 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ Deno.test("should capture stdout when inherited and piped", async () => {
assertEquals(output.stdout, "5\n");
});

Deno.test("should not get stdout when set to writer", async () => {
const buffer = new Buffer();
const output = await $`echo 5`.stdout(buffer);
assertEquals(output.code, 0);
assertEquals(new TextDecoder().decode(buffer.bytes()), "5\n");
assertThrows(() => output.stdout, Error, `Stdout was streamed to another source and is no longer available.`);
});

Deno.test("should not get stderr when inherited only (default)", async () => {
const output = await $`deno eval 'console.error("should output");'`;
assertEquals(output.code, 0);
Expand Down Expand Up @@ -79,6 +87,18 @@ Deno.test("should capture stderr when inherited and piped", async () => {
assertEquals(output.stderr, "5\n");
});

Deno.test("should not get stderr when set to writer", async () => {
const buffer = new Buffer();
const output = await $`deno eval 'console.error(5); console.log(1);'`.stderr(buffer);
assertEquals(output.code, 0);
assertEquals(new TextDecoder().decode(buffer.bytes()), "5\n");
assertThrows(
() => output.stderr,
Error,
`Stderr was not piped (was streamed). Call .stderr(\"piped\") or .stderr(\"inheritPiped\") when building the command.`,
);
});

Deno.test("should get combined stdout and stderr when specified", async () => {
const output = await $`echo 1 ; sleep 0.5 ; deno eval -q 'console.error(2);'`.captureCombined();
assertEquals(output.code, 0);
Expand Down
12 changes: 10 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ export class CommandBuilder implements PromiseLike<CommandResult> {
});

function getQuietKind(kind: ShellPipeWriterKind): ShellPipeWriterKind {
if (typeof kind === "object") {
return kind;
}
switch (kind) {
case "inheritPiped":
case "inherit":
Expand Down Expand Up @@ -690,6 +693,9 @@ export function parseAndSpawnCommand(state: CommandBuilderState) {
return [stdoutBuffer, stderrBuffer, undefined] as const;

function getOutputBuffer(innerWriter: Deno.WriterSync, kind: ShellPipeWriterKind) {
if (typeof kind === "object") {
return kind;
}
switch (kind) {
case "inherit":
if (hasProgressBars) {
Expand All @@ -712,7 +718,7 @@ export function parseAndSpawnCommand(state: CommandBuilderState) {
}

function finalizeCommandResultBuffer(
buffer: PipedBuffer | "inherit" | "null" | CapturingBufferWriter | InheritStaticTextBypassWriter,
buffer: PipedBuffer | "inherit" | "null" | CapturingBufferWriter | InheritStaticTextBypassWriter | Deno.WriterSync,
): BufferStdio {
if (buffer instanceof CapturingBufferWriter) {
return buffer.getBuffer();
Expand All @@ -722,13 +728,15 @@ export function parseAndSpawnCommand(state: CommandBuilderState) {
} else if (buffer instanceof PipedBuffer) {
buffer.close();
return buffer.getBuffer() ?? "streamed";
} else if (typeof buffer === "object") {
return "streamed";
} else {
return buffer;
}
}

function finalizeCommandResultBufferForError(
buffer: PipedBuffer | "inherit" | "null" | CapturingBufferWriter | InheritStaticTextBypassWriter,
buffer: PipedBuffer | "inherit" | "null" | CapturingBufferWriter | InheritStaticTextBypassWriter | Deno.WriterSync,
error: Error,
) {
if (buffer instanceof InheritStaticTextBypassWriter) {
Expand Down
2 changes: 1 addition & 1 deletion src/deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
assertRejects,
assertStringIncludes,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
} from "https://deno.land/[email protected]/assert/mod.ts";
export { readerFromStreamReader } from "https://deno.land/[email protected]/streams/reader_from_stream_reader.ts";
export { writableStreamFromWriter } from "https://deno.land/[email protected]/streams/writable_stream_from_writer.ts";
export { serve } from "https://deno.land/[email protected]/http/server.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/pipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ShellPipeReader = "inherit" | "null" | Deno.Reader;
* @value "piped" - Captures the pipe without outputting.
* @value "inheritPiped" - Captures the pipe with outputting.
*/
export type ShellPipeWriterKind = "inherit" | "null" | "piped" | "inheritPiped";
export type ShellPipeWriterKind = "inherit" | "null" | "piped" | "inheritPiped" | Deno.WriterSync;

export class NullPipeWriter implements Deno.WriterSync {
writeSync(p: Uint8Array): number {
Expand Down

0 comments on commit fc2ea78

Please sign in to comment.