nodeConsole.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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;
  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. }
  40. return prefix + str.replace(/\n/g, `\n${prefix}`);
  41. };
  42. const clearStatusMessage = () => {
  43. if (hasStatusMessage) {
  44. stream.write("\u001B[2K\r");
  45. hasStatusMessage = false;
  46. }
  47. };
  48. const writeStatusMessage = () => {
  49. if (!currentStatusMessage) return;
  50. const l = /** @type {TODO} */ (stream).columns || 40;
  51. const args = truncateArgs(currentStatusMessage, l - 1);
  52. const str = args.join(" ");
  53. const coloredStr = `\u001B[1m${str}\u001B[39m\u001B[22m`;
  54. stream.write(`\u001B[2K\r${coloredStr}`);
  55. hasStatusMessage = true;
  56. };
  57. /**
  58. * @param {string} prefix prefix
  59. * @param {string} colorPrefix color prefix
  60. * @param {string} colorSuffix color suffix
  61. * @returns {(function(...EXPECTED_ANY[]): void)} function to write with colors
  62. */
  63. const writeColored =
  64. (prefix, colorPrefix, colorSuffix) =>
  65. (...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. const writeGroupMessage = writeColored(
  78. "<-> ",
  79. "\u001B[1m\u001B[36m",
  80. "\u001B[39m\u001B[22m"
  81. );
  82. const writeGroupCollapsedMessage = writeColored(
  83. "<+> ",
  84. "\u001B[1m\u001B[36m",
  85. "\u001B[39m\u001B[22m"
  86. );
  87. return {
  88. log: writeColored(" ", "\u001B[1m", "\u001B[22m"),
  89. debug: writeColored(" ", "", ""),
  90. trace: writeColored(" ", "", ""),
  91. info: writeColored("<i> ", "\u001B[1m\u001B[32m", "\u001B[39m\u001B[22m"),
  92. warn: writeColored("<w> ", "\u001B[1m\u001B[33m", "\u001B[39m\u001B[22m"),
  93. error: writeColored("<e> ", "\u001B[1m\u001B[31m", "\u001B[39m\u001B[22m"),
  94. logTime: writeColored(
  95. "<t> ",
  96. "\u001B[1m\u001B[35m",
  97. "\u001B[39m\u001B[22m"
  98. ),
  99. group: (...args) => {
  100. writeGroupMessage(...args);
  101. if (currentCollapsed > 0) {
  102. currentCollapsed++;
  103. } else {
  104. currentIndent += " ";
  105. }
  106. },
  107. groupCollapsed: (...args) => {
  108. writeGroupCollapsedMessage(...args);
  109. currentCollapsed++;
  110. },
  111. groupEnd: () => {
  112. if (currentCollapsed > 0) currentCollapsed--;
  113. else if (currentIndent.length >= 2)
  114. currentIndent = currentIndent.slice(0, -2);
  115. },
  116. profile: console.profile && (name => console.profile(name)),
  117. profileEnd: console.profileEnd && (name => console.profileEnd(name)),
  118. clear:
  119. /** @type {() => void} */
  120. (
  121. !appendOnly &&
  122. console.clear &&
  123. (() => {
  124. clearStatusMessage();
  125. console.clear();
  126. writeStatusMessage();
  127. })
  128. ),
  129. status: appendOnly
  130. ? writeColored("<s> ", "", "")
  131. : (name, ...args) => {
  132. args = args.filter(Boolean);
  133. if (name === undefined && args.length === 0) {
  134. clearStatusMessage();
  135. currentStatusMessage = undefined;
  136. } else if (
  137. typeof name === "string" &&
  138. name.startsWith("[webpack.Progress] ")
  139. ) {
  140. currentStatusMessage = [name.slice(19), ...args];
  141. writeStatusMessage();
  142. } else if (name === "[webpack.Progress]") {
  143. currentStatusMessage = [...args];
  144. writeStatusMessage();
  145. } else {
  146. currentStatusMessage = [name, ...args];
  147. writeStatusMessage();
  148. }
  149. }
  150. };
  151. };