nodeConsole.js 5.2 KB

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