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

Add stdio options to spawn #1

Merged
merged 15 commits into from
Sep 7, 2024
Merged

Conversation

benjamin-wilkins
Copy link
Contributor

This adds the option to pass a stdio argument to spawn(). This allows you to interact more with the child process. For example, in Deno, you can pass the host process's stdio with:

import { spawn } from "@cross/utils";

spawn(["<COMMAND>"], {}, undefined, {
  stdin: Deno.stdin.readable,
  stdout: Deno.stdout.writable,
  stderr: Deno.stderr.writable,
});

This is fully backwards-compatible - if this argument is omitted it should run identically to the current version of @cross/utils.

I also removed the Deno and Bun implementations as both have decent Node compatibility covering spawn(), with just one quite small polyfill required in Bun.

Separately, I've also added an @cross/utils/stdio module to get the current process's stdio streams more easily across platforms (again, the Node API is mostly compatible with all 3 runtimes, though a small polyfill is again required for Bun) . With this, the above can be rewritten like this:

import { spawn, stdin, stdout, stderr } from "@cross/utils";

spawn(["<COMMAND>"], {}, undefined, {
  stdin: stdin(),
  stdout: stdout(),
  stderr: stderr(),
});

@benjamin-wilkins
Copy link
Contributor Author

Added inherit option from Node, which allows direct passing to the parent stdio, without passing through JS (which appears to garble arrow keys etc.). This works better if you're calling a separate CLI in a subprocess.

@benjamin-wilkins
Copy link
Contributor Author

Example from above:

spawn(["<COMMAND>"], {}, undefined, {
  stdin: "inherit",
  stdout: "inherit",
  stderr: "inherit",
});

@Hexagon
Copy link
Member

Hexagon commented Sep 7, 2024

Interesting, great work! Will review this asap.

@benjamin-wilkins
Copy link
Contributor Author

That should be fixed now

@Hexagon
Copy link
Member

Hexagon commented Sep 7, 2024

Run deno task check to run all CI tests locally 👍

@benjamin-wilkins
Copy link
Contributor Author

Passing locally now

@Hexagon Hexagon merged commit 8335842 into cross-org:main Sep 7, 2024
4 checks passed
@Hexagon
Copy link
Member

Hexagon commented Sep 7, 2024

Released through 0.15.0 🎉

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 this pull request may close these issues.

2 participants