Skip to content
Myr

default.js

This file

Path

lib/verbose/default.js

63 lines · 2.0 KiB · JavaScript

Last changed by

Fix the `verbose` option printing colors when `stderr` is not a terminalSindre Sorhusbefore adoptiona2eda67

History of this file

Take it

Fix the `verbose` option printing colors when `stderr` is not a terminalSindre Sorhusbefore adoptiona2eda67

line 1why, one step back
1import process from 'node:process';

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

lines 2–3why, one step back
2import figures from 'figures';
3import {

Fix the `verbose` option printing colors when `stderr` is not a terminalSindre Sorhusbefore adoptiona2eda67

lines 4–7why, one step back
4 gray as grayColor,
5 bold as boldColor,6 redBright as redBrightColor,7 yellowBright as yellowBrightColor,

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

lines 8–9why, one step back
8} from 'yoctocolors';
9

Fix the `verbose` option printing colors when `stderr` is not a terminalSindre Sorhusbefore adoptiona2eda67

lines 10–19why, one step back
10// `yoctocolors` checks whether the terminal type supports colors, but not whether `stderr`, where this is printed, is a terminal.
11// Like `util.styleText()`, colors are not used when `stderr` is redirected, e.g. to a file, unless `FORCE_COLOR` is set.12const hasColors = process.env.FORCE_COLOR !== undefined || process.stderr.isTTY === true;13const identity = string => string;14const getColor = color => hasColors ? color : identity;15const gray = getColor(grayColor);16const bold = getColor(boldColor);17const redBright = getColor(redBrightColor);18const yellowBright = getColor(yellowBrightColor);19

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 20–29why, one step back
20// Default when `verbose` is not a function
21export const defaultVerboseFunction = ({22 type,23 message,24 timestamp,25 piped,26 commandId,27 result: {failed = false} = {},28 options: {reject = true},29}) => {

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

lines 30–63why, one step back
30 const timestampString = serializeTimestamp(timestamp);
31 const icon = ICONS[type]({failed, reject, piped});32 const color = COLORS[type]({reject});33 return `${gray(`[${timestampString}]`)} ${gray(`[${commandId}]`)} ${color(icon)} ${color(message)}`;34};3536// Prepending the timestamp allows debugging the slow paths of a subprocess37const serializeTimestamp = timestamp => `${padField(timestamp.getHours(), 2)}:${padField(timestamp.getMinutes(), 2)}:${padField(timestamp.getSeconds(), 2)}.${padField(timestamp.getMilliseconds(), 3)}`;3839const padField = (field, padding) => String(field).padStart(padding, '0');4041const getFinalIcon = ({failed, reject}) => {42 if (!failed) {43 return figures.tick;44 }4546 return reject ? figures.cross : figures.warning;47};4849const ICONS = {50 command: ({piped}) => piped ? '|' : '$',51 output: () => ' ',52 ipc: () => '*',53 error: getFinalIcon,54 duration: getFinalIcon,55};5657const COLORS = {58 command: () => bold,59 output: () => identity,60 ipc: () => identity,61 error: ({reject}) => reject ? redBright : yellowBright,62 duration: () => gray,63};