0

first of all, sorry if this is a duplicate. Couldn't find a solution that worked for me.

I'm implementing a CLI tool based on node and have one of my tasks which asks the user to enter some information based on an initial parameter that the user has entered. In this particular case, the user will be setting a number N of steps, and will be asked the name of those N steps.

I'm trying to make a tool to autofill this with some values (for testing purposes) but am not able to make it work. Here is my piece of code (Assuming N = 2).

const child = childprocess.spawn(`my script`, [myArgs]);

child.on('error', (m) => { 
  console.log('ERROR: ', m);
})

child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
  console.log('stdout: ' + data);
});

child.stdin.setEncoding('utf-8');

child.stdin.write("step1\n");

child.stdin.write("step2\n");

child.stdin.end();

So my expected behavior would be that my script would take the first step name as step1 and the second one as step2.

Can somebody please help with this?

Thanks in advance

BalB
  • 107
  • 1
  • 13

1 Answers1

0

Looks like adding a synchronous wait between both write annotations made the trick. In my case I set it up to 2000 ms.

Just commented in case this can help somebody.

BalB
  • 107
  • 1
  • 13