I'm trying to write a node script that is able to run an external command that has two layers of user input.
const conf = spawn('my command', {shell: true});
conf.stdout.on('data', (data) => {
process.stdout.write(data)
conf.stdin.write("\n")
})
Basically spawn runs and I get the external command's output correctly showing by writing it to stdout, I then simulate the user pressing enter by writing to stdin conf.stdin.write("\n"), the problem I am having is that when that is simulated I get another prompt from the command I ran in spawn and it just goes on a eternal loop.
Is there a way of listening to the first set of data, write the input, read the second set of data and pass some input the user writes in the terminal?
Many thanks
You could use the prompt-sync library, get user input and then write it to stdin:
const prompt = require("prompt-sync")();
const { spawn } = require("node:child_process");
const conf = spawn("dir", { shell: true });
conf.stdout.on("data", async (data) => {
process.stdout.write(data);
conf.stdin.write("\n");
const input = prompt("");
conf.stdin.write(input);
});