Skip to content

Commit 656d406

Browse files
committed
Fork an inline a transpiled and unpolyfilled version of supports-color
1 parent a1f42a6 commit 656d406

File tree

3 files changed

+140
-3
lines changed

3 files changed

+140
-3
lines changed

lib/MagicPen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function MagicPen(options) {
4343
}
4444
}
4545

46-
if (typeof exports === 'object' && typeof exports.nodeName !== 'string' && require('supports-color').stdout) {
46+
if (typeof exports === 'object' && typeof exports.nodeName !== 'string' && require('./supportsColor').stdout) {
4747
MagicPen.defaultFormat = 'ansi'; // colored console
4848
} else if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
4949
if (window._phantom || window.mochaPhantomJS || (window.__karma__ && window.__karma__.config.captureConsole)) {

lib/supportsColor.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Fork from https://github.com/sindresorhus/has-flag
2+
var hasFlag = function hasFlag(flag, argv) {
3+
argv = argv || process.argv;
4+
var prefix = flag.indexOf('-') === 0 ? '' : flag.length === 1 ? '-' : '--';
5+
var pos = argv.indexOf(prefix + flag);
6+
var terminatorPos = argv.indexOf('--');
7+
return pos !== -1 && (terminatorPos === -1 || pos < terminatorPos);
8+
};
9+
10+
// Fork from https://github.com/chalk/supports-color
11+
var os = require('os');
12+
var env = process.env;
13+
14+
var forceColor = void 0;
15+
if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false') || hasFlag('color=never')) {
16+
forceColor = 0;
17+
} else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) {
18+
forceColor = 1;
19+
}
20+
21+
if ('FORCE_COLOR' in env) {
22+
if (env.FORCE_COLOR === 'true') {
23+
forceColor = 1;
24+
} else if (env.FORCE_COLOR === 'false') {
25+
forceColor = 0;
26+
} else {
27+
forceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(parseInt(env.FORCE_COLOR, 10), 3);
28+
}
29+
}
30+
31+
function translateLevel(level) {
32+
if (level === 0) {
33+
return false;
34+
}
35+
36+
return {
37+
level: level,
38+
hasBasic: true,
39+
has256: level >= 2,
40+
has16m: level >= 3
41+
};
42+
}
43+
44+
function supportsColor(stream) {
45+
if (forceColor === 0) {
46+
return 0;
47+
}
48+
49+
if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) {
50+
return 3;
51+
}
52+
53+
if (hasFlag('color=256')) {
54+
return 2;
55+
}
56+
57+
if (stream && !stream.isTTY && forceColor === undefined) {
58+
return 0;
59+
}
60+
61+
var min = forceColor || 0;
62+
63+
if (env.TERM === 'dumb') {
64+
return min;
65+
}
66+
67+
if (process.platform === 'win32') {
68+
// Node.js 7.5.0 is the first version of Node.js to include a patch to
69+
// libuv that enables 256 color output on Windows. Anything earlier and it
70+
// won't work. However, here we target Node.js 8 at minimum as it is an LTS
71+
// release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
72+
// release that supports 256 colors. Windows 10 build 14931 is the first release
73+
// that supports 16m/TrueColor.
74+
var osRelease = os.release().split('.');
75+
if (Number(process.versions.node.split('.')[0]) >= 8 && Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
76+
return Number(osRelease[2]) >= 14931 ? 3 : 2;
77+
}
78+
79+
return 1;
80+
}
81+
82+
if ('CI' in env) {
83+
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(function (sign) {
84+
return sign in env;
85+
}) || env.CI_NAME === 'codeship') {
86+
return 1;
87+
}
88+
89+
return min;
90+
}
91+
92+
if ('TEAMCITY_VERSION' in env) {
93+
return (/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0
94+
);
95+
}
96+
97+
if (env.COLORTERM === 'truecolor') {
98+
return 3;
99+
}
100+
101+
if ('TERM_PROGRAM' in env) {
102+
var version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
103+
104+
switch (env.TERM_PROGRAM) {
105+
case 'iTerm.app':
106+
return version >= 3 ? 3 : 2;
107+
case 'Apple_Terminal':
108+
return 2;
109+
// No default
110+
}
111+
}
112+
113+
if (/-256(color)?$/i.test(env.TERM)) {
114+
return 2;
115+
}
116+
117+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
118+
return 1;
119+
}
120+
121+
if ('COLORTERM' in env) {
122+
return 1;
123+
}
124+
125+
return min;
126+
}
127+
128+
function getSupportLevel(stream) {
129+
var level = supportsColor(stream);
130+
return translateLevel(level);
131+
}
132+
133+
module.exports = {
134+
supportsColor: getSupportLevel,
135+
stdout: getSupportLevel(process.stdout),
136+
stderr: getSupportLevel(process.stderr)
137+
};
138+

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
},
3333
"dependencies": {
3434
"ansi-styles": "2.0.0",
35-
"color-diff": "0.1.7",
36-
"supports-color": "6.1.0"
35+
"color-diff": "0.1.7"
3736
},
3837
"license": "MIT"
3938
}

0 commit comments

Comments
 (0)