for example, npm login
command requires entering username
, password
, email
I start a child process to execute npm login
how do I know that the child process is waiting for input and input to it?
for example, npm login
command requires entering username
, password
, email
I start a child process to execute npm login
how do I know that the child process is waiting for input and input to it?
is entered in the console, use readline
, the code is as follows
Node.jsstdinspawn</a>
const fs = require('fs');
const { spawn } = require('child_process');
var subProcess = spawn('cnpm.cmd', ['login'], { cmd: __dirname });
subProcess.on('error', function() {
console.log('error');
console.log(arguments);
});
subProcess.on('close', code => {
if (code != 0) {
console.log(`:${code}`);
} else {
console.log('');
}
process.stdin.end();
});
subProcess.stdin.on('end', () => {
process.stdout.write('end');
});
subProcess.stdout.on('data', onData);
subProcess.stderr.on('data', onData);
function onData(data) {
process.stdout.write('-sharp ' + data);
process.stdin.on('data', input => {
input = input.toString().trim();
subProcess.stdin.write(input + '\n');
});
}