index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import * as tty from "tty"
  2. const env = process.env || {}
  3. const argv = process.argv || []
  4. const isDisabled = "NO_COLOR" in env || argv.includes("--no-color")
  5. const isForced = "FORCE_COLOR" in env || argv.includes("--color")
  6. const isWindows = process.platform === "win32"
  7. const isCompatibleTerminal =
  8. tty && tty.isatty && tty.isatty(1) && env.TERM && env.TERM !== "dumb"
  9. const isCI =
  10. "CI" in env &&
  11. ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env)
  12. export const isColorSupported =
  13. !isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI)
  14. const replaceClose = (
  15. index,
  16. string,
  17. close,
  18. replace,
  19. head = string.substring(0, index) + replace,
  20. tail = string.substring(index + close.length),
  21. next = tail.indexOf(close)
  22. ) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace))
  23. const clearBleed = (index, string, open, close, replace) =>
  24. index < 0
  25. ? open + string + close
  26. : open + replaceClose(index, string, close, replace) + close
  27. const filterEmpty =
  28. (open, close, replace = open, at = open.length + 1) =>
  29. (string) =>
  30. string || !(string === "" || string === undefined)
  31. ? clearBleed(
  32. ("" + string).indexOf(close, at),
  33. string,
  34. open,
  35. close,
  36. replace
  37. )
  38. : ""
  39. const init = (open, close, replace) =>
  40. filterEmpty(`\x1b[${open}m`, `\x1b[${close}m`, replace)
  41. const colors = {
  42. reset: init(0, 0),
  43. bold: init(1, 22, "\x1b[22m\x1b[1m"),
  44. dim: init(2, 22, "\x1b[22m\x1b[2m"),
  45. italic: init(3, 23),
  46. underline: init(4, 24),
  47. inverse: init(7, 27),
  48. hidden: init(8, 28),
  49. strikethrough: init(9, 29),
  50. black: init(30, 39),
  51. red: init(31, 39),
  52. green: init(32, 39),
  53. yellow: init(33, 39),
  54. blue: init(34, 39),
  55. magenta: init(35, 39),
  56. cyan: init(36, 39),
  57. white: init(37, 39),
  58. gray: init(90, 39),
  59. bgBlack: init(40, 49),
  60. bgRed: init(41, 49),
  61. bgGreen: init(42, 49),
  62. bgYellow: init(43, 49),
  63. bgBlue: init(44, 49),
  64. bgMagenta: init(45, 49),
  65. bgCyan: init(46, 49),
  66. bgWhite: init(47, 49),
  67. blackBright: init(90, 39),
  68. redBright: init(91, 39),
  69. greenBright: init(92, 39),
  70. yellowBright: init(93, 39),
  71. blueBright: init(94, 39),
  72. magentaBright: init(95, 39),
  73. cyanBright: init(96, 39),
  74. whiteBright: init(97, 39),
  75. bgBlackBright: init(100, 49),
  76. bgRedBright: init(101, 49),
  77. bgGreenBright: init(102, 49),
  78. bgYellowBright: init(103, 49),
  79. bgBlueBright: init(104, 49),
  80. bgMagentaBright: init(105, 49),
  81. bgCyanBright: init(106, 49),
  82. bgWhiteBright: init(107, 49),
  83. }
  84. const none = (any) => any
  85. export const createColors = ({ useColor = isColorSupported } = {}) =>
  86. useColor
  87. ? colors
  88. : Object.keys(colors).reduce(
  89. (colors, key) => ({ ...colors, [key]: none }),
  90. {}
  91. )
  92. export const {
  93. reset,
  94. bold,
  95. dim,
  96. italic,
  97. underline,
  98. inverse,
  99. hidden,
  100. strikethrough,
  101. black,
  102. red,
  103. green,
  104. yellow,
  105. blue,
  106. magenta,
  107. cyan,
  108. white,
  109. gray,
  110. bgBlack,
  111. bgRed,
  112. bgGreen,
  113. bgYellow,
  114. bgBlue,
  115. bgMagenta,
  116. bgCyan,
  117. bgWhite,
  118. blackBright,
  119. redBright,
  120. greenBright,
  121. yellowBright,
  122. blueBright,
  123. magentaBright,
  124. cyanBright,
  125. whiteBright,
  126. bgBlackBright,
  127. bgRedBright,
  128. bgGreenBright,
  129. bgYellowBright,
  130. bgBlueBright,
  131. bgMagentaBright,
  132. bgCyanBright,
  133. bgWhiteBright,
  134. } = createColors()