Skip to content

Commit

Permalink
fix: actually implement pipe sequences (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 26, 2024
1 parent ed770cd commit a7e0b96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,11 @@ Deno.test("command .lines()", async () => {
});

Deno.test("piping in command", async () => {
await withTempDir(async (tempDir) => {
const result = await $`echo 1 | cat - > output.txt`.cwd(tempDir).text();
assertEquals(result, "");
assertEquals(tempDir.join("output.txt").readTextSync(), "1\n");
});
{
const result = await $`echo 1 | cat -`.text();
assertEquals(result, "1");
Expand Down
26 changes: 15 additions & 11 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,21 +995,12 @@ async function executePipeSequence(sequence: PipeSequence, context: Context): Pr
let lastOutput = context.stdin;
let nextInner: PipelineInner | undefined = sequence;
while (nextInner != null) {
let innerCommand: Command;
switch (nextInner.kind) {
case "pipeSequence":
switch (nextInner.op) {
case "stdout": {
const buffer = new PipeSequencePipe();
const newContext = context.withInner({
stdout: new ShellPipeWriter("piped", buffer),
stdin: lastOutput,
});
const commandPromise = executeCommand(nextInner.current, newContext);
waitTasks.push(commandPromise);
commandPromise.finally(() => {
buffer.close();
});
lastOutput = buffer;
innerCommand = nextInner.current;
break;
}
case "stdoutstderr": {
Expand All @@ -1025,9 +1016,22 @@ async function executePipeSequence(sequence: PipeSequence, context: Context): Pr
nextInner = nextInner.next;
break;
case "command":
innerCommand = nextInner;
nextInner = undefined;
break;
}

const buffer = new PipeSequencePipe();
const newContext = context.withInner({
stdout: new ShellPipeWriter("piped", buffer),
stdin: lastOutput,
});
const commandPromise = executeCommand(innerCommand, newContext);
waitTasks.push(commandPromise);
commandPromise.finally(() => {
buffer.close();
});
lastOutput = buffer;
}
waitTasks.push(
pipeCommandPipeReaderToWriterSync(lastOutput, context.stdout, context.signal)
Expand Down

0 comments on commit a7e0b96

Please sign in to comment.