| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	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"); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-20 18:50:12 +08:00
										 |  |  | /** @typedef {import("../config/defaults").InfrastructureLoggingNormalizedWithDefaults} InfrastructureLoggingNormalizedWithDefaults */ | 
					
						
							| 
									
										
										
										
											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-08-20 18:50:12 +08:00
										 |  |  |  * @param {InfrastructureLoggingNormalizedWithDefaults["stream"]} options.stream stream | 
					
						
							| 
									
										
										
										
											2024-03-11 22:56:35 +08:00
										 |  |  |  * @returns {LoggerConsole} logger function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	let hasStatusMessage = false; | 
					
						
							|  |  |  | 	let currentIndent = ""; | 
					
						
							|  |  |  | 	let currentCollapsed = 0; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	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}`) + | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 				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}`); | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	const clearStatusMessage = () => { | 
					
						
							|  |  |  | 		if (hasStatusMessage) { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			stream.write("\u001B[2K\r"); | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 			hasStatusMessage = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	const writeStatusMessage = () => { | 
					
						
							|  |  |  | 		if (!currentStatusMessage) return; | 
					
						
							| 
									
										
										
										
											2025-04-26 01:43:01 +08:00
										 |  |  | 		const l = stream.columns || 40; | 
					
						
							| 
									
										
										
										
											2023-03-12 09:57:19 +08:00
										 |  |  | 		const args = truncateArgs(currentStatusMessage, l - 1); | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 		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}`); | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 		hasStatusMessage = true; | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 22:56:35 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} prefix prefix | 
					
						
							|  |  |  | 	 * @param {string} colorPrefix color prefix | 
					
						
							|  |  |  | 	 * @param {string} colorSuffix color suffix | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  | 	 * @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) => { | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 			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`); | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 			writeStatusMessage(); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	const writeGroupMessage = writeColored( | 
					
						
							|  |  |  | 		"<-> ", | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		"\u001B[1m\u001B[36m", | 
					
						
							|  |  |  | 		"\u001B[39m\u001B[22m" | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	const writeGroupCollapsedMessage = writeColored( | 
					
						
							|  |  |  | 		"<+> ", | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		"\u001B[1m\u001B[36m", | 
					
						
							|  |  |  | 		"\u001B[39m\u001B[22m" | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	return { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		log: writeColored("    ", "\u001B[1m", "\u001B[22m"), | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 		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"), | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 		logTime: writeColored( | 
					
						
							|  |  |  | 			"<t> ", | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			"\u001B[1m\u001B[35m", | 
					
						
							|  |  |  | 			"\u001B[39m\u001B[22m" | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 		), | 
					
						
							|  |  |  | 		group: (...args) => { | 
					
						
							|  |  |  | 			writeGroupMessage(...args); | 
					
						
							|  |  |  | 			if (currentCollapsed > 0) { | 
					
						
							|  |  |  | 				currentCollapsed++; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				currentIndent += "  "; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		groupCollapsed: (...args) => { | 
					
						
							|  |  |  | 			writeGroupCollapsedMessage(...args); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 			currentCollapsed++; | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		groupEnd: () => { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 			if (currentCollapsed > 0) { | 
					
						
							|  |  |  | 				currentCollapsed--; | 
					
						
							|  |  |  | 			} else if (currentIndent.length >= 2) { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 				currentIndent = currentIndent.slice(0, -2); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		profile: console.profile && ((name) => console.profile(name)), | 
					
						
							|  |  |  | 		profileEnd: console.profileEnd && ((name) => console.profileEnd(name)), | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 		clear: | 
					
						
							| 
									
										
										
										
											2024-10-02 05:18:10 +08:00
										 |  |  | 			/** @type {() => void} */ | 
					
						
							|  |  |  | 			( | 
					
						
							|  |  |  | 				!appendOnly && | 
					
						
							|  |  |  | 					console.clear && | 
					
						
							|  |  |  | 					(() => { | 
					
						
							|  |  |  | 						clearStatusMessage(); | 
					
						
							|  |  |  | 						console.clear(); | 
					
						
							|  |  |  | 						writeStatusMessage(); | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 		status: appendOnly | 
					
						
							|  |  |  | 			? writeColored("<s> ", "", "") | 
					
						
							|  |  |  | 			: (name, ...args) => { | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 					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
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | }; |