UNPKG

3.25 kBJavaScriptView Raw
1import * as tty from "tty"
2
3const env = process.env
4
5const isDisabled = "NO_COLOR" in env
6const isForced = "FORCE_COLOR" in env
7const isWindows = process.platform === "win32"
8
9const isCompatibleTerminal =
10 tty && tty.isatty(1) && env.TERM && env.TERM !== "dumb"
11
12const isCI =
13 "CI" in env &&
14 ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env)
15
16export const isColorSupported =
17 !isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI)
18
19const raw = (open, close, searchRegex, replaceValue) => (s) =>
20 open +
21 (~(s += "").indexOf(close, 4) // skip opening \x1b[
22 ? s.replace(searchRegex, replaceValue)
23 : s) +
24 close
25
26const init = (open, close) =>
27 raw(
28 `\x1b[${open}m`,
29 `\x1b[${close}m`,
30 new RegExp(`\\x1b\\[${close}m`, "g"),
31 `\x1b[${open}m`
32 )
33
34export const reset = init(0, 0)
35export const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m")
36export const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m")
37export const italic = init(3, 23)
38export const underline = init(4, 24)
39export const inverse = init(7, 27)
40export const hidden = init(8, 28)
41export const strikethrough = init(9, 29)
42export const black = init(30, 39)
43export const red = init(31, 39)
44export const green = init(32, 39)
45export const yellow = init(33, 39)
46export const blue = init(34, 39)
47export const magenta = init(35, 39)
48export const cyan = init(36, 39)
49export const white = init(37, 39)
50export const gray = init(90, 39)
51export const bgBlack = init(40, 49)
52export const bgRed = init(41, 49)
53export const bgGreen = init(42, 49)
54export const bgYellow = init(43, 49)
55export const bgBlue = init(44, 49)
56export const bgMagenta = init(45, 49)
57export const bgCyan = init(46, 49)
58export const bgWhite = init(47, 49)
59export const blackBright = init(90, 39)
60export const redBright = init(91, 39)
61export const greenBright = init(92, 39)
62export const yellowBright = init(93, 39)
63export const blueBright = init(94, 39)
64export const magentaBright = init(95, 39)
65export const cyanBright = init(96, 39)
66export const whiteBright = init(97, 39)
67export const bgBlackBright = init(100, 49)
68export const bgRedBright = init(101, 49)
69export const bgGreenBright = init(102, 49)
70export const bgYellowBright = init(103, 49)
71export const bgBlueBright = init(104, 49)
72export const bgMagentaBright = init(105, 49)
73export const bgCyanBright = init(106, 49)
74export const bgWhiteBright = init(107, 49)
75
76const none = (any) => any
77
78export const createColors = ({ useColor = isColorSupported } = {}) => ({
79 ...Object.entries({
80 reset,
81 bold,
82 dim,
83 italic,
84 underline,
85 inverse,
86 hidden,
87 strikethrough,
88 black,
89 red,
90 green,
91 yellow,
92 blue,
93 magenta,
94 cyan,
95 white,
96 gray,
97 bgBlack,
98 bgRed,
99 bgGreen,
100 bgYellow,
101 bgBlue,
102 bgMagenta,
103 bgCyan,
104 bgWhite,
105 blackBright,
106 redBright,
107 greenBright,
108 yellowBright,
109 blueBright,
110 magentaBright,
111 cyanBright,
112 whiteBright,
113 bgBlackBright,
114 bgRedBright,
115 bgGreenBright,
116 bgYellowBright,
117 bgBlueBright,
118 bgMagentaBright,
119 bgCyanBright,
120 bgWhiteBright,
121 }).reduce((colorMap, [key, color]) => ({
122 ...colorMap,
123 [key]: useColor ? color : none,
124 })),
125})