source: imaps-frontend/node_modules/webpack/lib/node/nodeConsole.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9const truncateArgs = require("../logging/truncateArgs");
10
11/** @typedef {import("../logging/createConsoleLogger").LoggerConsole} LoggerConsole */
12
13/**
14 * @param {object} options options
15 * @param {boolean=} options.colors colors
16 * @param {boolean=} options.appendOnly append only
17 * @param {NodeJS.WritableStream} options.stream stream
18 * @returns {LoggerConsole} logger function
19 */
20module.exports = ({ colors, appendOnly, stream }) => {
21 /** @type {string[] | undefined} */
22 let currentStatusMessage;
23 let hasStatusMessage = false;
24 let currentIndent = "";
25 let currentCollapsed = 0;
26
27 /**
28 * @param {string} str string
29 * @param {string} prefix prefix
30 * @param {string} colorPrefix color prefix
31 * @param {string} colorSuffix color suffix
32 * @returns {string} indented string
33 */
34 const indent = (str, prefix, colorPrefix, colorSuffix) => {
35 if (str === "") return str;
36 prefix = currentIndent + prefix;
37 if (colors) {
38 return (
39 prefix +
40 colorPrefix +
41 str.replace(/\n/g, `${colorSuffix}\n${prefix}${colorPrefix}`) +
42 colorSuffix
43 );
44 }
45
46 return prefix + str.replace(/\n/g, `\n${prefix}`);
47 };
48
49 const clearStatusMessage = () => {
50 if (hasStatusMessage) {
51 stream.write("\u001B[2K\r");
52 hasStatusMessage = false;
53 }
54 };
55
56 const writeStatusMessage = () => {
57 if (!currentStatusMessage) return;
58 const l = /** @type {TODO} */ (stream).columns || 40;
59 const args = truncateArgs(currentStatusMessage, l - 1);
60 const str = args.join(" ");
61 const coloredStr = `\u001B[1m${str}\u001B[39m\u001B[22m`;
62 stream.write(`\u001B[2K\r${coloredStr}`);
63 hasStatusMessage = true;
64 };
65
66 /**
67 * @param {string} prefix prefix
68 * @param {string} colorPrefix color prefix
69 * @param {string} colorSuffix color suffix
70 * @returns {(function(...EXPECTED_ANY[]): void)} function to write with colors
71 */
72 const writeColored =
73 (prefix, colorPrefix, colorSuffix) =>
74 (...args) => {
75 if (currentCollapsed > 0) return;
76 clearStatusMessage();
77 const str = indent(
78 util.format(...args),
79 prefix,
80 colorPrefix,
81 colorSuffix
82 );
83 stream.write(`${str}\n`);
84 writeStatusMessage();
85 };
86
87 const writeGroupMessage = writeColored(
88 "<-> ",
89 "\u001B[1m\u001B[36m",
90 "\u001B[39m\u001B[22m"
91 );
92
93 const writeGroupCollapsedMessage = writeColored(
94 "<+> ",
95 "\u001B[1m\u001B[36m",
96 "\u001B[39m\u001B[22m"
97 );
98
99 return {
100 log: writeColored(" ", "\u001B[1m", "\u001B[22m"),
101 debug: writeColored(" ", "", ""),
102 trace: writeColored(" ", "", ""),
103 info: writeColored("<i> ", "\u001B[1m\u001B[32m", "\u001B[39m\u001B[22m"),
104 warn: writeColored("<w> ", "\u001B[1m\u001B[33m", "\u001B[39m\u001B[22m"),
105 error: writeColored("<e> ", "\u001B[1m\u001B[31m", "\u001B[39m\u001B[22m"),
106 logTime: writeColored(
107 "<t> ",
108 "\u001B[1m\u001B[35m",
109 "\u001B[39m\u001B[22m"
110 ),
111 group: (...args) => {
112 writeGroupMessage(...args);
113 if (currentCollapsed > 0) {
114 currentCollapsed++;
115 } else {
116 currentIndent += " ";
117 }
118 },
119 groupCollapsed: (...args) => {
120 writeGroupCollapsedMessage(...args);
121 currentCollapsed++;
122 },
123 groupEnd: () => {
124 if (currentCollapsed > 0) currentCollapsed--;
125 else if (currentIndent.length >= 2)
126 currentIndent = currentIndent.slice(0, -2);
127 },
128 profile: console.profile && (name => console.profile(name)),
129 profileEnd: console.profileEnd && (name => console.profileEnd(name)),
130 clear:
131 /** @type {() => void} */
132 (
133 !appendOnly &&
134 console.clear &&
135 (() => {
136 clearStatusMessage();
137 console.clear();
138 writeStatusMessage();
139 })
140 ),
141 status: appendOnly
142 ? writeColored("<s> ", "", "")
143 : (name, ...args) => {
144 args = args.filter(Boolean);
145 if (name === undefined && args.length === 0) {
146 clearStatusMessage();
147 currentStatusMessage = undefined;
148 } else if (
149 typeof name === "string" &&
150 name.startsWith("[webpack.Progress] ")
151 ) {
152 currentStatusMessage = [name.slice(19), ...args];
153 writeStatusMessage();
154 } else if (name === "[webpack.Progress]") {
155 currentStatusMessage = [...args];
156 writeStatusMessage();
157 } else {
158 currentStatusMessage = [name, ...args];
159 writeStatusMessage();
160 }
161 }
162 };
163};
Note: See TracBrowser for help on using the repository browser.