ethereum / ethereum-binaries

Fast, easy and secure Ethereum binary management
https://ethereum.github.io/ethereum-binaries/#/
19 stars 10 forks source link

Get stdout logs from a spawned child_process with stdio: 'inherit' #1

Open PhilippLgh opened 4 years ago

PhilippLgh commented 4 years ago

We want to be able to spawn a binary process with inherit but need to get the output to be able to analyse lifecycle events.

Unfortunately this is not possible: https://github.com/nodejs/node/issues/8033

The reason seems to be:

With inherit, the child process and the parent process end up with independent file descriptors pointing to the same resource

PhilippLgh commented 4 years ago

A temporary solution will be:

    const _process = spawn(command, args, {
      stdio: stdio === 'inherit' ? ['inherit', 'pipe', 'pipe'] : [stdio, stdio, stdio],
      detached: false,
      shell: false,
    })
    if (stdio === 'inherit') {
      const { stdout, stderr} = _process
      // please node that this is not a full replacement for 'inherit'
      // the child process can and will detect if stdout is a pty and change output based on it
      // the terminal context is lost & ansi information (coloring) etc will be lost
      if (stdout && stderr) {
        stdout.pipe(process.stdout)
        stderr.pipe(process.stderr)
      }
    }

long term it will be something like node-pty

PhilippLgh commented 4 years ago

breaks: https://github.com/PhilippLgh/ethereum-binaries/blob/master/examples/create_account.js#L6