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

Use inherited stdin instead of piped #192

Merged
merged 1 commit into from Aug 16, 2021
Merged

Use inherited stdin instead of piped #192

merged 1 commit into from Aug 16, 2021

Conversation

antonmedv
Copy link
Collaborator

Fixes #191

This PR refactors ProcessPromise.

Now call $ does not create the child process. then() creates child process.

child.on('exit', code => {
child.on('close', () => {
if (piped) process.stdin.unpipe(child.stdin)
let output = new ProcessOutput({
code, stdout, stderr, combined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combined, message props are not declaredProcessOutput iface. The rest seems fine to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, message goes to the parent class. And combined is used inside toString() func.

@antonmedv antonmedv merged commit e9d9b4e into main Aug 16, 2021
@antonmedv antonmedv deleted the inherit-stdin branch August 16, 2021 06:29
Copy link

@huaziHear huaziHear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm a little confused about the logic.

let child = spawn(prefix + cmd, {
cwd,
shell: typeof shell === 'string' ? shell : true,
stdio: [promise._inheritStdin ? 'inherit' : 'pipe', 'pipe', 'pipe'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use 'inherit' instead of 'pipe' for this parameter.

in cases like this, errors are reported:

#!/usr/bin/env zx
const sleep = () => new Promise((resolve) => setTimeout(resolve, 1000));

let p = $`read var; echo "$var";`

await sleep();

p.stdin.write('Hello, stdin!\n')  //  p.stdin is null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdin of the child process is not necessarily a synchronous call, so is there a flaw in the design?

Copy link
Collaborator Author

@antonmedv antonmedv Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done to allow interactively work with subprocesses if nothing is "touched".

This way to can make it work:

#!/usr/bin/env zx

// sleep() already defined in zx

let p = $`read var; echo "$var";`
let stdin = p.stdin // force piped mode.

await sleep(1000);

stdin.write('Hello, stdin!\n')

See http://go/gh/google/zx/blob/main/examples/interactive.mjs

Well, probably we can create separate API for setting inherited stdin. Like

$`...`.inheritStdin()

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.

Error: the input device is not a TTY
3 participants