| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | module.exports = ({ colors, appendOnly, stream }) => { | 
					
						
							|  |  |  | 	let currentStatusMessage = undefined; | 
					
						
							|  |  |  | 	let hasStatusMessage = false; | 
					
						
							|  |  |  | 	let currentIndent = ""; | 
					
						
							|  |  |  | 	let currentCollapsed = 0; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 + | 
					
						
							|  |  |  | 				str.replace(/\n/g, colorSuffix + "\n" + prefix + colorPrefix) + | 
					
						
							|  |  |  | 				colorSuffix | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return prefix + str.replace(/\n/g, "\n" + prefix); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	const clearStatusMessage = () => { | 
					
						
							|  |  |  | 		if (hasStatusMessage) { | 
					
						
							|  |  |  | 			stream.write("\x1b[2K\r"); | 
					
						
							|  |  |  | 			hasStatusMessage = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	const writeStatusMessage = () => { | 
					
						
							|  |  |  | 		if (!currentStatusMessage) return; | 
					
						
							|  |  |  | 		const l = stream.columns; | 
					
						
							|  |  |  | 		const args = l | 
					
						
							|  |  |  | 			? truncateArgs(currentStatusMessage, l - 1) | 
					
						
							|  |  |  | 			: currentStatusMessage; | 
					
						
							|  |  |  | 		const str = args.join(" "); | 
					
						
							|  |  |  | 		const coloredStr = `\u001b[1m${str}\u001b[39m\u001b[22m`; | 
					
						
							|  |  |  | 		stream.write(`\x1b[2K\r${coloredStr}`); | 
					
						
							|  |  |  | 		hasStatusMessage = true; | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 	const writeColored = (prefix, colorPrefix, colorSuffix) => { | 
					
						
							|  |  |  | 		return (...args) => { | 
					
						
							|  |  |  | 			if (currentCollapsed > 0) return; | 
					
						
							|  |  |  | 			clearStatusMessage(); | 
					
						
							|  |  |  | 			const str = indent( | 
					
						
							|  |  |  | 				util.format(...args), | 
					
						
							|  |  |  | 				prefix, | 
					
						
							|  |  |  | 				colorPrefix, | 
					
						
							|  |  |  | 				colorSuffix | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			stream.write(str + "\n"); | 
					
						
							|  |  |  | 			writeStatusMessage(); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	const writeGroupMessage = writeColored( | 
					
						
							|  |  |  | 		"<-> ", | 
					
						
							|  |  |  | 		"\u001b[1m\u001b[36m", | 
					
						
							|  |  |  | 		"\u001b[39m\u001b[22m" | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	const writeGroupCollapsedMessage = writeColored( | 
					
						
							|  |  |  | 		"<+> ", | 
					
						
							|  |  |  | 		"\u001b[1m\u001b[36m", | 
					
						
							|  |  |  | 		"\u001b[39m\u001b[22m" | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 	return { | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 		log: writeColored("    ", "\u001b[1m", "\u001b[22m"), | 
					
						
							|  |  |  | 		debug: writeColored("    ", "", ""), | 
					
						
							|  |  |  | 		trace: writeColored("    ", "", ""), | 
					
						
							|  |  |  | 		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> ", | 
					
						
							|  |  |  | 			"\u001b[1m\u001b[35m", | 
					
						
							|  |  |  | 			"\u001b[39m\u001b[22m" | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		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: () => { | 
					
						
							|  |  |  | 			if (currentCollapsed > 0) currentCollapsed--; | 
					
						
							|  |  |  | 			else if (currentIndent.length >= 2) | 
					
						
							|  |  |  | 				currentIndent = currentIndent.slice(0, currentIndent.length - 2); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 		profile: console.profile && (name => console.profile(name)), | 
					
						
							|  |  |  | 		// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 		profileEnd: console.profileEnd && (name => console.profileEnd(name)), | 
					
						
							|  |  |  | 		clear: | 
					
						
							| 
									
										
										
										
											2021-04-06 21:20:27 +08:00
										 |  |  | 			!appendOnly && | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 			// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:38:46 +08:00
										 |  |  | 			console.clear && | 
					
						
							|  |  |  | 			(() => { | 
					
						
							|  |  |  | 				clearStatusMessage(); | 
					
						
							|  |  |  | 				// eslint-disable-next-line node/no-unsupported-features/node-builtins
 | 
					
						
							|  |  |  | 				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(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 			  } | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | }; |