Skip to content

Commit 427b062

Browse files
committedMar 7, 2023
feat: use custom colors implementation instead of picocolors
1 parent 1bdcc21 commit 427b062

File tree

5 files changed

+110
-46
lines changed

5 files changed

+110
-46
lines changed
 

‎packages/expect/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export * from './types'
55
export { getState, setState } from './state'
66
export { JestChaiExpect } from './jest-expect'
77
export { JestExtend } from './jest-extend'
8-
export { setColors as setupColors } from '@vitest/utils'
8+
export { setupColors } from '@vitest/utils'

‎packages/utils/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"cli-truncate": "^3.1.0",
4141
"diff": "^5.1.0",
4242
"loupe": "^2.3.6",
43-
"picocolors": "^1.0.0",
4443
"pretty-format": "^27.5.1"
4544
},
4645
"devDependencies": {

‎packages/utils/src/colors.ts

+74-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type p from 'picocolors'
2-
import type { Formatter } from 'picocolors/types'
31
import { SAFE_COLORS_SYMBOL } from './constants'
42

3+
type Colors = ReturnType<typeof createColors>
4+
55
const colors = [
66
'reset',
77
'bold',
@@ -30,17 +30,84 @@ const colors = [
3030
'bgWhite',
3131
] as const
3232

33-
const formatter: Formatter = str => String(str)
33+
const string = (str: unknown) => String(str)
34+
string.open = ''
35+
string.close = ''
3436

3537
const defaultColors = colors.reduce((acc, key) => {
36-
acc[key] = formatter
38+
acc[key] = string
3739
return acc
38-
}, { isColorSupported: false } as typeof p)
40+
}, {} as Colors)
41+
42+
export function getDefaultColors(): Colors {
43+
return { ...defaultColors }
44+
}
3945

40-
export function getColors(): typeof p {
46+
export function getColors(): Colors {
4147
return (globalThis as any)[SAFE_COLORS_SYMBOL] || defaultColors
4248
}
4349

44-
export function setColors(colors: typeof p) {
50+
export function createColors(isTTY = false) {
51+
const enabled = typeof process !== 'undefined'
52+
&& !('NO_COLOR' in process.env || process.argv.includes('--no-color'))
53+
&& !('GITHUB_ACTIONS' in process.env)
54+
&& ('FORCE_COLOR' in process.env
55+
|| process.argv.includes('--color')
56+
|| process.platform === 'win32'
57+
|| (isTTY && process.env.TERM !== 'dumb')
58+
|| 'CI' in process.env)
59+
60+
const replaceClose = (string: string, close: string, replace: string, index: number): string => {
61+
const start = string.substring(0, index) + replace
62+
const end = string.substring(index + close.length)
63+
const nextIndex = end.indexOf(close)
64+
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
65+
}
66+
67+
const formatter = (open: string, close: string, replace = open) => {
68+
const fn = (input: unknown) => {
69+
const string = `${input}`
70+
const index = string.indexOf(close, open.length)
71+
return ~index
72+
? open + replaceClose(string, close, replace, index) + close
73+
: open + string + close
74+
}
75+
fn.open = open
76+
fn.close = close
77+
return fn
78+
}
79+
80+
// based on "https://github.com/alexeyraspopov/picocolors", but browser-friendly
81+
return {
82+
isColorSupported: enabled,
83+
reset: enabled ? (s: string) => `\x1B[0m${s}\x1B[0m` : string,
84+
bold: enabled ? formatter('\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m') : string,
85+
dim: enabled ? formatter('\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m') : string,
86+
italic: enabled ? formatter('\x1B[3m', '\x1B[23m') : string,
87+
underline: enabled ? formatter('\x1B[4m', '\x1B[24m') : string,
88+
inverse: enabled ? formatter('\x1B[7m', '\x1B[27m') : string,
89+
hidden: enabled ? formatter('\x1B[8m', '\x1B[28m') : string,
90+
strikethrough: enabled ? formatter('\x1B[9m', '\x1B[29m') : string,
91+
black: enabled ? formatter('\x1B[30m', '\x1B[39m') : string,
92+
red: enabled ? formatter('\x1B[31m', '\x1B[39m') : string,
93+
green: enabled ? formatter('\x1B[32m', '\x1B[39m') : string,
94+
yellow: enabled ? formatter('\x1B[33m', '\x1B[39m') : string,
95+
blue: enabled ? formatter('\x1B[34m', '\x1B[39m') : string,
96+
magenta: enabled ? formatter('\x1B[35m', '\x1B[39m') : string,
97+
cyan: enabled ? formatter('\x1B[36m', '\x1B[39m') : string,
98+
white: enabled ? formatter('\x1B[37m', '\x1B[39m') : string,
99+
gray: enabled ? formatter('\x1B[90m', '\x1B[39m') : string,
100+
bgBlack: enabled ? formatter('\x1B[40m', '\x1B[49m') : string,
101+
bgRed: enabled ? formatter('\x1B[41m', '\x1B[49m') : string,
102+
bgGreen: enabled ? formatter('\x1B[42m', '\x1B[49m') : string,
103+
bgYellow: enabled ? formatter('\x1B[43m', '\x1B[49m') : string,
104+
bgBlue: enabled ? formatter('\x1B[44m', '\x1B[49m') : string,
105+
bgMagenta: enabled ? formatter('\x1B[45m', '\x1B[49m') : string,
106+
bgCyan: enabled ? formatter('\x1B[46m', '\x1B[49m') : string,
107+
bgWhite: enabled ? formatter('\x1B[47m', '\x1B[49m') : string,
108+
}
109+
}
110+
111+
export function setupColors(colors: Colors) {
45112
(globalThis as any)[SAFE_COLORS_SYMBOL] = colors
46113
}

‎packages/vitest/src/runtime/setup.node.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createRequire } from 'node:module'
2-
import p from 'picocolors'
2+
import { isatty } from 'node:tty'
33
import { installSourcemapsSupport } from 'vite-node/source-map'
4-
import { setColors } from '@vitest/utils'
4+
import { createColors, setupColors } from '@vitest/utils'
55
import { environments } from '../integrations/env'
66
import type { Environment, ResolvedConfig } from '../types'
77
import { getSafeTimers, getWorkerState } from '../utils'
@@ -29,13 +29,13 @@ export async function setupGlobalEnv(config: ResolvedConfig) {
2929

3030
globalSetup = true
3131
setupSnapshotEnvironment(new NodeSnapshotEnvironment())
32-
setColors(p)
32+
setupColors(createColors(isatty(1)))
3333

34-
const require = createRequire(import.meta.url)
34+
const _require = createRequire(import.meta.url)
3535
// always mock "required" `css` files, because we cannot process them
36-
require.extensions['.css'] = () => ({})
37-
require.extensions['.scss'] = () => ({})
38-
require.extensions['.sass'] = () => ({})
36+
_require.extensions['.css'] = () => ({})
37+
_require.extensions['.scss'] = () => ({})
38+
_require.extensions['.sass'] = () => ({})
3939

4040
const state = getWorkerState()
4141

‎pnpm-lock.yaml

+28-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.