index.js 2.2 KB

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