diff --git a/packages/cli-kit/src/public/node/output.ts b/packages/cli-kit/src/public/node/output.ts index d1488702da..9e058d529b 100644 --- a/packages/cli-kit/src/public/node/output.ts +++ b/packages/cli-kit/src/public/node/output.ts @@ -229,6 +229,11 @@ function shouldOutput(logLevel: LogLevel): boolean { // eslint-disable-next-line import-x/no-mutable-exports export let collectedLogs: Record = {} +/** + * 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, @@ -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 } /**