index.js 2.7 KB

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