|
| 1 | +// MIT License |
| 2 | + |
| 3 | +// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) |
| 4 | + |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +'use strict'; |
| 24 | + |
| 25 | +const { release } = require('os'); |
| 26 | + |
| 27 | +const OSRelease = release().split('.'); |
| 28 | + |
| 29 | +const COLORS_2 = 1; |
| 30 | +const COLORS_16 = 4; |
| 31 | +const COLORS_256 = 8; |
| 32 | +const COLORS_16m = 24; |
| 33 | + |
| 34 | +// The `getColorDepth` API got inspired by multiple sources such as |
| 35 | +// https://github.com/chalk/supports-color, |
| 36 | +// https://github.com/isaacs/color-support. |
| 37 | +function getColorDepth(env = process.env) { |
| 38 | + if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb' && !env.COLORTERM) { |
| 39 | + return COLORS_2; |
| 40 | + } |
| 41 | + |
| 42 | + if (process.platform === 'win32') { |
| 43 | + // Windows 10 build 10586 is the first Windows release that supports 256 |
| 44 | + // colors. Windows 10 build 14931 is the first release that supports |
| 45 | + // 16m/TrueColor. |
| 46 | + if (+OSRelease[0] >= 10) { |
| 47 | + const build = +OSRelease[2]; |
| 48 | + if (build >= 14931) |
| 49 | + return COLORS_16m; |
| 50 | + if (build >= 10586) |
| 51 | + return COLORS_256; |
| 52 | + } |
| 53 | + |
| 54 | + return COLORS_16; |
| 55 | + } |
| 56 | + |
| 57 | + if (env.TMUX) { |
| 58 | + return COLORS_256; |
| 59 | + } |
| 60 | + |
| 61 | + if (env.CI) { |
| 62 | + if ('TRAVIS' in env || 'CIRCLECI' in env || 'APPVEYOR' in env || |
| 63 | + 'GITLAB_CI' in env || env.CI_NAME === 'codeship') { |
| 64 | + return COLORS_256; |
| 65 | + } |
| 66 | + return COLORS_2; |
| 67 | + } |
| 68 | + |
| 69 | + if ('TEAMCITY_VERSION' in env) { |
| 70 | + return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? |
| 71 | + COLORS_16 : COLORS_2; |
| 72 | + } |
| 73 | + |
| 74 | + switch (env.TERM_PROGRAM) { |
| 75 | + case 'iTerm.app': |
| 76 | + if (!env.TERM_PROGRAM_VERSION || |
| 77 | + /^[0-2]\./.test(env.TERM_PROGRAM_VERSION)) { |
| 78 | + return COLORS_256; |
| 79 | + } |
| 80 | + return COLORS_16m; |
| 81 | + case 'HyperTerm': |
| 82 | + case 'Hyper': |
| 83 | + case 'MacTerm': |
| 84 | + return COLORS_16m; |
| 85 | + case 'Apple_Terminal': |
| 86 | + return COLORS_256; |
| 87 | + } |
| 88 | + |
| 89 | + if (env.TERM) { |
| 90 | + if (/^xterm-256/.test(env.TERM)) |
| 91 | + return COLORS_256; |
| 92 | + if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env.TERM)) |
| 93 | + return COLORS_16; |
| 94 | + } |
| 95 | + |
| 96 | + if (env.COLORTERM) |
| 97 | + return COLORS_16; |
| 98 | + |
| 99 | + return COLORS_2; |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = { |
| 103 | + getColorDepth |
| 104 | +}; |
0 commit comments