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