supports-color 8.1.0 → 9.0.2

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.
Files changed (4) hide show
  1. package/browser.js +5 -12
  2. package/index.js +28 -21
  3. package/package.json +14 -13
  4. package/readme.md +18 -6
package/browser.js CHANGED
@@ -1,24 +1,17 @@
1
1
  /* eslint-env browser */
2
- 'use strict';
3
2
 
4
- function getChromeVersion() {
5
- const matches = /(Chrome|Chromium)\/(?<chromeVersion>\d+)\./.exec(navigator.userAgent);
3
+ const isBlinkBasedBrowser = /\b(Chrome|Chromium)\//.test(navigator.userAgent);
6
4
 
7
- if (!matches) {
8
- return;
9
- }
10
-
11
- return Number.parseInt(matches.groups.chromeVersion, 10);
12
- }
13
-
14
- const colorSupport = getChromeVersion() >= 69 ? {
5
+ const colorSupport = isBlinkBasedBrowser ? {
15
6
  level: 1,
16
7
  hasBasic: true,
17
8
  has256: false,
18
9
  has16m: false
19
10
  } : false;
20
11
 
21
- module.exports = {
12
+ const supportsColor = {
22
13
  stdout: colorSupport,
23
14
  stderr: colorSupport
24
15
  };
16
+
17
+ export default supportsColor;
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
- 'use strict';
2
- const os = require('os');
3
- const tty = require('tty');
4
- const hasFlag = require('has-flag');
1
+ import process from 'process'; // eslint-disable-line node/prefer-global/process
2
+ import os from 'os';
3
+ import tty from 'tty';
4
+ import hasFlag from 'has-flag';
5
5
 
6
6
  const {env} = process;
7
7
 
@@ -18,17 +18,18 @@ if (hasFlag('no-color') ||
18
18
  flagForceColor = 1;
19
19
  }
20
20
 
21
- let noFlagForceColor;
22
- if ('FORCE_COLOR' in env) {
23
- if (env.FORCE_COLOR === 'true') {
24
- noFlagForceColor = 1;
25
- } else if (env.FORCE_COLOR === 'false') {
26
- noFlagForceColor = 0;
27
- } else {
28
- noFlagForceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
29
- }
21
+ function envForceColor() {
22
+ if ('FORCE_COLOR' in env) {
23
+ if (env.FORCE_COLOR === 'true') {
24
+ return 1;
25
+ }
26
+
27
+ if (env.FORCE_COLOR === 'false') {
28
+ return 0;
29
+ }
30
30
 
31
- flagForceColor = noFlagForceColor;
31
+ return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
32
+ }
32
33
  }
33
34
 
34
35
  function translateLevel(level) {
@@ -44,7 +45,12 @@ function translateLevel(level) {
44
45
  };
45
46
  }
46
47
 
47
- function supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
48
+ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
49
+ const noFlagForceColor = envForceColor();
50
+ if (noFlagForceColor !== undefined) {
51
+ flagForceColor = noFlagForceColor;
52
+ }
53
+
48
54
  const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
49
55
 
50
56
  if (forceColor === 0) {
@@ -130,8 +136,8 @@ function supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
130
136
  return min;
131
137
  }
132
138
 
133
- function getSupportLevel(stream, options = {}) {
134
- const level = supportsColor(stream, {
139
+ export function createSupportsColor(stream, options = {}) {
140
+ const level = _supportsColor(stream, {
135
141
  streamIsTTY: stream && stream.isTTY,
136
142
  ...options
137
143
  });
@@ -139,8 +145,9 @@ function getSupportLevel(stream, options = {}) {
139
145
  return translateLevel(level);
140
146
  }
141
147
 
142
- module.exports = {
143
- supportsColor: getSupportLevel,
144
- stdout: getSupportLevel({isTTY: tty.isatty(1)}),
145
- stderr: getSupportLevel({isTTY: tty.isatty(2)})
148
+ const supportsColor = {
149
+ stdout: createSupportsColor({isTTY: tty.isatty(1)}),
150
+ stderr: createSupportsColor({isTTY: tty.isatty(2)})
146
151
  };
152
+
153
+ export default supportsColor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supports-color",
3
- "version": "8.1.0",
3
+ "version": "9.0.2",
4
4
  "description": "Detect whether a terminal supports color",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/supports-color",
@@ -10,20 +10,22 @@
10
10
  "email": "sindresorhus@gmail.com",
11
11
  "url": "https://sindresorhus.com"
12
12
  },
13
+ "type": "module",
14
+ "exports": {
15
+ "node": "./index.js",
16
+ "default": "./browser.js"
17
+ },
13
18
  "engines": {
14
- "node": ">=10"
19
+ "node": ">=12"
15
20
  },
16
21
  "scripts": {
17
- "test": "xo && ava"
22
+ "//test": "xo && ava",
23
+ "test": "xo"
18
24
  },
19
25
  "files": [
20
26
  "index.js",
21
27
  "browser.js"
22
28
  ],
23
- "exports": {
24
- "node": "./index.js",
25
- "default": "./browser.js"
26
- },
27
29
  "keywords": [
28
30
  "color",
29
31
  "colour",
@@ -47,12 +49,11 @@
47
49
  "16m"
48
50
  ],
49
51
  "dependencies": {
50
- "has-flag": "^4.0.0"
52
+ "has-flag": "^5.0.0"
51
53
  },
52
54
  "devDependencies": {
53
- "ava": "^2.4.0",
54
- "import-fresh": "^3.2.2",
55
- "xo": "^0.35.0"
56
- },
57
- "browser": "browser.js"
55
+ "ava": "^3.15.0",
56
+ "import-fresh": "^3.3.0",
57
+ "xo": "^0.38.2"
58
+ }
58
59
  }
package/readme.md CHANGED
@@ -11,7 +11,7 @@ $ npm install supports-color
11
11
  ## Usage
12
12
 
13
13
  ```js
14
- const supportsColor = require('supports-color');
14
+ import supportsColor from 'supports-color';
15
15
 
16
16
  if (supportsColor.stdout) {
17
17
  console.log('Terminal stdout supports color');
@@ -28,7 +28,7 @@ if (supportsColor.stderr.has16m) {
28
28
 
29
29
  ## API
30
30
 
31
- Returns an `Object` with a `stdout` and `stderr` property for testing either streams. Each property is an `Object`, or `false` if color is not supported.
31
+ Returns an `object` with a `stdout` and `stderr` property for testing either streams. Each property is an `Object`, or `false` if color is not supported.
32
32
 
33
33
  The `stdout`/`stderr` objects specifies a level of support for color through a `.level` property and a corresponding flag:
34
34
 
@@ -36,13 +36,23 @@ The `stdout`/`stderr` objects specifies a level of support for color through a `
36
36
  - `.level = 2` and `.has256 = true`: 256 color support
37
37
  - `.level = 3` and `.has16m = true`: Truecolor support (16 million colors)
38
38
 
39
- ### `require('supports-color').supportsColor(stream, options?)`
39
+ ### Custom instance
40
40
 
41
- Additionally, `supports-color` exposes the `.supportsColor()` function that takes an arbitrary write stream (e.g. `process.stdout`) and an optional options object to (re-)evaluate color support for an arbitrary stream.
41
+ The package also exposes the named export `createSupportColor` function that takes an arbitrary write stream (for example, `process.stdout`) and an optional options object to (re-)evaluate color support for an arbitrary stream.
42
42
 
43
- For example, `require('supports-color').stdout` is the equivalent of `require('supports-color').supportsColor(process.stdout)`.
43
+ ```js
44
+ import {createSupportsColor} from 'supports-color';
45
+
46
+ const stdoutSupportsColor = createSupportsColor(process.stdout);
47
+
48
+ if (stdoutSupportsColor) {
49
+ console.log('Terminal stdout supports color');
50
+ }
51
+
52
+ // `stdoutSupportsColor` is the same as `supportsColor.stdout`
53
+ ```
44
54
 
45
- The options object supports a single boolean property `sniffFlags`. By default it is `true`, which instructs `supportsColor()` to sniff `process.argv` for the multitude of `--color` flags (see _Info_ below). If `false`, then `process.argv` is not considered when determining color support.
55
+ The options object supports a single boolean property `sniffFlags`. By default it is `true`, which instructs the detection to sniff `process.argv` for the multitude of `--color` flags (see _Info_ below). If `false`, then `process.argv` is not considered when determining color support.
46
56
 
47
57
  ## Info
48
58
 
@@ -56,6 +66,8 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
56
66
 
57
67
  - [supports-color-cli](https://github.com/chalk/supports-color-cli) - CLI for this module
58
68
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
69
+ - [is-unicode-supported](https://github.com/sindresorhus/is-unicode-supported) - Detect whether the terminal supports Unicode
70
+ - [is-interactive](https://github.com/sindresorhus/is-interactive) - Check if stdout or stderr is interactive
59
71
 
60
72
  ## Maintainers
61
73