nodeConsole.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const truncateArgs = require("../logging/truncateArgs");
  8. module.exports = ({ colors, appendOnly, stream }) => {
  9. let currentStatusMessage = undefined;
  10. let hasStatusMessage = false;
  11. let currentIndent = "";
  12. let currentCollapsed = 0;
  13. const indent = (str, prefix, colorPrefix, colorSuffix) => {
  14. if (str === "") return str;
  15. prefix = currentIndent + prefix;
  16. if (colors) {
  17. return (
  18. prefix +
  19. colorPrefix +
  20. str.replace(/\n/g, colorSuffix + "\n" + prefix + colorPrefix) +
  21. colorSuffix
  22. );
  23. } else {
  24. return prefix + str.replace(/\n/g, "\n" + prefix);
  25. }
  26. };
  27. const clearStatusMessage = () => {
  28. if (hasStatusMessage) {
  29. stream.write("\x1b[2K\r");
  30. hasStatusMessage = false;
  31. }
  32. };
  33. const writeStatusMessage = () => {
  34. if (!currentStatusMessage) return;
  35. const l = stream.columns;
  36. const args = l
  37. ? truncateArgs(currentStatusMessage, l - 1)
  38. : currentStatusMessage;
  39. const str = args.join(" ");
  40. const coloredStr = `\u001b[1m${str}\u001b[39m\u001b[22m`;
  41. stream.write(`\x1b[2K\r${coloredStr}`);
  42. hasStatusMessage = true;
  43. };
  44. const writeColored = (prefix, colorPrefix, colorSuffix) => {
  45. return (...args) => {
  46. if (currentCollapsed > 0) return;
  47. clearStatusMessage();
  48. const str = indent(
  49. util.format(...args),
  50. prefix,
  51. colorPrefix,
  52. colorSuffix
  53. );
  54. stream.write(str + "\n");
  55. writeStatusMessage();
  56. };
  57. };
  58. const writeGroupMessage = writeColored(
  59. "<-> ",
  60. "\u001b[1m\u001b[36m",
  61. "\u001b[39m\u001b[22m"
  62. );
  63. const writeGroupCollapsedMessage = writeColored(
  64. "<+> ",
  65. "\u001b[1m\u001b[36m",
  66. "\u001b[39m\u001b[22m"
  67. );
  68. return {
  69. log: writeColored(" ", "\u001b[1m", "\u001b[22m"),
  70. debug: writeColored(" ", "", ""),
  71. trace: writeColored(" ", "", ""),
  72. info: writeColored("<i> ", "\u001b[1m\u001b[32m", "\u001b[39m\u001b[22m"),
  73. warn: writeColored("<w> ", "\u001b[1m\u001b[33m", "\u001b[39m\u001b[22m"),
  74. error: writeColored("<e> ", "\u001b[1m\u001b[31m", "\u001b[39m\u001b[22m"),
  75. logTime: writeColored(
  76. "<t> ",
  77. "\u001b[1m\u001b[35m",
  78. "\u001b[39m\u001b[22m"
  79. ),
  80. group: (...args) => {
  81. writeGroupMessage(...args);
  82. if (currentCollapsed > 0) {
  83. currentCollapsed++;
  84. } else {
  85. currentIndent += " ";
  86. }
  87. },
  88. groupCollapsed: (...args) => {
  89. writeGroupCollapsedMessage(...args);
  90. currentCollapsed++;
  91. },
  92. groupEnd: () => {
  93. if (currentCollapsed > 0) currentCollapsed--;
  94. else if (currentIndent.length >= 2)
  95. currentIndent = currentIndent.slice(0, currentIndent.length - 2);
  96. },
  97. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  98. profile: console.profile && (name => console.profile(name)),
  99. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  100. profileEnd: console.profileEnd && (name => console.profileEnd(name)),
  101. clear:
  102. !appendOnly &&
  103. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  104. console.clear &&
  105. (() => {
  106. clearStatusMessage();
  107. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  108. console.clear();
  109. writeStatusMessage();
  110. }),
  111. status: appendOnly
  112. ? writeColored("<s> ", "", "")
  113. : (name, ...args) => {
  114. args = args.filter(Boolean);
  115. if (name === undefined && args.length === 0) {
  116. clearStatusMessage();
  117. currentStatusMessage = undefined;
  118. } else if (
  119. typeof name === "string" &&
  120. name.startsWith("[webpack.Progress] ")
  121. ) {
  122. currentStatusMessage = [name.slice(19), ...args];
  123. writeStatusMessage();
  124. } else if (name === "[webpack.Progress]") {
  125. currentStatusMessage = [...args];
  126. writeStatusMessage();
  127. } else {
  128. currentStatusMessage = [name, ...args];
  129. writeStatusMessage();
  130. }
  131. }
  132. };
  133. };