Friday, 22 March 2019

NodeJS get color in readline

I have a NodeJS app that needs to execute some commands using spawn, I'm reading the output for later processing using readline which works flawlessly.

But I also need to get the color of the text, For example: when executing another Node.js script that uses chalk module.

How can this be accomplished ?

This is my code so far:

const spawn = require('child_process').spawn;
const readline = require('readline');

let myCommand = spawn(<command>, [<args...>], { cwd: dirPath });

readline.createInterface({
  input    : myCommand.stdout,
  terminal : true
}).on('line', (line) => handleOutput(myCommand.pid, line));

readline.createInterface({
  input    : myCommand.stderr,
  terminal : true
}).on('line', (line) => handleErrorOutput(myCommand.pid, line));

myCommand.on('exit', (code) => {
  // Do more stuff ..
});



from NodeJS get color in readline

No comments:

Post a Comment