Skip to content

Commit

Permalink
feat: ability to provide a CommandBuilder to .stdin(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 26, 2024
1 parent da97144 commit b6a1666
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,26 @@ Deno.test("piping to stdin", async () => {
const output = await $`cat`.stdin(tempFile).text();
assertEquals(output, fileText.trim());
});

// command via stdin
{
const child = $`echo 1 && echo 2`;
const result = await $`deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable);'`
.stdin(child)
.text();
assertEquals(result, "1\n2");
}

// command that exists via stdin
{
const child = $`echo 1 && echo 2 && exit 1`;
const result = await $`deno eval 'await Deno.stdin.readable.pipeTo(Deno.stdout.writable);'`
.stdin(child)
.stderr("piped")
.noThrow();
assertEquals(result.code, 1);
assertEquals(result.stderr, "stdin pipe broken. Error: Exited with code: 1\n");
}
});

Deno.test("piping to a writable and the command fails", async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ export class CommandBuilder implements PromiseLike<CommandResult> {
const body = await reader;
return body.readable;
});
} else if (reader instanceof CommandBuilder) {
state.stdin = new Deferred(() => {
return reader.stdout("piped").spawn().stdout();
});
} else {
state.stdin = new Box(reader);
}
Expand Down
2 changes: 2 additions & 0 deletions src/pipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type FsFileWrapper, PathRef } from "./path.ts";
import { logger } from "./console/logger.ts";
import { Buffer, writeAllSync } from "./deps.ts";
import type { RequestBuilder } from "./request.ts";
import type { CommandBuilder } from "./command.ts";

const encoder = new TextEncoder();

Expand Down Expand Up @@ -30,6 +31,7 @@ export type ShellPipeReaderKind =
| Reader
| ReadableStream<Uint8Array>
| Uint8Array
| CommandBuilder
| FsFileWrapper
| PathRef
| RequestBuilder;
Expand Down

0 comments on commit b6a1666

Please sign in to comment.