nodeConsole.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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("../config/defaults").InfrastructureLoggingNormalizedWithDefaults} InfrastructureLoggingNormalizedWithDefaults */
  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 {InfrastructureLoggingNormalizedWithDefaults["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. * @template T
  61. * @param {string} prefix prefix
  62. * @param {string} colorPrefix color prefix
  63. * @param {string} colorSuffix color suffix
  64. * @returns {(...args: T[]) => void} function to write with colors
  65. */
  66. const writeColored =
  67. (prefix, colorPrefix, colorSuffix) =>
  68. (...args) => {
  69. if (currentCollapsed > 0) return;
  70. clearStatusMessage();
  71. const str = indent(
  72. util.format(...args),
  73. prefix,
  74. colorPrefix,
  75. colorSuffix
  76. );
  77. stream.write(`${str}\n`);
  78. writeStatusMessage();
  79. };
  80. /** @type {<T extends unknown[]>(...args: T) => void} */
  81. const writeGroupMessage = writeColored(
  82. "<-> ",
  83. "\u001B[1m\u001B[36m",
  84. "\u001B[39m\u001B[22m"
  85. );
  86. /** @type {<T extends unknown[]>(...args: T) => void} */
  87. const writeGroupCollapsedMessage = writeColored(
  88. "<+> ",
  89. "\u001B[1m\u001B[36m",
  90. "\u001B[39m\u001B[22m"
  91. );
  92. return {
  93. /** @type {LoggerConsole["log"]} */
  94. log: writeColored(" ", "\u001B[1m", "\u001B[22m"),
  95. /** @type {LoggerConsole["debug"]} */
  96. debug: writeColored(" ", "", ""),
  97. /** @type {LoggerConsole["trace"]} */
  98. trace: writeColored(" ", "", ""),
  99. /** @type {LoggerConsole["info"]} */
  100. info: writeColored("<i> ", "\u001B[1m\u001B[32m", "\u001B[39m\u001B[22m"),
  101. /** @type {LoggerConsole["warn"]} */
  102. warn: writeColored("<w> ", "\u001B[1m\u001B[33m", "\u001B[39m\u001B[22m"),
  103. /** @type {LoggerConsole["error"]} */
  104. error: writeColored("<e> ", "\u001B[1m\u001B[31m", "\u001B[39m\u001B[22m"),
  105. /** @type {LoggerConsole["logTime"]} */
  106. logTime: writeColored(
  107. "<t> ",
  108. "\u001B[1m\u001B[35m",
  109. "\u001B[39m\u001B[22m"
  110. ),
  111. /** @type {LoggerConsole["group"]} */
  112. group: (...args) => {
  113. writeGroupMessage(...args);
  114. if (currentCollapsed > 0) {
  115. currentCollapsed++;
  116. } else {
  117. currentIndent += " ";
  118. }
  119. },
  120. /** @type {LoggerConsole["groupCollapsed"]} */
  121. groupCollapsed: (...args) => {
  122. writeGroupCollapsedMessage(...args);
  123. currentCollapsed++;
  124. },
  125. /** @type {LoggerConsole["groupEnd"]} */
  126. groupEnd: () => {
  127. if (currentCollapsed > 0) {
  128. currentCollapsed--;
  129. } else if (currentIndent.length >= 2) {
  130. currentIndent = currentIndent.slice(0, -2);
  131. }
  132. },
  133. /** @type {LoggerConsole["profile"]} */
  134. profile: console.profile && ((name) => console.profile(name)),
  135. /** @type {LoggerConsole["profileEnd"]} */
  136. profileEnd: console.profileEnd && ((name) => console.profileEnd(name)),
  137. /** @type {LoggerConsole["clear"]} */
  138. clear:
  139. /** @type {() => void} */
  140. (
  141. !appendOnly &&
  142. console.clear &&
  143. (() => {
  144. clearStatusMessage();
  145. console.clear();
  146. writeStatusMessage();
  147. })
  148. ),
  149. /** @type {LoggerConsole["status"]} */
  150. status: appendOnly
  151. ? writeColored("<s> ", "", "")
  152. : (name, ...args) => {
  153. args = args.filter(Boolean);
  154. if (name === undefined && args.length === 0) {
  155. clearStatusMessage();
  156. currentStatusMessage = undefined;
  157. } else if (
  158. typeof name === "string" &&
  159. name.startsWith("[webpack.Progress] ")
  160. ) {
  161. currentStatusMessage = [name.slice(19), ...args];
  162. writeStatusMessage();
  163. } else if (name === "[webpack.Progress]") {
  164. currentStatusMessage = [...args];
  165. writeStatusMessage();
  166. } else {
  167. currentStatusMessage = [name, ...args];
  168. writeStatusMessage();
  169. }
  170. }
  171. };
  172. };