Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/cli-kit/src/public/node/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ function shouldOutput(logLevel: LogLevel): boolean {
// eslint-disable-next-line import-x/no-mutable-exports
export let collectedLogs: Record<string, string[]> = {}

/**
* Memoized value for the color check.
*/
let memoizedShouldDisplayColors: boolean | undefined

/**
* This is only used during UnitTesting.
* If we are in a testing context, instead of printing the logs to the console,
Expand Down Expand Up @@ -410,12 +415,18 @@ export function unstyled(message: string): string {
* @returns True if the console outputs should display colors, false otherwise.
*/
export function shouldDisplayColors(_process = process): boolean {
if (_process === process && memoizedShouldDisplayColors !== undefined) {
return memoizedShouldDisplayColors
}

const {env, stdout} = _process
if (Object.hasOwnProperty.call(env, 'FORCE_COLOR')) {
return isTruthy(env.FORCE_COLOR)
} else {
return Boolean(stdout.isTTY)
const result = Object.hasOwnProperty.call(env, 'FORCE_COLOR') ? isTruthy(env.FORCE_COLOR) : Boolean(stdout.isTTY)

if (_process === process) {
memoizedShouldDisplayColors = result
}

return result
}

/**
Expand Down
Loading