@depup/chalk 6.0.0-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,206 @@
1
+ import process from 'node:process';
2
+ import os from 'node:os';
3
+ import tty from 'node:tty';
4
+
5
+ // From: https://github.com/sindresorhus/has-flag/blob/main/index.js
6
+ /// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
7
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process.argv) {
8
+ const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
9
+ const position = argv.indexOf(prefix + flag);
10
+ const terminatorPosition = argv.indexOf('--');
11
+ return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
12
+ }
13
+
14
+ const {env} = process;
15
+
16
+ let flagForceColor;
17
+ if (
18
+ hasFlag('no-color')
19
+ || hasFlag('no-colors')
20
+ || hasFlag('color=false')
21
+ || hasFlag('color=never')
22
+ ) {
23
+ flagForceColor = 0;
24
+ } else if (
25
+ hasFlag('color')
26
+ || hasFlag('colors')
27
+ || hasFlag('color=true')
28
+ || hasFlag('color=always')
29
+ ) {
30
+ flagForceColor = 1;
31
+ }
32
+
33
+ // Whether `FORCE_COLOR` names a level. Shared with the exact-level check below so that the two cannot disagree on what counts as numeric.
34
+ function hasNumericForceColor() {
35
+ return /^\d+$/.test(env.FORCE_COLOR);
36
+ }
37
+
38
+ function envForceColor() {
39
+ if (!('FORCE_COLOR' in env)) {
40
+ return;
41
+ }
42
+
43
+ if (env.FORCE_COLOR === 'false') {
44
+ return 0;
45
+ }
46
+
47
+ if (env.FORCE_COLOR === 'true' || env.FORCE_COLOR.length === 0) {
48
+ return 1;
49
+ }
50
+
51
+ if (!hasNumericForceColor()) {
52
+ return;
53
+ }
54
+
55
+ return Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
56
+ }
57
+
58
+ function translateLevel(level) {
59
+ if (level === 0) {
60
+ return false;
61
+ }
62
+
63
+ return {
64
+ level,
65
+ hasBasic: true,
66
+ has256: level >= 2,
67
+ has16m: level >= 3,
68
+ };
69
+ }
70
+
71
+ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
72
+ const noFlagForceColor = envForceColor();
73
+ if (noFlagForceColor !== undefined) {
74
+ flagForceColor = noFlagForceColor;
75
+ }
76
+
77
+ const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
78
+
79
+ if (forceColor === 0) {
80
+ return 0;
81
+ }
82
+
83
+ if (sniffFlags) {
84
+ if (hasFlag('color=16m')
85
+ || hasFlag('color=full')
86
+ || hasFlag('color=truecolor')) {
87
+ return 3;
88
+ }
89
+
90
+ if (hasFlag('color=256')) {
91
+ return 2;
92
+ }
93
+ }
94
+
95
+ // A numeric `FORCE_COLOR` requests an exact level, while `FORCE_COLOR=true` and `FORCE_COLOR=` only enable color and let the level be detected.
96
+ if (forceColor !== undefined && hasNumericForceColor()) {
97
+ return forceColor;
98
+ }
99
+
100
+ // Check for Azure DevOps pipelines.
101
+ // Has to be above the `!streamIsTTY` check.
102
+ if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
103
+ return 1;
104
+ }
105
+
106
+ if (haveStream && !streamIsTTY && forceColor === undefined) {
107
+ return 0;
108
+ }
109
+
110
+ const min = forceColor || 0;
111
+
112
+ if (env.TERM === 'dumb') {
113
+ return min;
114
+ }
115
+
116
+ if (process.platform === 'win32') {
117
+ // Windows 10 build 10586 is the first Windows release that supports 256 colors.
118
+ // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
119
+ const osRelease = os.release().split('.');
120
+ if (
121
+ Number(osRelease[0]) >= 10
122
+ && Number(osRelease[2]) >= 10_586
123
+ ) {
124
+ return Number(osRelease[2]) >= 14_931 ? 3 : 2;
125
+ }
126
+
127
+ return 1;
128
+ }
129
+
130
+ if ('CI' in env) {
131
+ if (['GITHUB_ACTIONS', 'GITEA_ACTIONS', 'CIRCLECI'].some(key => key in env)) {
132
+ return 3;
133
+ }
134
+
135
+ if (['TRAVIS', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
136
+ return 1;
137
+ }
138
+
139
+ return min;
140
+ }
141
+
142
+ if ('TEAMCITY_VERSION' in env) {
143
+ return /^(?:9\.0*[1-9]\d*\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
144
+ }
145
+
146
+ if (env.COLORTERM === 'truecolor') {
147
+ return 3;
148
+ }
149
+
150
+ if (env.TERM === 'xterm-kitty') {
151
+ return 3;
152
+ }
153
+
154
+ if (env.TERM === 'xterm-ghostty') {
155
+ return 3;
156
+ }
157
+
158
+ if (env.TERM === 'wezterm') {
159
+ return 3;
160
+ }
161
+
162
+ if ('TERM_PROGRAM' in env) {
163
+ const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.', 1)[0], 10);
164
+
165
+ switch (env.TERM_PROGRAM) {
166
+ case 'iTerm.app': {
167
+ return version >= 3 ? 3 : 2;
168
+ }
169
+
170
+ case 'Apple_Terminal': {
171
+ return 2;
172
+ }
173
+ // No default
174
+ }
175
+ }
176
+
177
+ if (/-256(?:color)?$/i.test(env.TERM)) {
178
+ return 2;
179
+ }
180
+
181
+ if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
182
+ return 1;
183
+ }
184
+
185
+ if ('COLORTERM' in env) {
186
+ return 1;
187
+ }
188
+
189
+ return min;
190
+ }
191
+
192
+ export function createSupportsColor(stream, options = {}) {
193
+ const level = _supportsColor(stream, {
194
+ streamIsTTY: stream && stream.isTTY,
195
+ ...options,
196
+ });
197
+
198
+ return translateLevel(level);
199
+ }
200
+
201
+ const supportsColor = {
202
+ stdout: createSupportsColor({isTTY: tty.isatty(1)}),
203
+ stderr: createSupportsColor({isTTY: tty.isatty(2)}),
204
+ };
205
+
206
+ export default supportsColor;