Add colors to `verbose` option (#893)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent a7f8598 · Browse files · 6 files·+34 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+34 added−18 removed
- tests:+9 added−0 removed
- other:+25 added−18 removed
Provenance
- Commit
281d504- Tree
339e25b- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a7f8598
6 files changed+34 added−18 removed
modified:lib/verbose/complete.js+3 added-2 removed
12345613 unmodified lines2021222323242526272829303031323334355 unmodified linesimport prettyMs from 'pretty-ms';import {gray} from 'yoctocolors';import {escapeLines} from '../arguments/escape.js';import {verboseLog} from './log.js';import {getCommandDuration} from './info.js';import {logError} from './error.js';13 unmodified lines return; }
const icon = getIcon(failed, reject); logError({message, failed, verboseId, icon}); logError({message, failed, reject, verboseId, icon}); logDuration(startTime, verboseId, icon);};
const logDuration = (startTime, verboseId, icon) => { const durationMs = getCommandDuration(startTime); const durationMessage = `(done in ${prettyMs(durationMs)})`; verboseLog(durationMessage, verboseId, icon); verboseLog(durationMessage, verboseId, icon, gray);};
const getIcon = (failed, reject) => { if (!failed) {5 unmodified linesmodified:lib/verbose/error.js+4 added-2 removed
12344567891091112import {redBright, yellowBright} from 'yoctocolors';import {verboseLog} from './log.js';
// When `verbose` is `short|full`, print each command's error when it failsexport const logError = ({message, failed, verboseId, icon}) => {export const logError = ({message, failed, reject, verboseId, icon}) => { if (!failed) { return; }
const color = reject ? redBright : yellowBright; verboseLog(message, verboseId, icon); verboseLog(message, verboseId, icon, color);};modified:lib/verbose/log.js+13 added-10 removed
12345566789101011121313141515161717181920211819202122232425262728293012 unmodified linesimport {writeFileSync} from 'node:fs';import process from 'node:process';import {gray} from 'yoctocolors';
// Write synchronously to ensure lines are properly ordered and not interleaved with `stdout`export const verboseLog = (string, verboseId, icon) => { const prefixedLines = addPrefix(string, verboseId, icon);export const verboseLog = (string, verboseId, icon, color) => { const prefixedLines = addPrefix(string, verboseId, icon, color); writeFileSync(process.stderr.fd, `${prefixedLines}\n`);};
const addPrefix = (string, verboseId, icon) => string.includes('\n')const addPrefix = (string, verboseId, icon, color) => string.includes('\n') ? string .split('\n') .map(line => addPrefixToLine(line, verboseId, icon)) .map(line => addPrefixToLine(line, verboseId, icon, color)) .join('\n') : addPrefixToLine(string, verboseId, icon); : addPrefixToLine(string, verboseId, icon, color);
const addPrefixToLine = (line, verboseId, icon) => [ `[${getTimestamp()}]`, `[${verboseId}]`, ICONS[icon], line,const addPrefixToLine = (line, verboseId, icon, color = identity) => [ gray(`[${getTimestamp()}]`), gray(`[${verboseId}]`), color(ICONS[icon]), color(line),].join(' ');
const identity = string => string;
// Prepending the timestamp allows debugging the slow paths of a processconst getTimestamp = () => { const date = new Date(); return `${padField(date.getHours(), 2)}:${padField(date.getMinutes(), 2)}:${padField(date.getSeconds(), 2)}.${padField(date.getMilliseconds(), 3)}`;12 unmodified linesmodified:lib/verbose/start.js+2 added-1 removed
12345678910101112import {bold} from 'yoctocolors';import {verboseLog} from './log.js';
// When `verbose` is `short|full`, print each commandexport const logCommand = (escapedCommand, {verbose, verboseId}, {piped = false}) => { if (verbose === 'none') { return; }
const icon = piped ? 'pipedCommand' : 'command'; verboseLog(escapedCommand, verboseId, icon); verboseLog(escapedCommand, verboseId, icon, bold);};modified:test/verbose/log.js+9 added
1234510 unmodified lines161718192021222324252627import {stripVTControlCharacters} from 'node:util';import test from 'ava';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {foobarString} from '../helpers/input.js';import {nestedExecaAsync, nestedExecaSync} from '../helpers/verbose.js';10 unmodified linestest('Logs on stderr not stdout, verbose "full"', testNoStdout, 'full', nestedExecaAsync);test('Logs on stderr not stdout, verbose "none", sync', testNoStdout, 'none', nestedExecaSync);test('Logs on stderr not stdout, verbose "short", sync', testNoStdout, 'short', nestedExecaSync);test('Logs on stderr not stdout, verbose "full", sync', testNoStdout, 'full', nestedExecaSync);
const testColor = async (t, expectedResult, forceColor) => { const {stderr} = await nestedExecaAsync('noop.js', [foobarString], {verbose: 'short'}, {env: {FORCE_COLOR: forceColor}}); t.is(stderr !== stripVTControlCharacters(stderr), expectedResult);};
test('Prints with colors if supported', testColor, true, '1');test('Prints without colors if not supported', testColor, false, '0');modified:package.json+3 added-3 removed
52 unmodified lines53 "is-stream": "^4.0.1",54 "npm-run-path": "^5.2.0",55 "pretty-ms": "^9.0.0",56 "signal-exit": "^4.1.0",57- "strip-final-newline": "^4.0.0"57+ "strip-final-newline": "^4.0.0",58+ "yoctocolors": "^2.0.0"59 },60 "devDependencies": {61 "@types/node": "^20.8.9",62 "ava": "^6.0.1",4 unmodified lines67 "path-key": "^4.0.0",68 "tempfile": "^5.0.0",69 "tsd": "^0.29.0",70 "which": "^4.0.0",70- "xo": "^0.56.0",71- "yoctocolors": "^2.0.0"71+ "xo": "^0.56.0"72 },73 "c8": {74 "reporter": [75 "text",20 unmodified lines