Skip to content

Please implement a feature where one can check if stdout wants input. #16214

@Megajin

Description

@Megajin

I had a little freetime so I decided to rewrite all my bash scripts in JavaScript (NodeJS - ES6) with child processes. Everything went smoothly until I wanted to automate user input.

Yes, you can do automate the user input. But there is one Problem - you can't determine if the given data event is a feedback or a request for input. At least I can't find a way to do it.

So basically you can do this:

// new Spawn.
let spawn = require('child_process');
// new ufw process.
let ufw = spawn('ufw', ['enable']);

// Use defined input.
ufw.stdin.setEncoding('utf-8');
ufw.stdout.pipe(process.stdout);
ufw.stdin.write('y\n');

// Event Standard Out.
ufw.stdout.on('data', (data) => {
  console.log(data.toString('utf8'));
});

// Event Standard Error.
ufw.stderr.on('data', (err) => {
  // Logerror.
  console.log(err);
});

// When job is finished (with or without error) it ends up here.
ufw.on('close', (code) => {
  // Check if there were errors.
  if (code !== 0) console.log('Exited with code: ' + code.toString());
  // End input stream.
  ufw.stdin.end();
});

The given example only works if there is one guaranteed input. But what about multiple inputs? And how can I tell my code to use the right input on the right time?

I don't think that nodejs has a built in feature which will tell me "Ok, now I need input!". Because the only way to get a non error feedback is through:

 // Event Standard Out.
process.stdout.on('data', (data) => {
   // Feedback.
   console.log(data);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.wrong repoIssues that should be opened in another repository.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions