0

问题基本上是,子进程将正常输出打印为错误,我认为这会导致某些事情被停止。

ERROR 1
==> WARNING: A package has already been built, installing existing package...

[sudo] password for user: 
// That point, I can enter my password and the input works.
ERROR 1
warning: spotify-1:1.1.67.586-1 is up to date -- reinstalling

ERROR 1
:: Proceed with installation? [Y/n] 
// But here, whatever I enter it doesn't work
// Even if I send Y or n from stdin.write()
Send inputs from stderr
Y
Y
n
n

在这里,我不明白触发阻止输入以选择接受或拒绝安装的原因是什么。

  const config = { cwd: packagePath }
  const child = cp.exec(command, config);

  child.stdout.on("data", (data) => {
    const output = data.toString();
    // console.log(output);
    if (output.includes(":: Proceed with installation? [Y/n]")) {
      console.log("Send inputs from stdout");
      process.stdin.write("Y\n");
      process.stdout.write("Y\n");
      process.stdin.write("n\n");
      process.stdout.write("n\n");
    }
  });

  child.stderr.on("data", (data) => {
    console.log("ERROR 1");
    const output = data.toString();
    console.log(output);
    if (output.includes(":: Proceed with installation? [Y/n]")) {
      console.log("Send inputs from stderr");
      process.stdin.write("Y\n");
      process.stdout.write("Y\n");
      process.stdin.write("n\n");
      process.stdout.write("n\n");
    }
  });

  child.on("error", (err) => {
    console.log("ERROR 2");
    console.log(err);
  });

  child.on("exit", (code) => {
    console.log("Exit code: " + code);
  });

此外,程序在取消之前什么都没有继续,CTRL + C但也有问题,因为没有退出代码返回。

4

1 回答 1

0

作为一种解决方案,可以用作cp.exec("yes | " + command, config);但它不完全是我想要的,也不安全

于 2021-10-15T19:42:20.733 回答