From 18d8019999a74c9b29dd4ec7a0d9e2a473f677d1 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 7 Mar 2023 20:23:49 +0100 Subject: [PATCH] refactor: update colors implementation (#2975) --- packages/utils/src/colors.ts | 109 ++++++++---------- .../test/__snapshots__/mocked.test.ts.snap | 80 ++++++------- test/core/test/setup.ts | 3 +- 3 files changed, 93 insertions(+), 99 deletions(-) diff --git a/packages/utils/src/colors.ts b/packages/utils/src/colors.ts index f40cb0de8136..94047a0e92e8 100644 --- a/packages/utils/src/colors.ts +++ b/packages/utils/src/colors.ts @@ -1,43 +1,52 @@ import { SAFE_COLORS_SYMBOL } from './constants' -type Colors = ReturnType +const colorsMap = { + bold: ['\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m'], + dim: ['\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m'], + italic: ['\x1B[3m', '\x1B[23m'], + underline: ['\x1B[4m', '\x1B[24m'], + inverse: ['\x1B[7m', '\x1B[27m'], + hidden: ['\x1B[8m', '\x1B[28m'], + strikethrough: ['\x1B[9m', '\x1B[29m'], + black: ['\x1B[30m', '\x1B[39m'], + red: ['\x1B[31m', '\x1B[39m'], + green: ['\x1B[32m', '\x1B[39m'], + yellow: ['\x1B[33m', '\x1B[39m'], + blue: ['\x1B[34m', '\x1B[39m'], + magenta: ['\x1B[35m', '\x1B[39m'], + cyan: ['\x1B[36m', '\x1B[39m'], + white: ['\x1B[37m', '\x1B[39m'], + gray: ['\x1B[90m', '\x1B[39m'], + bgBlack: ['\x1B[40m', '\x1B[49m'], + bgRed: ['\x1B[41m', '\x1B[49m'], + bgGreen: ['\x1B[42m', '\x1B[49m'], + bgYellow: ['\x1B[43m', '\x1B[49m'], + bgBlue: ['\x1B[44m', '\x1B[49m'], + bgMagenta: ['\x1B[45m', '\x1B[49m'], + bgCyan: ['\x1B[46m', '\x1B[49m'], + bgWhite: ['\x1B[47m', '\x1B[49m'], +} as const -const colors = [ - 'reset', - 'bold', - 'dim', - 'italic', - 'underline', - 'inverse', - 'hidden', - 'strikethrough', - 'black', - 'red', - 'green', - 'yellow', - 'blue', - 'magenta', - 'cyan', - 'white', - 'gray', - 'bgBlack', - 'bgRed', - 'bgGreen', - 'bgYellow', - 'bgBlue', - 'bgMagenta', - 'bgCyan', - 'bgWhite', -] as const +type ColorName = keyof typeof colorsMap +type ColorsMethods = { + [Key in ColorName]: (input: unknown) => string +} + +type Colors = ColorsMethods & { + isColorSupported: boolean + reset: (input: unknown) => string +} + +const colorsEntries = Object.entries(colorsMap) const string = (str: unknown) => String(str) string.open = '' string.close = '' -const defaultColors = colors.reduce((acc, key) => { - acc[key] = string +const defaultColors = colorsEntries.reduce((acc, [key]) => { + acc[key as ColorName] = string return acc -}, {} as Colors) +}, { isColorSupported: false } as Colors) export function getDefaultColors(): Colors { return { ...defaultColors } @@ -47,7 +56,7 @@ export function getColors(): Colors { return (globalThis as any)[SAFE_COLORS_SYMBOL] || defaultColors } -export function createColors(isTTY = false) { +export function createColors(isTTY = false): Colors { const enabled = typeof process !== 'undefined' && !('NO_COLOR' in process.env || process.argv.includes('--no-color')) && !('GITHUB_ACTIONS' in process.env) @@ -66,7 +75,7 @@ export function createColors(isTTY = false) { const formatter = (open: string, close: string, replace = open) => { const fn = (input: unknown) => { - const string = `${input}` + const string = String(input) const index = string.indexOf(close, open.length) return ~index ? open + replaceClose(string, close, replace, index) + close @@ -78,34 +87,18 @@ export function createColors(isTTY = false) { } // based on "https://github.com/alexeyraspopov/picocolors", but browser-friendly - return { + const colorsObject = { isColorSupported: enabled, reset: enabled ? (s: string) => `\x1B[0m${s}\x1B[0m` : string, - bold: enabled ? formatter('\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m') : string, - dim: enabled ? formatter('\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m') : string, - italic: enabled ? formatter('\x1B[3m', '\x1B[23m') : string, - underline: enabled ? formatter('\x1B[4m', '\x1B[24m') : string, - inverse: enabled ? formatter('\x1B[7m', '\x1B[27m') : string, - hidden: enabled ? formatter('\x1B[8m', '\x1B[28m') : string, - strikethrough: enabled ? formatter('\x1B[9m', '\x1B[29m') : string, - black: enabled ? formatter('\x1B[30m', '\x1B[39m') : string, - red: enabled ? formatter('\x1B[31m', '\x1B[39m') : string, - green: enabled ? formatter('\x1B[32m', '\x1B[39m') : string, - yellow: enabled ? formatter('\x1B[33m', '\x1B[39m') : string, - blue: enabled ? formatter('\x1B[34m', '\x1B[39m') : string, - magenta: enabled ? formatter('\x1B[35m', '\x1B[39m') : string, - cyan: enabled ? formatter('\x1B[36m', '\x1B[39m') : string, - white: enabled ? formatter('\x1B[37m', '\x1B[39m') : string, - gray: enabled ? formatter('\x1B[90m', '\x1B[39m') : string, - bgBlack: enabled ? formatter('\x1B[40m', '\x1B[49m') : string, - bgRed: enabled ? formatter('\x1B[41m', '\x1B[49m') : string, - bgGreen: enabled ? formatter('\x1B[42m', '\x1B[49m') : string, - bgYellow: enabled ? formatter('\x1B[43m', '\x1B[49m') : string, - bgBlue: enabled ? formatter('\x1B[44m', '\x1B[49m') : string, - bgMagenta: enabled ? formatter('\x1B[45m', '\x1B[49m') : string, - bgCyan: enabled ? formatter('\x1B[46m', '\x1B[49m') : string, - bgWhite: enabled ? formatter('\x1B[47m', '\x1B[49m') : string, + } as Colors + + for (const [name, formatterArgs] of colorsEntries) { + colorsObject[name as ColorName] = enabled + ? formatter(...formatterArgs as [string, string]) + : string } + + return colorsObject } export function setupColors(colors: Colors) { diff --git a/test/core/test/__snapshots__/mocked.test.ts.snap b/test/core/test/__snapshots__/mocked.test.ts.snap index 24ac5b21f83a..5de49f7f9ee6 100644 --- a/test/core/test/__snapshots__/mocked.test.ts.snap +++ b/test/core/test/__snapshots__/mocked.test.ts.snap @@ -1,79 +1,79 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`mocked function which fails on toReturnWith > just one call 1`] = ` -"expected \\"spy\\" to return with: 2 at least once +"expected \\"spy\\" to return with: 2 at least once Received: - 1st spy call return: + 1st spy call return: - 1 - 2 - + 1 + 2 -Number of calls: 1 -" + +Number of calls: 1 +" `; exports[`mocked function which fails on toReturnWith > multi calls 1`] = ` -"expected \\"spy\\" to return with: 2 at least once +"expected \\"spy\\" to return with: 2 at least once Received: - 1st spy call return: + 1st spy call return: + + 1 + 2 - 1 - 2 + 2nd spy call return: - 2nd spy call return: + 1 + 2 - 1 - 2 + 3rd spy call return: - 3rd spy call return: + 1 + 2 - 1 - 2 - -Number of calls: 3 -" +Number of calls: 3 +" `; exports[`mocked function which fails on toReturnWith > oject type 1`] = ` -"expected \\"spy\\" to return with: { a: '4' } at least once +"expected \\"spy\\" to return with: { a: '4' } at least once Received: - 1st spy call return: + 1st spy call return: - Object { - - \\"a\\": \\"1\\", - + \\"a\\": \\"4\\", + Object { + - \\"a\\": \\"1\\", + + \\"a\\": \\"4\\", } - 2nd spy call return: + 2nd spy call return: - Object { - - \\"a\\": \\"1\\", - + \\"a\\": \\"4\\", + Object { + - \\"a\\": \\"1\\", + + \\"a\\": \\"4\\", } - 3rd spy call return: + 3rd spy call return: - Object { - - \\"a\\": \\"1\\", - + \\"a\\": \\"4\\", + Object { + - \\"a\\": \\"1\\", + + \\"a\\": \\"4\\", } - -Number of calls: 3 -" + +Number of calls: 3 +" `; exports[`mocked function which fails on toReturnWith > zero call 1`] = ` -"expected \\"spy\\" to return with: 2 at least once +"expected \\"spy\\" to return with: 2 at least once Received: - -Number of calls: 0 -" + +Number of calls: 0 +" `; diff --git a/test/core/test/setup.ts b/test/core/test/setup.ts index f9a460bb2cc0..4c3ee23e0aad 100644 --- a/test/core/test/setup.ts +++ b/test/core/test/setup.ts @@ -1,7 +1,8 @@ +import { getDefaultColors, setupColors } from '@vitest/utils' import { beforeEach, vi } from 'vitest' vi.mock('../src/global-mock', () => ({ mocked: true })) beforeEach(() => { - // console.log(`hi ${s.name}`) + setupColors(getDefaultColors()) })