UNPKG

@intlayer/config

Version:

Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.

140 lines (138 loc) 5.28 kB
//#region src/logger.ts const logger = (content, details) => { const isVerbose = details?.isVerbose ?? false; const mode = details?.config?.mode ?? "default"; const level = details?.level ?? "info"; const prefix = details?.config?.prefix ? details?.config?.prefix : void 0; const log = details?.config?.log ?? console.log; const info = details?.config?.info ?? console.info; const warn = details?.config?.warn ?? console.warn; const error = details?.config?.error ?? console.error; const debug = details?.config?.debug ?? console.debug; if (mode === "disabled") return; if (isVerbose && mode !== "verbose") return; const flatContent = prefix ? [prefix, ...[content].flat()] : [content].flat(); if (level === "debug") return debug(...flatContent); if (level === "info") return info(...flatContent); if (level === "warn") return warn(...flatContent); if (level === "error") return error(...flatContent); log(...flatContent); }; let ANSIColors = /* @__PURE__ */ function(ANSIColors$1) { ANSIColors$1["RESET"] = "\x1B[0m"; ANSIColors$1["GREY"] = "\x1B[90m"; ANSIColors$1["GREY_DARK"] = "\x1B[38;5;239m"; ANSIColors$1["GREY_LIGHT"] = "\x1B[38;5;252m"; ANSIColors$1["BLUE"] = "\x1B[34m"; ANSIColors$1["RED"] = "\x1B[31m"; ANSIColors$1["GREEN"] = "\x1B[32m"; ANSIColors$1["YELLOW"] = "\x1B[38;5;226m"; ANSIColors$1["MAGENTA"] = "\x1B[35m"; ANSIColors$1["BEIGE"] = "\x1B[38;5;3m"; ANSIColors$1["ORANGE"] = "\x1B[38;5;208m"; ANSIColors$1["CYAN"] = "\x1B[36m"; ANSIColors$1["WHITE"] = "\x1B[37m"; ANSIColors$1["BOLD"] = "\x1B[1m"; return ANSIColors$1; }({}); const spinnerFrames = [ "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" ]; /** * The appLogger function takes the logger and merges it with the configuration from the intlayer config file. * It allows overriding the default configuration by passing a config object in the details parameter. * The configuration is merged with the default configuration from the intlayer config file. */ const getAppLogger = (configuration, globalDetails) => (content, details) => logger(content, { ...details ?? {}, config: { ...configuration?.log, ...globalDetails?.config, ...details?.config ?? {} } }); const colorize = (s, color, reset) => color ? `${color}${s}${reset ? typeof reset === "boolean" ? ANSIColors.RESET : reset : ANSIColors.RESET}` : s; const colorizeLocales = (locales, color = ANSIColors.GREEN, reset = ANSIColors.RESET) => [locales].flat().map((locale) => colorize(locale, color, reset)).join(`, `); const colorizeKey = (keyPath, color = ANSIColors.BEIGE, reset = ANSIColors.RESET) => [keyPath].flat().map((key) => colorize(key, color, reset)).join(`, `); const colorizePath = (path, color = ANSIColors.GREY, reset = ANSIColors.RESET) => [path].flat().map((p) => colorize(p, color, reset)).join(`, `); /** * Colorize numeric value using Intl.NumberFormat and optional ANSI colors. * * Examples: * colorizeNumber(2, [{ pluralRule: 'one' , color: ANSIColors.GREEN}, { pluralRule: 'other' , color: ANSIColors.RED}]) // "'\x1b[31m2\x1b[0m" */ const colorizeNumber = (number, options = { zero: ANSIColors.BLUE, one: ANSIColors.BLUE, two: ANSIColors.BLUE, few: ANSIColors.BLUE, many: ANSIColors.BLUE, other: ANSIColors.BLUE }) => { if (number === 0) { const color$1 = options.zero ?? ANSIColors.GREEN; return colorize(number.toString(), color$1); } const color = options[new Intl.PluralRules("en").select(number)]; return colorize(number.toString(), color); }; const removeColor = (text) => text.replace(/\x1b\[[0-9;]*m/g, ""); const getLength = (length) => { let value = 0; if (typeof length === "number") value = length; if (typeof length === "string") value = length.length; if (Array.isArray(length) && length.every((l) => typeof l === "string")) value = Math.max(...length.map((str) => str.length)); if (Array.isArray(length) && length.every((l) => typeof l === "number")) value = Math.max(...length); return Math.max(value, 0); }; const defaultColonOptions = { colSize: 0, minSize: 0, maxSize: Infinity, pad: "right", padChar: "0" }; /** * Create a string of spaces of a given length. * * @param colSize - The length of the string to create. * @returns A string of spaces. */ const colon = (text, options) => [text].flat().map((text$1) => { const { colSize, minSize, maxSize, pad, padChar } = { ...defaultColonOptions, ...options ?? {} }; const length = getLength(colSize); const spacesLength = Math.max(minSize, Math.min(maxSize, length - removeColor(text$1).length)); if (pad === "left") return `${" ".repeat(spacesLength)}${text$1}`; return `${text$1}${" ".repeat(spacesLength)}`; }).join(""); const x = colorize("✗", ANSIColors.RED); const v = colorize("✓", ANSIColors.GREEN); const clock = colorize("⏲", ANSIColors.BLUE); //#endregion exports.ANSIColors = ANSIColors; exports.clock = clock; exports.colon = colon; exports.colorize = colorize; exports.colorizeKey = colorizeKey; exports.colorizeLocales = colorizeLocales; exports.colorizeNumber = colorizeNumber; exports.colorizePath = colorizePath; exports.getAppLogger = getAppLogger; exports.logger = logger; exports.removeColor = removeColor; exports.spinnerFrames = spinnerFrames; exports.v = v; exports.x = x; //# sourceMappingURL=logger.cjs.map