2 const os = require('os');
3 const hasFlag = require('has-flag');
5 const env = process.env;
8 if (hasFlag('no-color') ||
9 hasFlag('no-colors') ||
10 hasFlag('color=false')) {
12 } else if (hasFlag('color') ||
14 hasFlag('color=true') ||
15 hasFlag('color=always')) {
18 if ('FORCE_COLOR' in env) {
19 forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0;
22 function translateLevel(level) {
35 function supportsColor(stream) {
36 if (forceColor === false) {
40 if (hasFlag('color=16m') ||
41 hasFlag('color=full') ||
42 hasFlag('color=truecolor')) {
46 if (hasFlag('color=256')) {
50 if (stream && !stream.isTTY && forceColor !== true) {
54 const min = forceColor ? 1 : 0;
56 if (process.platform === 'win32') {
57 // Node.js 7.5.0 is the first version of Node.js to include a patch to
58 // libuv that enables 256 color output on Windows. Anything earlier and it
59 // won't work. However, here we target Node.js 8 at minimum as it is an LTS
60 // release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
61 // release that supports 256 colors. Windows 10 build 14931 is the first release
62 // that supports 16m/TrueColor.
63 const osRelease = os.release().split('.');
65 Number(process.versions.node.split('.')[0]) >= 8 &&
66 Number(osRelease[0]) >= 10 &&
67 Number(osRelease[2]) >= 10586
69 return Number(osRelease[2]) >= 14931 ? 3 : 2;
76 if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
83 if ('TEAMCITY_VERSION' in env) {
84 return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
87 if (env.COLORTERM === 'truecolor') {
91 if ('TERM_PROGRAM' in env) {
92 const version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
94 switch (env.TERM_PROGRAM) {
96 return version >= 3 ? 3 : 2;
97 case 'Apple_Terminal':
103 if (/-256(color)?$/i.test(env.TERM)) {
107 if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
111 if ('COLORTERM' in env) {
115 if (env.TERM === 'dumb') {
122 function getSupportLevel(stream) {
123 const level = supportsColor(stream);
124 return translateLevel(level);
128 supportsColor: getSupportLevel,
129 stdout: getSupportLevel(process.stdout),
130 stderr: getSupportLevel(process.stderr)