index.js 2.9 KB

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