index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict';
  2. const os = require('os');
  3. const tty = require('tty');
  4. const hasFlag = require('has-flag');
  5. const {env} = process;
  6. let forceColor;
  7. if (hasFlag('no-color') ||
  8. hasFlag('no-colors') ||
  9. hasFlag('color=false') ||
  10. hasFlag('color=never')) {
  11. forceColor = 0;
  12. } else if (hasFlag('color') ||
  13. hasFlag('colors') ||
  14. hasFlag('color=true') ||
  15. hasFlag('color=always')) {
  16. forceColor = 1;
  17. }
  18. if ('FORCE_COLOR' in env) {
  19. if (env.FORCE_COLOR === 'true') {
  20. forceColor = 1;
  21. } else if (env.FORCE_COLOR === 'false') {
  22. forceColor = 0;
  23. } else {
  24. forceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(parseInt(env.FORCE_COLOR, 10), 3);
  25. }
  26. }
  27. function translateLevel(level) {
  28. if (level === 0) {
  29. return false;
  30. }
  31. return {
  32. level,
  33. hasBasic: true,
  34. has256: level >= 2,
  35. has16m: level >= 3
  36. };
  37. }
  38. function supportsColor(haveStream, streamIsTTY) {
  39. if (forceColor === 0) {
  40. return 0;
  41. }
  42. if (hasFlag('color=16m') ||
  43. hasFlag('color=full') ||
  44. hasFlag('color=truecolor')) {
  45. return 3;
  46. }
  47. if (hasFlag('color=256')) {
  48. return 2;
  49. }
  50. if (haveStream && !streamIsTTY && forceColor === undefined) {
  51. return 0;
  52. }
  53. const min = forceColor || 0;
  54. if (env.TERM === 'dumb') {
  55. return min;
  56. }
  57. if (process.platform === 'win32') {
  58. // Windows 10 build 10586 is the first Windows release that supports 256 colors.
  59. // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
  60. const osRelease = os.release().split('.');
  61. if (
  62. Number(osRelease[0]) >= 10 &&
  63. Number(osRelease[2]) >= 10586
  64. ) {
  65. return Number(osRelease[2]) >= 14931 ? 3 : 2;
  66. }
  67. return 1;
  68. }
  69. if ('CI' in env) {
  70. if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
  71. return 1;
  72. }
  73. return min;
  74. }
  75. if ('TEAMCITY_VERSION' in env) {
  76. return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
  77. }
  78. if (env.COLORTERM === 'truecolor') {
  79. return 3;
  80. }
  81. if ('TERM_PROGRAM' in env) {
  82. const version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
  83. switch (env.TERM_PROGRAM) {
  84. case 'iTerm.app':
  85. return version >= 3 ? 3 : 2;
  86. case 'Apple_Terminal':
  87. return 2;
  88. // No default
  89. }
  90. }
  91. if (/-256(color)?$/i.test(env.TERM)) {
  92. return 2;
  93. }
  94. if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
  95. return 1;
  96. }
  97. if ('COLORTERM' in env) {
  98. return 1;
  99. }
  100. return min;
  101. }
  102. function getSupportLevel(stream) {
  103. const level = supportsColor(stream, stream && stream.isTTY);
  104. return translateLevel(level);
  105. }
  106. module.exports = {
  107. supportsColor: getSupportLevel,
  108. stdout: translateLevel(supportsColor(true, tty.isatty(1))),
  109. stderr: translateLevel(supportsColor(true, tty.isatty(2)))
  110. };