1 | /*
|
---|
2 | The MIT License (MIT)
|
---|
3 |
|
---|
4 | Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
---|
5 |
|
---|
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
7 | of this software and associated documentation files (the "Software"), to deal
|
---|
8 | in the Software without restriction, including without limitation the rights
|
---|
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
10 | copies of the Software, and to permit persons to whom the Software is
|
---|
11 | furnished to do so, subject to the following conditions:
|
---|
12 |
|
---|
13 | The above copyright notice and this permission notice shall be included in
|
---|
14 | all copies or substantial portions of the Software.
|
---|
15 |
|
---|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
22 | THE SOFTWARE.
|
---|
23 |
|
---|
24 | */
|
---|
25 |
|
---|
26 | 'use strict';
|
---|
27 |
|
---|
28 | var os = require('os');
|
---|
29 | var hasFlag = require('./has-flag.js');
|
---|
30 |
|
---|
31 | var env = process.env;
|
---|
32 |
|
---|
33 | var forceColor = void 0;
|
---|
34 | if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false')) {
|
---|
35 | forceColor = false;
|
---|
36 | } else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true')
|
---|
37 | || hasFlag('color=always')) {
|
---|
38 | forceColor = true;
|
---|
39 | }
|
---|
40 | if ('FORCE_COLOR' in env) {
|
---|
41 | forceColor = env.FORCE_COLOR.length === 0
|
---|
42 | || parseInt(env.FORCE_COLOR, 10) !== 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | function translateLevel(level) {
|
---|
46 | if (level === 0) {
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | return {
|
---|
51 | level: level,
|
---|
52 | hasBasic: true,
|
---|
53 | has256: level >= 2,
|
---|
54 | has16m: level >= 3,
|
---|
55 | };
|
---|
56 | }
|
---|
57 |
|
---|
58 | function supportsColor(stream) {
|
---|
59 | if (forceColor === false) {
|
---|
60 | return 0;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (hasFlag('color=16m') || hasFlag('color=full')
|
---|
64 | || hasFlag('color=truecolor')) {
|
---|
65 | return 3;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (hasFlag('color=256')) {
|
---|
69 | return 2;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (stream && !stream.isTTY && forceColor !== true) {
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | var min = forceColor ? 1 : 0;
|
---|
77 |
|
---|
78 | if (process.platform === 'win32') {
|
---|
79 | // Node.js 7.5.0 is the first version of Node.js to include a patch to
|
---|
80 | // libuv that enables 256 color output on Windows. Anything earlier and it
|
---|
81 | // won't work. However, here we target Node.js 8 at minimum as it is an LTS
|
---|
82 | // release, and Node.js 7 is not. Windows 10 build 10586 is the first
|
---|
83 | // Windows release that supports 256 colors. Windows 10 build 14931 is the
|
---|
84 | // first release that supports 16m/TrueColor.
|
---|
85 | var osRelease = os.release().split('.');
|
---|
86 | if (Number(process.versions.node.split('.')[0]) >= 8
|
---|
87 | && Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
|
---|
88 | return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
---|
89 | }
|
---|
90 |
|
---|
91 | return 1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if ('CI' in env) {
|
---|
95 | if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(function(sign) {
|
---|
96 | return sign in env;
|
---|
97 | }) || env.CI_NAME === 'codeship') {
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return min;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if ('TEAMCITY_VERSION' in env) {
|
---|
105 | return (/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0
|
---|
106 | );
|
---|
107 | }
|
---|
108 |
|
---|
109 | if ('TERM_PROGRAM' in env) {
|
---|
110 | var version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
---|
111 |
|
---|
112 | switch (env.TERM_PROGRAM) {
|
---|
113 | case 'iTerm.app':
|
---|
114 | return version >= 3 ? 3 : 2;
|
---|
115 | case 'Hyper':
|
---|
116 | return 3;
|
---|
117 | case 'Apple_Terminal':
|
---|
118 | return 2;
|
---|
119 | // No default
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (/-256(color)?$/i.test(env.TERM)) {
|
---|
124 | return 2;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
---|
128 | return 1;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if ('COLORTERM' in env) {
|
---|
132 | return 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (env.TERM === 'dumb') {
|
---|
136 | return min;
|
---|
137 | }
|
---|
138 |
|
---|
139 | return min;
|
---|
140 | }
|
---|
141 |
|
---|
142 | function getSupportLevel(stream) {
|
---|
143 | var level = supportsColor(stream);
|
---|
144 | return translateLevel(level);
|
---|
145 | }
|
---|
146 |
|
---|
147 | module.exports = {
|
---|
148 | supportsColor: getSupportLevel,
|
---|
149 | stdout: getSupportLevel(process.stdout),
|
---|
150 | stderr: getSupportLevel(process.stderr),
|
---|
151 | };
|
---|