webpack/lib/node/nodeConsole.js

167 lines
4.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const util = require("util");
2019-08-05 18:15:03 +08:00
const truncateArgs = require("../logging/truncateArgs");
2025-04-26 01:43:01 +08:00
/** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
2024-03-11 22:56:35 +08:00
/** @typedef {import("../logging/createConsoleLogger").LoggerConsole} LoggerConsole */
2025-03-07 23:32:55 +08:00
/* eslint-disable no-console */
2024-03-11 22:56:35 +08:00
/**
2024-06-11 21:09:50 +08:00
* @param {object} options options
2024-03-11 22:56:35 +08:00
* @param {boolean=} options.colors colors
* @param {boolean=} options.appendOnly append only
2025-04-26 01:43:01 +08:00
* @param {NonNullable<InfrastructureLogging["stream"]>} options.stream stream
2024-03-11 22:56:35 +08:00
* @returns {LoggerConsole} logger function
*/
module.exports = ({ colors, appendOnly, stream }) => {
2024-03-11 22:56:35 +08:00
/** @type {string[] | undefined} */
2024-07-31 06:15:03 +08:00
let currentStatusMessage;
let hasStatusMessage = false;
let currentIndent = "";
let currentCollapsed = 0;
2024-03-11 22:56:35 +08:00
/**
* @param {string} str string
* @param {string} prefix prefix
* @param {string} colorPrefix color prefix
* @param {string} colorSuffix color suffix
* @returns {string} indented string
*/
const indent = (str, prefix, colorPrefix, colorSuffix) => {
if (str === "") return str;
prefix = currentIndent + prefix;
if (colors) {
return (
prefix +
colorPrefix +
2024-07-31 10:39:30 +08:00
str.replace(/\n/g, `${colorSuffix}\n${prefix}${colorPrefix}`) +
colorSuffix
);
}
2024-07-31 04:21:27 +08:00
2024-07-31 10:39:30 +08:00
return prefix + str.replace(/\n/g, `\n${prefix}`);
};
const clearStatusMessage = () => {
if (hasStatusMessage) {
2024-08-02 02:36:27 +08:00
stream.write("\u001B[2K\r");
hasStatusMessage = false;
}
};
const writeStatusMessage = () => {
if (!currentStatusMessage) return;
2025-04-26 01:43:01 +08:00
const l = stream.columns || 40;
const args = truncateArgs(currentStatusMessage, l - 1);
const str = args.join(" ");
2024-08-02 02:36:27 +08:00
const coloredStr = `\u001B[1m${str}\u001B[39m\u001B[22m`;
stream.write(`\u001B[2K\r${coloredStr}`);
hasStatusMessage = true;
};
2024-03-11 22:56:35 +08:00
/**
* @param {string} prefix prefix
* @param {string} colorPrefix color prefix
* @param {string} colorSuffix color suffix
* @returns {(...args: EXPECTED_ANY[]) => void} function to write with colors
2024-03-11 22:56:35 +08:00
*/
2024-07-31 11:31:11 +08:00
const writeColored =
(prefix, colorPrefix, colorSuffix) =>
(...args) => {
if (currentCollapsed > 0) return;
clearStatusMessage();
const str = indent(
util.format(...args),
prefix,
colorPrefix,
colorSuffix
);
2024-07-31 10:39:30 +08:00
stream.write(`${str}\n`);
writeStatusMessage();
};
const writeGroupMessage = writeColored(
"<-> ",
2024-08-02 02:36:27 +08:00
"\u001B[1m\u001B[36m",
"\u001B[39m\u001B[22m"
);
const writeGroupCollapsedMessage = writeColored(
"<+> ",
2024-08-02 02:36:27 +08:00
"\u001B[1m\u001B[36m",
"\u001B[39m\u001B[22m"
);
return {
2024-08-02 02:36:27 +08:00
log: writeColored(" ", "\u001B[1m", "\u001B[22m"),
debug: writeColored(" ", "", ""),
trace: writeColored(" ", "", ""),
2024-08-02 02:36:27 +08:00
info: writeColored("<i> ", "\u001B[1m\u001B[32m", "\u001B[39m\u001B[22m"),
warn: writeColored("<w> ", "\u001B[1m\u001B[33m", "\u001B[39m\u001B[22m"),
error: writeColored("<e> ", "\u001B[1m\u001B[31m", "\u001B[39m\u001B[22m"),
logTime: writeColored(
"<t> ",
2024-08-02 02:36:27 +08:00
"\u001B[1m\u001B[35m",
"\u001B[39m\u001B[22m"
),
group: (...args) => {
writeGroupMessage(...args);
if (currentCollapsed > 0) {
currentCollapsed++;
} else {
currentIndent += " ";
}
},
groupCollapsed: (...args) => {
writeGroupCollapsedMessage(...args);
currentCollapsed++;
},
groupEnd: () => {
if (currentCollapsed > 0) currentCollapsed--;
else if (currentIndent.length >= 2)
2024-08-02 02:36:27 +08:00
currentIndent = currentIndent.slice(0, -2);
},
profile: console.profile && (name => console.profile(name)),
profileEnd: console.profileEnd && (name => console.profileEnd(name)),
clear:
2024-10-02 05:18:10 +08:00
/** @type {() => void} */
(
!appendOnly &&
console.clear &&
(() => {
clearStatusMessage();
console.clear();
writeStatusMessage();
})
),
status: appendOnly
? writeColored("<s> ", "", "")
: (name, ...args) => {
args = args.filter(Boolean);
if (name === undefined && args.length === 0) {
clearStatusMessage();
currentStatusMessage = undefined;
} else if (
typeof name === "string" &&
name.startsWith("[webpack.Progress] ")
) {
currentStatusMessage = [name.slice(19), ...args];
writeStatusMessage();
} else if (name === "[webpack.Progress]") {
currentStatusMessage = [...args];
writeStatusMessage();
} else {
currentStatusMessage = [name, ...args];
writeStatusMessage();
}
2024-07-31 05:43:19 +08:00
}
};
};