Lets say I have some nodejs questions:
ask-questions.js
const arrQuestions = [
"what is your age?",
"what is your gender?",
"where do you live?",
];
let ind = 0;
const total = arrQuestions.length
process.stdin.on("data", (data) => {
if ( ind >= arrQuestions.length) {
console.log("process - NO MORE QUESTIONS - EXIT!");
process.exit();
}
process.stdout.write(`${arrQuestions.at(ind++)}n`);
});
process.stdout.write(`${arrQuestions.at(ind++)}n`);
module.exports = {
arrQuestions,
};
When I run: node questions.js
-
stdout: What is your age?
-
user input: 2n
-
stdout: What is your gender?
-
user input: 2n
-
stdout: Where do you live?
-
user input: 2n
-
stdout: process – NO MORE QUESTIONS – EXIT!
-
process.exit()
How do I write to the stdin of this process with the following code?:
spawn-questions.js
const { arrQuestions } = require("./ask-questions");
const path = require("path");
const spawn = require("child_process").spawn;
const arrUserInput = [];
const childProcess = spawn(
"node",
[path.join(__dirname, "node-questions.js")],
{ env: process.env }
);
// These do nothing.
childProcess.stdout.write("answer 1 from spawnern");
childProcess.stdout.write("answer 2 from spawnern");
childProcess.stdout.write("answer 3 from spawnern");
childProcess.stdin.write("answer from spawnern");
childProcess.stdin.write("answer from spawnern");
childProcess.stdin.write("answer from spawnern");
// expect exit
I want to automate programmatically the user input above to trigger the next question.
How do I do that?