dsherret / dax

Cross-platform shell tools for Deno and Node.js inspired by zx.
MIT License
970 stars 33 forks source link

Can the existing API support the <() expression in the shell #140

Closed char8x closed 5 months ago

char8x commented 1 year ago

Is there any way to make the following code execute successfully

Example 1

import $ from "https://deno.land/x/dax/mod.ts";

const a = "12345"
const b = "23456"

await $`diff <(echo ${a}) <(echo ${b})`

Output

error: Uncaught "Unexpected character.\n  <(echo 12345) <(echo 23456)\n  ~"

Example 2

import $ from "https://deno.land/x/dax/mod.ts";

const a = "12345"
const b = "23456"

const tempFileA = await Deno.makeTempFile();
await Deno.writeTextFile(tempFileA, a);

const tempFileB = await Deno.makeTempFile();
await Deno.writeTextFile(tempFileB, b);

await $`diff ${tempFileA} ${tempFileB}`

Output

1c1
< 12345
\ No newline at end of file
---
> 23456
\ No newline at end of file
error: Uncaught Error: Exited with code: 1
          throw new Error(`Exited with code: ${code}`);
                ^
    at CommandChild.pipedStdoutBuffer (https://deno.land/x/dax@0.31.1/src/command.ts:587:17)
    at eventLoopTick (ext:core/01_core.js:181:11)

Env

dax@0.31.1 deno 1.33.2

dsherret commented 5 months ago

So from my basic understanding (just going to write this out to ensure I'm on the right page) this isn't an sh feature and is something found in other shells like bash. It requires creating a pipe containing the string's contents and passing the file descriptor of that pipe as an argument to the diff subprocess. For this to work in dax there would need to be a way in Deno of creating named pipes and then providing those to subprocesses as arguments (it's something that's been discussed in the past and I think we could do it).

Looking at diff, it seems like there isn't a way to just provide it two strings as arguments. This is the best I can do in this scenario for the time being, which is slightly unfortunate:

async function diff(a: string, b: string) {
  async function makeTempFile() {
    const filePath = await Deno.makeTempFile();
    return $.path(filePath);
  }

  const [temp1, temp2] = await Promise.all([makeTempFile(), makeTempFile()]);
  try {
    await Promise.all([temp1.writeText(a), temp2.writeText(b)]);
    await $`diff ${temp1} ${temp2}`;
  } finally {
    await Promise.all([temp1.remove(), temp2.remove()]);
  }
}

const a = "12345"
const b = "23456"
await diff(a, b);
char8x commented 5 months ago

Thank you for sharing your understanding. You are correct that this feature isn't native to sh and is more commonly found in shells like bash. I appreciate your insight into the potential solution for Dax, and I agree that having the capability to create named pipes in Deno and passing them as arguments to subprocesses would be a valuable addition.

makidoll commented 4 months ago

I was wondering if it would be possible to instead set fds ourselves. For example, imagemagick lets you run something like convert fd:3 fd:4 to take data from pipe 3 and 4 to use. Here's an example of it for a different project in node.

https://github.com/makidoll/frog-bot/blob/ebd80eca7514a65607927c294faf102698d67f34/src/image-utils.ts#L234