update supports-color

This commit is contained in:
idanran 2022-12-07 20:32:31 +08:00
parent a027e3c1e0
commit 75ace79cc7
No known key found for this signature in database
GPG key ID: 6AAA420F30EBA960
3 changed files with 34 additions and 21 deletions

View file

@ -1,15 +1,26 @@
/* eslint-env browser */ /* eslint-env browser */
const isBlinkBasedBrowser = navigator.userAgentData const level = (() => {
? navigator.userAgentData.brands.some(({brand}) => brand === 'Chromium') if (navigator.userAgentData) {
: /\b(Chrome|Chromium)\//.test(navigator.userAgent); const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
if (brand?.version > 93) {
return 3;
}
}
const colorSupport = isBlinkBasedBrowser ? { if (/\b(Chrome|Chromium)\//.test(navigator.userAgent)) {
level: 1, return 1;
}
return 0;
})();
const colorSupport = level !== 0 && {
level,
hasBasic: true, hasBasic: true,
has256: false, has256: level >= 2,
has16m: false, has16m: level >= 3,
} : false; };
const supportsColor = { const supportsColor = {
stdout: colorSupport, stdout: colorSupport,

View file

@ -45,7 +45,7 @@ export interface ColorSupport {
export type ColorInfo = ColorSupport | false; export type ColorInfo = ColorSupport | false;
export function createSupportsColor(stream: WriteStream, options?: Options): ColorInfo; export function createSupportsColor(stream?: WriteStream, options?: Options): ColorInfo;
declare const supportsColor: { declare const supportsColor: {
stdout: ColorInfo; stdout: ColorInfo;

View file

@ -3,7 +3,7 @@ import os from 'node:os';
import tty from 'node:tty'; import tty from 'node:tty';
// From: https://github.com/sindresorhus/has-flag/blob/main/index.js // From: https://github.com/sindresorhus/has-flag/blob/main/index.js
function hasFlag(flag, argv = process.argv) { function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--'); const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
const position = argv.indexOf(prefix + flag); const position = argv.indexOf(prefix + flag);
const terminatorPosition = argv.indexOf('--'); const terminatorPosition = argv.indexOf('--');
@ -80,6 +80,12 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
} }
} }
// Check for Azure DevOps pipelines.
// Has to be above the `!streamIsTTY` check.
if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
return 1;
}
if (haveStream && !streamIsTTY && forceColor === undefined) { if (haveStream && !streamIsTTY && forceColor === undefined) {
return 0; return 0;
} }
@ -105,7 +111,11 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
} }
if ('CI' in env) { if ('CI' in env) {
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') { if ('GITHUB_ACTIONS' in env) {
return 3;
}
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
return 1; return 1;
} }
@ -116,11 +126,6 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0; return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
} }
// Check for Azure DevOps pipelines
if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
return 1;
}
if (env.COLORTERM === 'truecolor') { if (env.COLORTERM === 'truecolor') {
return 3; return 3;
} }
@ -129,13 +134,10 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10); const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
switch (env.TERM_PROGRAM) { switch (env.TERM_PROGRAM) {
case 'iTerm.app': { case 'iTerm.app':
return version >= 3 ? 3 : 2; return version >= 3 ? 3 : 2;
} case 'Apple_Terminal':
case 'Apple_Terminal': {
return 2; return 2;
}
// No default // No default
} }
} }