Fix the `verbose` option printing colors when `stderr` is not a terminal
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 6925cc3 · Browse files · 2 files·+19 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+19 added−8 removed
- tests:+4 added−2 removed
- other:+15 added−6 removed
Provenance
- Commit
a2eda67- Tree
88391ab- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
6925cc3
2 files changed+19 added−8 removed
modified:lib/verbose/default.js+15 added-6 removed
1233456456789101112131415161718192021222329 unmodified lines535455564647575859603 unmodified linesimport process from 'node:process';import figures from 'figures';import { gray, bold, redBright, yellowBright, gray as grayColor, bold as boldColor, redBright as redBrightColor, yellowBright as yellowBrightColor,} from 'yoctocolors';
// `yoctocolors` checks whether the terminal type supports colors, but not whether `stderr`, where this is printed, is a terminal.// Like `util.styleText()`, colors are not used when `stderr` is redirected, e.g. to a file, unless `FORCE_COLOR` is set.const hasColors = process.env.FORCE_COLOR !== undefined || process.stderr.isTTY === true;const identity = string => string;const getColor = color => hasColors ? color : identity;const gray = getColor(grayColor);const bold = getColor(boldColor);const redBright = getColor(redBrightColor);const yellowBright = getColor(yellowBrightColor);
// Default when `verbose` is not a functionexport const defaultVerboseFunction = ({ type, message,29 unmodified lines error: getFinalIcon, duration: getFinalIcon,};
const identity = string => string;
const COLORS = { command: () => bold, output: () => identity, ipc: () => identity,3 unmodified linesmodified:test/verbose/log.js+4 added-2 removed
20 unmodified lines212223242526252627282930313233343536379 unmodified lines20 unmodified linestest('Logs on stderr not stdout, verbose "none", sync', () => testNoStdout('none', true));test('Logs on stderr not stdout, verbose "short", sync', () => testNoStdout('short', true));test('Logs on stderr not stdout, verbose "full", sync', () => testNoStdout('full', true));
const testColor = async (expectedResult, forceColor) => { const {stderr} = await nestedSubprocess('noop.js', [foobarString], {verbose: 'short'}, {env: {FORCE_COLOR: forceColor, NO_COLOR: undefined}});const testColor = async (expectedResult, forceColor, term) => { const {stderr} = await nestedSubprocess('noop.js', [foobarString], {verbose: 'short'}, {env: {FORCE_COLOR: forceColor, NO_COLOR: undefined, TERM: term}}); assert.equal(stderr !== stripVTControlCharacters(stderr), expectedResult);};
test('Prints with colors if supported', () => testColor(true, '1'));test('Prints without colors if not supported', () => testColor(false, '0'));// `stderr` is not a terminal, e.g. when redirected to a file, even though the terminal type supports colorstest('Prints without colors if stderr is not a terminal', () => testColor(false, undefined, 'xterm-256color'));
test('Prints lines in order when interleaved with subprocess stderr', async () => { const results = await Promise.all(Array.from({length: PARALLEL_COUNT}, () => nestedSubprocess('noop-fd.js', ['2', `${foobarString}\n`], {verbose: 'full', stderr: 'inherit'}, {all: true})));9 unmodified lines