Inquirer.js
Inquirer.js copied to clipboard
Color is not preserved when setting output to stderr and stdout is redirected
I'm guessing this is due to chalk is not detecting the right stream for color support?
i = require("inquirer")
p = i.createPromptModule({ output: process.stderr })
p({ name: "value", message: "hello", type: "question" })
node test.js # this works
node test.js > test # this will show "? hello" without bold
Just came here to report this myself. I'm running Bitwarden Cli (which uses inquirer) commands as part of a script. Interactive output from Bitwarden is sent to stderr, which is not colored if stdout is redirected.
The source of the issue is that the default chalk instance - which detects color support for stdout - is always used, even if output goes to stderr. For stderr chalk.stderr should be used.
# test.js
const chalk = require('chalk');
function isStderr(stream) {
// Or whatever the best method of detecting stderr is.
return stream === process.stderr;
}
function getChalkFor(stream) {
//return chalk; // bad!
return isStderr(stream) ? chalk.stderr : chalk;
}
function test(stream, msg) {
const ch = getChalkFor(stream);
stream.write(`${ch.green(msg)}\n`);
}
test(process.stdout, 'stdout');
test(process.stderr, 'stderr');
This works no matter which standard stream is redirected:
node test.js
node test.js >/dev/null
node test.js 2>/dev/null
Detecting stderr by simply comparing it with process.stderr like that is probably naive, idk.