Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
Closes #151
  • Loading branch information
sindresorhus committed Feb 13, 2024
1 parent edc5d7d commit c214314
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 165 deletions.
1 change: 0 additions & 1 deletion .github/funding.yml
@@ -1,2 +1 @@
github: [sindresorhus, Qix-]
tidelift: npm/supports-color
7 changes: 3 additions & 4 deletions .github/workflows/main.yml
Expand Up @@ -10,12 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 20
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
8 changes: 6 additions & 2 deletions browser.js
@@ -1,14 +1,18 @@
/* eslint-env browser */

const level = (() => {
if (navigator.userAgentData) {
if (!('navigator' in globalThis)) {
return 0;
}

if (globalThis.navigator.userAgentData) {
const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
if (brand?.version > 93) {
return 3;
}
}

if (/\b(Chrome|Chromium)\//.test(navigator.userAgent)) {
if (/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)) {
return 1;
}

Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
@@ -1,13 +1,13 @@
import type {WriteStream} from 'node:tty';

export interface Options {
export type Options = {
/**
Whether `process.argv` should be sniffed for `--color` and `--no-color` flags.
@default true
*/
readonly sniffFlags?: boolean;
}
};

/**
Levels:
Expand All @@ -21,7 +21,7 @@ export type ColorSupportLevel = 0 | 1 | 2 | 3;
/**
Detect whether the terminal supports color.
*/
export interface ColorSupport {
export type ColorSupport = {
/**
The color level.
*/
Expand All @@ -41,7 +41,7 @@ export interface ColorSupport {
Whether Truecolor 16 million colors are supported.
*/
has16m: boolean;
}
};

export type ColorInfo = ColorSupport | false;

Expand Down
28 changes: 20 additions & 8 deletions index.js
Expand Up @@ -31,17 +31,29 @@ if (
}

function envForceColor() {
if ('FORCE_COLOR' in env) {
if (env.FORCE_COLOR === 'true') {
return 1;
}
if (!('FORCE_COLOR' in env)) {
return;
}

if (env.FORCE_COLOR === 'false') {
return 0;
}
if (env.FORCE_COLOR === 'true') {
return 1;
}

if (env.FORCE_COLOR === 'false') {
return 0;
}

return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
if (env.FORCE_COLOR.length === 0) {
return 1;
}

const level = Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);

if (![0, 1, 2, 3].includes(level)) {
return;
}

return level;
}

function translateLevel(level) {
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
@@ -1,6 +1,6 @@
import {stdout, stderr} from 'node:process';
import {expectType} from 'tsd';
import supportsColor, {createSupportsColor, Options, ColorInfo} from './index.js';
import supportsColor, {createSupportsColor, type Options, type ColorInfo} from './index.js';

const options: Options = {};

Expand Down
20 changes: 12 additions & 8 deletions package.json
Expand Up @@ -12,15 +12,16 @@
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"node": "./index.js",
"default": "./browser.js"
},
"sideEffects": false,
"engines": {
"node": ">=12"
"node": ">=18"
},
"scripts": {
"//test": "xo && ava && tsd",
"test": "tsd"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand Down Expand Up @@ -51,10 +52,13 @@
"16m"
],
"devDependencies": {
"@types/node": "^20.3.2",
"ava": "^5.3.1",
"import-fresh": "^3.3.0",
"tsd": "^0.18.0",
"xo": "^0.54.2"
"@types/node": "^20.11.17",
"ava": "^6.1.1",
"tsd": "^0.30.4",
"xo": "^0.57.0"
},
"ava": {
"serial": true,
"workerThreads": false
}
}
18 changes: 2 additions & 16 deletions readme.md
Expand Up @@ -4,8 +4,8 @@
## Install

```
$ npm install supports-color
```sh
npm install supports-color
```

## Usage
Expand Down Expand Up @@ -73,17 +73,3 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Josh Junon](https://github.com/qix-)

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-supports-color?utm_source=npm-supports-color&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>

---

0 comments on commit c214314

Please sign in to comment.