tty: do not end in an infinite warning recursion

It was possible that this warning ends up in an infinite recursion.
The reason is that printing the warning triggered a color check and
that triggered another warning. Limiting it to a single warning
prevents this.

PR-URL: https://github.com/nodejs/node/pull/31429
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Ruben Bridgewater 2020-01-20 21:37:14 +01:00
parent 0f6fed4f07
commit 63f10b9f0d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
6 changed files with 33 additions and 11 deletions

View File

@ -73,18 +73,26 @@ const TERM_ENVS_REG_EXP = [
/^vt100/
];
let warned = false;
function warnOnDeactivatedColors(env) {
let name;
if (warned)
return;
let name = '';
if (env.NODE_DISABLE_COLORS !== undefined)
name = 'NODE_DISABLE_COLORS';
if (env.NO_COLOR !== undefined)
name = 'NO_COLOR';
if (env.NO_COLOR !== undefined) {
if (name !== '') {
name += "' and '";
}
name += 'NO_COLOR';
}
if (name !== undefined) {
if (name !== '') {
process.emitWarning(
`The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`,
'Warning'
);
warned = true;
}
}

View File

@ -0,0 +1,8 @@
'use strict';
require('../common');
process.env.NODE_DISABLE_COLORS = '1';
process.env.FORCE_COLOR = '3';
console.log();

View File

@ -0,0 +1,2 @@
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.

View File

@ -0,0 +1,9 @@
'use strict';
require('../common');
process.env.NO_COLOR = '1';
process.env.NODE_DISABLE_COLORS = '1';
process.env.FORCE_COLOR = '3';
console.log();

View File

@ -0,0 +1,2 @@
(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.

View File

@ -1,8 +1 @@
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.