nodeConsole.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const truncateArgs = require("../logging/truncateArgs");
  7. const util = require("util");
  8. const tty = process.stderr.isTTY && process.env.TERM !== "dumb";
  9. let currentStatusMessage = undefined;
  10. let hasStatusMessage = false;
  11. let currentIndent = "";
  12. let currentCollapsed = 0;
  13. const indent = (str, prefix, colorPrefix, colorSuffix) => {
  14. if (str === "") return str;
  15. prefix = currentIndent + prefix;
  16. if (tty) {
  17. return (
  18. prefix +
  19. colorPrefix +
  20. str.replace(/\n/g, colorSuffix + "\n" + prefix + colorPrefix) +
  21. colorSuffix
  22. );
  23. } else {
  24. return prefix + str.replace(/\n/g, "\n" + prefix);
  25. }
  26. };
  27. const clearStatusMessage = () => {
  28. if (hasStatusMessage) {
  29. process.stderr.write("\x1b[2K\r");
  30. hasStatusMessage = false;
  31. }
  32. };
  33. const writeStatusMessage = () => {
  34. if (!currentStatusMessage) return;
  35. const l = process.stderr.columns;
  36. const args = l
  37. ? truncateArgs(currentStatusMessage, l - 1)
  38. : currentStatusMessage;
  39. const str = args.join(" ");
  40. const coloredStr = `\u001b[1m${str}\u001b[39m\u001b[22m`;
  41. process.stderr.write(`\x1b[2K\r${coloredStr}`);
  42. hasStatusMessage = true;
  43. };
  44. const writeColored = (prefix, colorPrefix, colorSuffix) => {
  45. return (...args) => {
  46. if (currentCollapsed > 0) return;
  47. clearStatusMessage();
  48. // @ts-ignore
  49. const str = indent(util.format(...args), prefix, colorPrefix, colorSuffix);
  50. process.stderr.write(str + "\n");
  51. writeStatusMessage();
  52. };
  53. };
  54. const writeGroupMessage = writeColored(
  55. "<-> ",
  56. "\u001b[1m\u001b[36m",
  57. "\u001b[39m\u001b[22m"
  58. );
  59. const writeGroupCollapsedMessage = writeColored(
  60. "<+> ",
  61. "\u001b[1m\u001b[36m",
  62. "\u001b[39m\u001b[22m"
  63. );
  64. module.exports = {
  65. log: writeColored(" ", "\u001b[1m", "\u001b[22m"),
  66. debug: writeColored(" ", "", ""),
  67. trace: writeColored(" ", "", ""),
  68. info: writeColored("<i> ", "\u001b[1m\u001b[32m", "\u001b[39m\u001b[22m"),
  69. warn: writeColored("<w> ", "\u001b[1m\u001b[33m", "\u001b[39m\u001b[22m"),
  70. error: writeColored("<e> ", "\u001b[1m\u001b[31m", "\u001b[39m\u001b[22m"),
  71. logTime: writeColored("<t> ", "\u001b[1m\u001b[35m", "\u001b[39m\u001b[22m"),
  72. group: (...args) => {
  73. writeGroupMessage(...args);
  74. if (currentCollapsed > 0) {
  75. currentCollapsed++;
  76. } else {
  77. currentIndent += " ";
  78. }
  79. },
  80. groupCollapsed: (...args) => {
  81. writeGroupCollapsedMessage(...args);
  82. currentCollapsed++;
  83. },
  84. groupEnd: () => {
  85. if (currentCollapsed > 0) currentCollapsed--;
  86. else if (currentIndent.length >= 2)
  87. currentIndent = currentIndent.slice(0, currentIndent.length - 2);
  88. },
  89. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  90. profile: console.profile && (name => console.profile(name)),
  91. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  92. profileEnd: console.profileEnd && (name => console.profileEnd(name)),
  93. clear:
  94. tty &&
  95. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  96. console.clear &&
  97. (() => {
  98. clearStatusMessage();
  99. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  100. console.clear();
  101. writeStatusMessage();
  102. }),
  103. status: tty
  104. ? (name, ...args) => {
  105. args = args.filter(Boolean);
  106. if (name === undefined && args.length === 0) {
  107. clearStatusMessage();
  108. currentStatusMessage = undefined;
  109. } else if (
  110. typeof name === "string" &&
  111. name.startsWith("[webpack.Progress] ")
  112. ) {
  113. currentStatusMessage = [name.slice(19), ...args];
  114. writeStatusMessage();
  115. } else if (name === "[webpack.Progress]") {
  116. currentStatusMessage = [...args];
  117. writeStatusMessage();
  118. } else {
  119. currentStatusMessage = [name, ...args];
  120. writeStatusMessage();
  121. }
  122. }
  123. : writeColored("<s> ", "", "")
  124. };