nodeConsole.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 || 40;
  36. const args = truncateArgs(currentStatusMessage, l - 1);
  37. const str = args.join(" ");
  38. const coloredStr = `\u001b[1m${str}\u001b[39m\u001b[22m`;
  39. stream.write(`\x1b[2K\r${coloredStr}`);
  40. hasStatusMessage = true;
  41. };
  42. const writeColored = (prefix, colorPrefix, colorSuffix) => {
  43. return (...args) => {
  44. if (currentCollapsed > 0) return;
  45. clearStatusMessage();
  46. const str = indent(
  47. util.format(...args),
  48. prefix,
  49. colorPrefix,
  50. colorSuffix
  51. );
  52. stream.write(str + "\n");
  53. writeStatusMessage();
  54. };
  55. };
  56. const writeGroupMessage = writeColored(
  57. "<-> ",
  58. "\u001b[1m\u001b[36m",
  59. "\u001b[39m\u001b[22m"
  60. );
  61. const writeGroupCollapsedMessage = writeColored(
  62. "<+> ",
  63. "\u001b[1m\u001b[36m",
  64. "\u001b[39m\u001b[22m"
  65. );
  66. return {
  67. log: writeColored(" ", "\u001b[1m", "\u001b[22m"),
  68. debug: writeColored(" ", "", ""),
  69. trace: writeColored(" ", "", ""),
  70. info: writeColored("<i> ", "\u001b[1m\u001b[32m", "\u001b[39m\u001b[22m"),
  71. warn: writeColored("<w> ", "\u001b[1m\u001b[33m", "\u001b[39m\u001b[22m"),
  72. error: writeColored("<e> ", "\u001b[1m\u001b[31m", "\u001b[39m\u001b[22m"),
  73. logTime: writeColored(
  74. "<t> ",
  75. "\u001b[1m\u001b[35m",
  76. "\u001b[39m\u001b[22m"
  77. ),
  78. group: (...args) => {
  79. writeGroupMessage(...args);
  80. if (currentCollapsed > 0) {
  81. currentCollapsed++;
  82. } else {
  83. currentIndent += " ";
  84. }
  85. },
  86. groupCollapsed: (...args) => {
  87. writeGroupCollapsedMessage(...args);
  88. currentCollapsed++;
  89. },
  90. groupEnd: () => {
  91. if (currentCollapsed > 0) currentCollapsed--;
  92. else if (currentIndent.length >= 2)
  93. currentIndent = currentIndent.slice(0, currentIndent.length - 2);
  94. },
  95. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  96. profile: console.profile && (name => console.profile(name)),
  97. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  98. profileEnd: console.profileEnd && (name => console.profileEnd(name)),
  99. clear:
  100. !appendOnly &&
  101. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  102. console.clear &&
  103. (() => {
  104. clearStatusMessage();
  105. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  106. console.clear();
  107. writeStatusMessage();
  108. }),
  109. status: appendOnly
  110. ? writeColored("<s> ", "", "")
  111. : (name, ...args) => {
  112. args = args.filter(Boolean);
  113. if (name === undefined && args.length === 0) {
  114. clearStatusMessage();
  115. currentStatusMessage = undefined;
  116. } else if (
  117. typeof name === "string" &&
  118. name.startsWith("[webpack.Progress] ")
  119. ) {
  120. currentStatusMessage = [name.slice(19), ...args];
  121. writeStatusMessage();
  122. } else if (name === "[webpack.Progress]") {
  123. currentStatusMessage = [...args];
  124. writeStatusMessage();
  125. } else {
  126. currentStatusMessage = [name, ...args];
  127. writeStatusMessage();
  128. }
  129. }
  130. };
  131. };