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