Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An easy way to specify stdin #720

Closed
neongreen opened this issue Feb 3, 2024 · 3 comments · Fixed by #736
Closed

An easy way to specify stdin #720

neongreen opened this issue Feb 3, 2024 · 3 comments · Fixed by #736

Comments

@neongreen
Copy link

neongreen commented Feb 3, 2024

Right now, there are (at least?) two ways to specify stdin:

  1. Via pipes:

    $`echo ${input} | command`

    This works, but at least on GitHub runners it can suffer from random SIGPIPE (exit code 141) failures that are not reproducible locally. No idea why.

  2. Via .stdin:

    const p = $`command`
    p.stdin.write(input)
    p.stdin.end()

    This requires giving a name to the command itself, and is a bit cumbersome to use.

I propose pushing the latter pattern into a function or a method, eg. something like

$`command`.input(input)

or perhaps

function withInput(cmd: zx.ProcessPromise, input: string): zx.ProcessPromise {
  cmd.stdin.write(input)
  cmd.stdin.end()
  return cmd
}
@antonmedv
Copy link
Collaborator

Nice idea

@XDuskAshes
Copy link

Not quite familiar with zx or js, but I have played a bit with both as well as other languages. I think this is a good idea, and could cut down on cumbersome-ness.

@antongolub
Copy link
Collaborator

antongolub commented Mar 4, 2024

$command.input(input)

We're planning to introduce smth like $(opts)`cmd args` in the netx major release, and the input option is definitely adopted.

const input = '{"name": "foo"}'
const name = await $({input})`jq -r .name` // foo

const stdin = fs.createReadStream(path.join(fixtures, 'foo.json'))
const data = await $({stdin})`jq -r .data` // foo

const p = $`echo "5\\n3\\n1\\n4\\n2"`
const sorted = $({input: p})`sort` // 1\n2\n3\n4\n5

antongolub added a commit to antongolub/zx that referenced this issue Mar 17, 2024
antonmedv pushed a commit that referenced this issue Mar 17, 2024
* chore(deps): update yaml to 2.4.1

* feat: provide `input` option

closes #720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants