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