nodeConsole.js 4.4 KB

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