nodeConsole.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* eslint-disable no-console */
  10. /**
  11. * @param {object} options options
  12. * @param {boolean=} options.colors colors
  13. * @param {boolean=} options.appendOnly append only
  14. * @param {NodeJS.WritableStream} options.stream stream
  15. * @returns {LoggerConsole} logger function
  16. */
  17. module.exports = ({ colors, appendOnly, stream }) => {
  18. /** @type {string[] | undefined} */
  19. let currentStatusMessage;
  20. let hasStatusMessage = false;
  21. let currentIndent = "";
  22. let currentCollapsed = 0;
  23. /**
  24. * @param {string} str string
  25. * @param {string} prefix prefix
  26. * @param {string} colorPrefix color prefix
  27. * @param {string} colorSuffix color suffix
  28. * @returns {string} indented string
  29. */
  30. const indent = (str, prefix, colorPrefix, colorSuffix) => {
  31. if (str === "") return str;
  32. prefix = currentIndent + prefix;
  33. if (colors) {
  34. return (
  35. prefix +
  36. colorPrefix +
  37. str.replace(/\n/g, `${colorSuffix}\n${prefix}${colorPrefix}`) +
  38. colorSuffix
  39. );
  40. }
  41. return prefix + str.replace(/\n/g, `\n${prefix}`);
  42. };
  43. const clearStatusMessage = () => {
  44. if (hasStatusMessage) {
  45. stream.write("\u001B[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(`\u001B[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 {(...args: EXPECTED_ANY[]) => void} function to write with colors
  63. */
  64. const writeColored =
  65. (prefix, colorPrefix, colorSuffix) =>
  66. (...args) => {
  67. if (currentCollapsed > 0) return;
  68. clearStatusMessage();
  69. const str = indent(
  70. util.format(...args),
  71. prefix,
  72. colorPrefix,
  73. colorSuffix
  74. );
  75. stream.write(`${str}\n`);
  76. writeStatusMessage();
  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, -2);
  116. },
  117. profile: console.profile && (name => console.profile(name)),
  118. profileEnd: console.profileEnd && (name => console.profileEnd(name)),
  119. clear:
  120. /** @type {() => void} */
  121. (
  122. !appendOnly &&
  123. console.clear &&
  124. (() => {
  125. clearStatusMessage();
  126. console.clear();
  127. writeStatusMessage();
  128. })
  129. ),
  130. status: appendOnly
  131. ? writeColored("<s> ", "", "")
  132. : (name, ...args) => {
  133. args = args.filter(Boolean);
  134. if (name === undefined && args.length === 0) {
  135. clearStatusMessage();
  136. currentStatusMessage = undefined;
  137. } else if (
  138. typeof name === "string" &&
  139. name.startsWith("[webpack.Progress] ")
  140. ) {
  141. currentStatusMessage = [name.slice(19), ...args];
  142. writeStatusMessage();
  143. } else if (name === "[webpack.Progress]") {
  144. currentStatusMessage = [...args];
  145. writeStatusMessage();
  146. } else {
  147. currentStatusMessage = [name, ...args];
  148. writeStatusMessage();
  149. }
  150. }
  151. };
  152. };