The problem is basically, child process prints normal outputs as errors and I think that causes to something being stopped.
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
Here I don't understand what's the reason that triggers to blocking inputs to select accept or decline installation.
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);
});
Also the program continues with nothing until cancelling with CTRL + C
but there's problem too because no exit code returns.
As a solution, can be used as cp.exec("yes | " + command, config);
but it's not completely what I want and also not safe.