feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -27,7 +27,7 @@ const LogType = Object.freeze({
status: /** @type {"status"} */ ("status") // message, arguments
});
exports.LogType = LogType;
module.exports.LogType = LogType;
/** @typedef {typeof LogType[keyof typeof LogType]} LogTypeEnum */
@@ -37,7 +37,7 @@ const TIMERS_AGGREGATES_SYMBOL = Symbol("webpack logger aggregated times");
class WebpackLogger {
/**
* @param {function(LogTypeEnum, any[]=): void} log log function
* @param {function(LogTypeEnum, EXPECTED_ANY[]=): void} log log function
* @param {function(string | function(): string): WebpackLogger} getChildLogger function to create child logger
*/
constructor(log, getChildLogger) {
@@ -45,26 +45,45 @@ class WebpackLogger {
this.getChildLogger = getChildLogger;
}
/**
* @param {...EXPECTED_ANY} args args
*/
error(...args) {
this[LOG_SYMBOL](LogType.error, args);
}
/**
* @param {...EXPECTED_ANY} args args
*/
warn(...args) {
this[LOG_SYMBOL](LogType.warn, args);
}
/**
* @param {...EXPECTED_ANY} args args
*/
info(...args) {
this[LOG_SYMBOL](LogType.info, args);
}
/**
* @param {...EXPECTED_ANY} args args
*/
log(...args) {
this[LOG_SYMBOL](LogType.log, args);
}
/**
* @param {...EXPECTED_ANY} args args
*/
debug(...args) {
this[LOG_SYMBOL](LogType.debug, args);
}
/**
* @param {EXPECTED_ANY} assertion assertion
* @param {...EXPECTED_ANY} args args
*/
assert(assertion, ...args) {
if (!assertion) {
this[LOG_SYMBOL](LogType.error, args);
@@ -79,20 +98,29 @@ class WebpackLogger {
this[LOG_SYMBOL](LogType.clear);
}
/**
* @param {...EXPECTED_ANY} args args
*/
status(...args) {
this[LOG_SYMBOL](LogType.status, args);
}
/**
* @param {...EXPECTED_ANY} args args
*/
group(...args) {
this[LOG_SYMBOL](LogType.group, args);
}
/**
* @param {...EXPECTED_ANY} args args
*/
groupCollapsed(...args) {
this[LOG_SYMBOL](LogType.groupCollapsed, args);
}
groupEnd(...args) {
this[LOG_SYMBOL](LogType.groupEnd, args);
groupEnd() {
this[LOG_SYMBOL](LogType.groupEnd);
}
/**
@@ -185,4 +213,4 @@ class WebpackLogger {
}
}
exports.Logger = WebpackLogger;
module.exports.Logger = WebpackLogger;

View File

@@ -12,24 +12,24 @@ const { LogType } = require("./Logger");
/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
/** @typedef {function(string): boolean} FilterFunction */
/** @typedef {function(string, LogTypeEnum, any[]): void} LoggingFunction */
/** @typedef {function(string, LogTypeEnum, EXPECTED_ANY[]=): void} LoggingFunction */
/**
* @typedef {object} LoggerConsole
* @property {function(): void} clear
* @property {function(): void} trace
* @property {(...args: any[]) => void} info
* @property {(...args: any[]) => void} log
* @property {(...args: any[]) => void} warn
* @property {(...args: any[]) => void} error
* @property {(...args: any[]) => void=} debug
* @property {(...args: any[]) => void=} group
* @property {(...args: any[]) => void=} groupCollapsed
* @property {(...args: any[]) => void=} groupEnd
* @property {(...args: any[]) => void=} status
* @property {(...args: any[]) => void=} profile
* @property {(...args: any[]) => void=} profileEnd
* @property {(...args: any[]) => void=} logTime
* @property {(...args: EXPECTED_ANY[]) => void} info
* @property {(...args: EXPECTED_ANY[]) => void} log
* @property {(...args: EXPECTED_ANY[]) => void} warn
* @property {(...args: EXPECTED_ANY[]) => void} error
* @property {(...args: EXPECTED_ANY[]) => void=} debug
* @property {(...args: EXPECTED_ANY[]) => void=} group
* @property {(...args: EXPECTED_ANY[]) => void=} groupCollapsed
* @property {(...args: EXPECTED_ANY[]) => void=} groupEnd
* @property {(...args: EXPECTED_ANY[]) => void=} status
* @property {(...args: EXPECTED_ANY[]) => void=} profile
* @property {(...args: EXPECTED_ANY[]) => void=} profileEnd
* @property {(...args: EXPECTED_ANY[]) => void=} logTime
*/
/**
@@ -95,7 +95,7 @@ module.exports = ({ level = "info", debug = false, console }) => {
/**
* @param {string} name name of the logger
* @param {LogTypeEnum} type type of the log entry
* @param {any[]} args arguments of the log entry
* @param {EXPECTED_ANY[]=} args arguments of the log entry
* @returns {void}
*/
const logger = (name, type, args) => {
@@ -103,12 +103,10 @@ module.exports = ({ level = "info", debug = false, console }) => {
if (Array.isArray(args)) {
if (args.length > 0 && typeof args[0] === "string") {
return [`[${name}] ${args[0]}`, ...args.slice(1)];
} else {
return [`[${name}]`, ...args];
}
} else {
return [];
return [`[${name}]`, ...args];
}
return [];
};
const debug = debugFilters.some(f => f(name));
switch (type) {
@@ -167,8 +165,11 @@ module.exports = ({ level = "info", debug = false, console }) => {
break;
case LogType.time: {
if (!debug && loglevel > LogLevel.log) return;
const ms = args[1] * 1000 + args[2] / 1000000;
const msg = `[${name}] ${args[0]}: ${ms} ms`;
const [label, start, end] =
/** @type {[string, number, number]} */
(args);
const ms = start * 1000 + end / 1000000;
const msg = `[${name}] ${label}: ${ms} ms`;
if (typeof console.logTime === "function") {
console.logTime(msg);
} else {
@@ -195,15 +196,13 @@ module.exports = ({ level = "info", debug = false, console }) => {
case LogType.status:
if (!debug && loglevel > LogLevel.info) return;
if (typeof console.status === "function") {
if (args.length === 0) {
if (!args || args.length === 0) {
console.status();
} else {
console.status(...labeledArgs());
}
} else {
if (args.length !== 0) {
console.info(...labeledArgs());
}
} else if (args && args.length !== 0) {
console.info(...labeledArgs());
}
break;
default:

View File

@@ -10,7 +10,7 @@ const { Logger } = require("./Logger");
const createConsoleLogger = require("./createConsoleLogger");
/** @type {createConsoleLogger.LoggerOptions} */
let currentDefaultLoggerOptions = {
const currentDefaultLoggerOptions = {
level: "info",
debug: false,
console
@@ -21,26 +21,25 @@ let currentDefaultLogger = createConsoleLogger(currentDefaultLoggerOptions);
* @param {string} name name of the logger
* @returns {Logger} a logger
*/
exports.getLogger = name => {
return new Logger(
module.exports.getLogger = name =>
new Logger(
(type, args) => {
if (exports.hooks.log.call(name, type, args) === undefined) {
if (module.exports.hooks.log.call(name, type, args) === undefined) {
currentDefaultLogger(name, type, args);
}
},
childName => exports.getLogger(`${name}/${childName}`)
childName => module.exports.getLogger(`${name}/${childName}`)
);
};
/**
* @param {createConsoleLogger.LoggerOptions} options new options, merge with old options
* @returns {void}
*/
exports.configureDefaultLogger = options => {
module.exports.configureDefaultLogger = options => {
Object.assign(currentDefaultLoggerOptions, options);
currentDefaultLogger = createConsoleLogger(currentDefaultLoggerOptions);
};
exports.hooks = {
module.exports.hooks = {
log: new SyncBailHook(["origin", "type", "args"])
};

View File

@@ -16,7 +16,7 @@ const arraySum = array => {
};
/**
* @param {any[]} args items to be truncated
* @param {EXPECTED_ANY[]} args items to be truncated
* @param {number} maxLength maximum length of args including spaces between
* @returns {string[]} truncated args
*/
@@ -28,17 +28,15 @@ const truncateArgs = (args, maxLength) => {
if (availableLength >= args[0].length) {
return args;
} else if (availableLength > 3) {
return ["..." + args[0].slice(-availableLength + 3)];
} else {
return [args[0].slice(-availableLength)];
return [`...${args[0].slice(-availableLength + 3)}`];
}
return [args[0].slice(-availableLength)];
}
// Check if there is space for at least 4 chars per arg
if (availableLength < arraySum(lengths.map(i => Math.min(i, 6)))) {
// remove args
if (args.length > 1)
return truncateArgs(args.slice(0, args.length - 1), maxLength);
if (args.length > 1) return truncateArgs(args.slice(0, -1), maxLength);
return [];
}
@@ -74,12 +72,11 @@ const truncateArgs = (args, maxLength) => {
if (str.length === length) {
return str;
} else if (length > 5) {
return "..." + str.slice(-length + 3);
return `...${str.slice(-length + 3)}`;
} else if (length > 0) {
return str.slice(-length);
} else {
return "";
}
return "";
});
};