supports-color 9.0.0 → 9.2.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.
package/browser.js CHANGED
@@ -1,25 +1,17 @@
1
1
  /* eslint-env browser */
2
2
 
3
- function getChromeVersion() {
4
- const matches = /(Chrome|Chromium)\/(?<chromeVersion>\d+)\./.exec(navigator.userAgent);
3
+ const isBlinkBasedBrowser = /\b(Chrome|Chromium)\//.test(navigator.userAgent);
5
4
 
6
- if (!matches) {
7
- return;
8
- }
9
-
10
- return Number.parseInt(matches.groups.chromeVersion, 10);
11
- }
12
-
13
- const colorSupport = getChromeVersion() >= 69 ? {
5
+ const colorSupport = isBlinkBasedBrowser ? {
14
6
  level: 1,
15
7
  hasBasic: true,
16
8
  has256: false,
17
- has16m: false
9
+ has16m: false,
18
10
  } : false;
19
11
 
20
12
  const supportsColor = {
21
13
  stdout: colorSupport,
22
- stderr: colorSupport
14
+ stderr: colorSupport,
23
15
  };
24
16
 
25
17
  export default supportsColor;
package/index.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ import {WriteStream} from 'node:tty';
2
+
3
+ export interface Options {
4
+ /**
5
+ Whether `process.argv` should be sniffed for `--color` and `--no-color` flags.
6
+
7
+ @default true
8
+ */
9
+ readonly sniffFlags?: boolean;
10
+ }
11
+
12
+ /**
13
+ Levels:
14
+ - `0` - All colors disabled.
15
+ - `1` - Basic 16 colors support.
16
+ - `2` - ANSI 256 colors support.
17
+ - `3` - Truecolor 16 million colors support.
18
+ */
19
+ export type ColorSupportLevel = 0 | 1 | 2 | 3;
20
+
21
+ /**
22
+ Detect whether the terminal supports color.
23
+ */
24
+ export interface ColorSupport {
25
+ /**
26
+ The color level.
27
+ */
28
+ level: ColorSupportLevel;
29
+
30
+ /**
31
+ Whether basic 16 colors are supported.
32
+ */
33
+ hasBasic: boolean;
34
+
35
+ /**
36
+ Whether ANSI 256 colors are supported.
37
+ */
38
+ has256: boolean;
39
+
40
+ /**
41
+ Whether Truecolor 16 million colors are supported.
42
+ */
43
+ has16m: boolean;
44
+ }
45
+
46
+ export type ColorInfo = ColorSupport | false;
47
+
48
+ export function createSupportsColor(stream: WriteStream, options?: Options): ColorInfo;
49
+
50
+ declare const supportsColor: {
51
+ stdout: ColorInfo;
52
+ stderr: ColorInfo;
53
+ };
54
+
55
+ export default supportsColor;
package/index.js CHANGED
@@ -1,19 +1,31 @@
1
- import os from 'os';
2
- import tty from 'tty';
3
- import hasFlag from 'has-flag';
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 = process.argv) {
7
+ const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
8
+ const position = argv.indexOf(prefix + flag);
9
+ const terminatorPosition = argv.indexOf('--');
10
+ return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
11
+ }
4
12
 
5
13
  const {env} = process;
6
14
 
7
15
  let flagForceColor;
8
- if (hasFlag('no-color') ||
9
- hasFlag('no-colors') ||
10
- hasFlag('color=false') ||
11
- hasFlag('color=never')) {
16
+ if (
17
+ hasFlag('no-color')
18
+ || hasFlag('no-colors')
19
+ || hasFlag('color=false')
20
+ || hasFlag('color=never')
21
+ ) {
12
22
  flagForceColor = 0;
13
- } else if (hasFlag('color') ||
14
- hasFlag('colors') ||
15
- hasFlag('color=true') ||
16
- hasFlag('color=always')) {
23
+ } else if (
24
+ hasFlag('color')
25
+ || hasFlag('colors')
26
+ || hasFlag('color=true')
27
+ || hasFlag('color=always')
28
+ ) {
17
29
  flagForceColor = 1;
18
30
  }
19
31
 
@@ -40,7 +52,7 @@ function translateLevel(level) {
40
52
  level,
41
53
  hasBasic: true,
42
54
  has256: level >= 2,
43
- has16m: level >= 3
55
+ has16m: level >= 3,
44
56
  };
45
57
  }
46
58
 
@@ -57,9 +69,9 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
57
69
  }
58
70
 
59
71
  if (sniffFlags) {
60
- if (hasFlag('color=16m') ||
61
- hasFlag('color=full') ||
62
- hasFlag('color=truecolor')) {
72
+ if (hasFlag('color=16m')
73
+ || hasFlag('color=full')
74
+ || hasFlag('color=truecolor')) {
63
75
  return 3;
64
76
  }
65
77
 
@@ -83,10 +95,10 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
83
95
  // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
84
96
  const osRelease = os.release().split('.');
85
97
  if (
86
- Number(osRelease[0]) >= 10 &&
87
- Number(osRelease[2]) >= 10586
98
+ Number(osRelease[0]) >= 10
99
+ && Number(osRelease[2]) >= 10_586
88
100
  ) {
89
- return Number(osRelease[2]) >= 14931 ? 3 : 2;
101
+ return Number(osRelease[2]) >= 14_931 ? 3 : 2;
90
102
  }
91
103
 
92
104
  return 1;
@@ -104,6 +116,11 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
104
116
  return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
105
117
  }
106
118
 
119
+ // Check for Azure DevOps pipelines
120
+ if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
121
+ return 1;
122
+ }
123
+
107
124
  if (env.COLORTERM === 'truecolor') {
108
125
  return 3;
109
126
  }
@@ -138,7 +155,7 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
138
155
  export function createSupportsColor(stream, options = {}) {
139
156
  const level = _supportsColor(stream, {
140
157
  streamIsTTY: stream && stream.isTTY,
141
- ...options
158
+ ...options,
142
159
  });
143
160
 
144
161
  return translateLevel(level);
@@ -146,7 +163,7 @@ export function createSupportsColor(stream, options = {}) {
146
163
 
147
164
  const supportsColor = {
148
165
  stdout: createSupportsColor({isTTY: tty.isatty(1)}),
149
- stderr: createSupportsColor({isTTY: tty.isatty(2)})
166
+ stderr: createSupportsColor({isTTY: tty.isatty(2)}),
150
167
  };
151
168
 
152
169
  export default supportsColor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supports-color",
3
- "version": "9.0.0",
3
+ "version": "9.2.0",
4
4
  "description": "Detect whether a terminal supports color",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/supports-color",
@@ -19,11 +19,12 @@
19
19
  "node": ">=12"
20
20
  },
21
21
  "scripts": {
22
- "//test": "xo && ava",
23
- "test": "xo"
22
+ "//test": "xo && ava && tsd",
23
+ "test": "xo && tsd"
24
24
  },
25
25
  "files": [
26
26
  "index.js",
27
+ "index.d.ts",
27
28
  "browser.js"
28
29
  ],
29
30
  "keywords": [
@@ -48,12 +49,12 @@
48
49
  "truecolor",
49
50
  "16m"
50
51
  ],
51
- "dependencies": {
52
- "has-flag": "^5.0.0"
53
- },
54
52
  "devDependencies": {
53
+ "@types/node": "^16.11.7",
55
54
  "ava": "^3.15.0",
56
55
  "import-fresh": "^3.3.0",
57
- "xo": "^0.38.2"
56
+ "tsd": "^0.18.0",
57
+ "typescript": "^4.4.3",
58
+ "xo": "^0.44.0"
58
59
  }
59
60
  }