nodeConsole.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /** @typedef {import("../logging/createConsoleLogger").LoggerConsole} LoggerConsole */
  9. /**
  10. * @param {object} options options
  11. * @param {boolean=} options.colors colors
  12. * @param {boolean=} options.appendOnly append only
  13. * @param {NodeJS.WritableStream} options.stream stream
  14. * @returns {LoggerConsole} logger function
  15. */
  16. module.exports = ({ colors, appendOnly, stream }) => {
  17. /** @type {string[] | undefined} */
  18. let currentStatusMessage = undefined;
  19. let hasStatusMessage = false;
  20. let currentIndent = "";
  21. let currentCollapsed = 0;
  22. /**
  23. * @param {string} str string
  24. * @param {string} prefix prefix
  25. * @param {string} colorPrefix color prefix
  26. * @param {string} colorSuffix color suffix
  27. * @returns {string} indented string
  28. */
  29. const indent = (str, prefix, colorPrefix, colorSuffix) => {
  30. if (str === "") return str;
  31. prefix = currentIndent + prefix;
  32. if (colors) {
  33. return (
  34. prefix +
  35. colorPrefix +
  36. str.replace(/\n/g, colorSuffix + "\n" + prefix + colorPrefix) +
  37. colorSuffix
  38. );
  39. } else {
  40. return prefix + str.replace(/\n/g, "\n" + prefix);
  41. }
  42. };
  43. const clearStatusMessage = () => {
  44. if (hasStatusMessage) {
  45. stream.write("\x1b[2K\r");
  46. hasStatusMessage = false;
  47. }
  48. };
  49. const writeStatusMessage = () => {
  50. if (!currentStatusMessage) return;
  51. const l = /** @type {TODO} */ (stream).columns || 40;
  52. const args = truncateArgs(currentStatusMessage, l - 1);
  53. const str = args.join(" ");
  54. const coloredStr = `\u001b[1m${str}\u001b[39m\u001b[22m`;
  55. stream.write(`\x1b[2K\r${coloredStr}`);
  56. hasStatusMessage = true;
  57. };
  58. /**
  59. * @param {string} prefix prefix
  60. * @param {string} colorPrefix color prefix
  61. * @param {string} colorSuffix color suffix
  62. * @returns {(function(...any[]): void)} function to write with colors
  63. */
  64. const writeColored = (prefix, colorPrefix, colorSuffix) => {
  65. return (...args) => {
  66. if (currentCollapsed > 0) return;
  67. clearStatusMessage();
  68. const str = indent(
  69. util.format(...args),
  70. prefix,
  71. colorPrefix,
  72. colorSuffix
  73. );
  74. stream.write(str + "\n");
  75. writeStatusMessage();
  76. };
  77. };
  78. const writeGroupMessage = writeColored(
  79. "<-> ",
  80. "\u001b[1m\u001b[36m",
  81. "\u001b[39m\u001b[22m"
  82. );
  83. const writeGroupCollapsedMessage = writeColored(
  84. "<+> ",
  85. "\u001b[1m\u001b[36m",
  86. "\u001b[39m\u001b[22m"
  87. );
  88. return {
  89. log: writeColored(" ", "\u001b[1m", "\u001b[22m"),
  90. debug: writeColored(" ", "", ""),
  91. trace: writeColored(" ", "", ""),
  92. info: writeColored("<i> ", "\u001b[1m\u001b[32m", "\u001b[39m\u001b[22m"),
  93. warn: writeColored("<w> ", "\u001b[1m\u001b[33m", "\u001b[39m\u001b[22m"),
  94. error: writeColored("<e> ", "\u001b[1m\u001b[31m", "\u001b[39m\u001b[22m"),
  95. logTime: writeColored(
  96. "<t> ",
  97. "\u001b[1m\u001b[35m",
  98. "\u001b[39m\u001b[22m"
  99. ),
  100. group: (...args) => {
  101. writeGroupMessage(...args);
  102. if (currentCollapsed > 0) {
  103. currentCollapsed++;
  104. } else {
  105. currentIndent += " ";
  106. }
  107. },
  108. groupCollapsed: (...args) => {
  109. writeGroupCollapsedMessage(...args);
  110. currentCollapsed++;
  111. },
  112. groupEnd: () => {
  113. if (currentCollapsed > 0) currentCollapsed--;
  114. else if (currentIndent.length >= 2)
  115. currentIndent = currentIndent.slice(0, currentIndent.length - 2);
  116. },
  117. profile: console.profile && (name => console.profile(name)),
  118. profileEnd: console.profileEnd && (name => console.profileEnd(name)),
  119. clear:
  120. !appendOnly &&
  121. console.clear &&
  122. (() => {
  123. clearStatusMessage();
  124. console.clear();
  125. writeStatusMessage();
  126. }),
  127. status: appendOnly
  128. ? writeColored("<s> ", "", "")
  129. : (name, ...args) => {
  130. args = args.filter(Boolean);
  131. if (name === undefined && args.length === 0) {
  132. clearStatusMessage();
  133. currentStatusMessage = undefined;
  134. } else if (
  135. typeof name === "string" &&
  136. name.startsWith("[webpack.Progress] ")
  137. ) {
  138. currentStatusMessage = [name.slice(19), ...args];
  139. writeStatusMessage();
  140. } else if (name === "[webpack.Progress]") {
  141. currentStatusMessage = [...args];
  142. writeStatusMessage();
  143. } else {
  144. currentStatusMessage = [name, ...args];
  145. writeStatusMessage();
  146. }
  147. }
  148. };
  149. };