first build
This commit is contained in:
-280
File diff suppressed because one or more lines are too long
-512
@@ -1,512 +0,0 @@
|
||||
const LogLevels = {
|
||||
silent: Number.NEGATIVE_INFINITY,
|
||||
fatal: 0,
|
||||
error: 0,
|
||||
warn: 1,
|
||||
log: 2,
|
||||
info: 3,
|
||||
success: 3,
|
||||
fail: 3,
|
||||
ready: 3,
|
||||
start: 3,
|
||||
box: 3,
|
||||
debug: 4,
|
||||
trace: 5,
|
||||
verbose: Number.POSITIVE_INFINITY
|
||||
};
|
||||
const LogTypes = {
|
||||
// Silent
|
||||
silent: {
|
||||
level: -1
|
||||
},
|
||||
// Level 0
|
||||
fatal: {
|
||||
level: LogLevels.fatal
|
||||
},
|
||||
error: {
|
||||
level: LogLevels.error
|
||||
},
|
||||
// Level 1
|
||||
warn: {
|
||||
level: LogLevels.warn
|
||||
},
|
||||
// Level 2
|
||||
log: {
|
||||
level: LogLevels.log
|
||||
},
|
||||
// Level 3
|
||||
info: {
|
||||
level: LogLevels.info
|
||||
},
|
||||
success: {
|
||||
level: LogLevels.success
|
||||
},
|
||||
fail: {
|
||||
level: LogLevels.fail
|
||||
},
|
||||
ready: {
|
||||
level: LogLevels.info
|
||||
},
|
||||
start: {
|
||||
level: LogLevels.info
|
||||
},
|
||||
box: {
|
||||
level: LogLevels.info
|
||||
},
|
||||
// Level 4
|
||||
debug: {
|
||||
level: LogLevels.debug
|
||||
},
|
||||
// Level 5
|
||||
trace: {
|
||||
level: LogLevels.trace
|
||||
},
|
||||
// Verbose
|
||||
verbose: {
|
||||
level: LogLevels.verbose
|
||||
}
|
||||
};
|
||||
|
||||
function isPlainObject$1(value) {
|
||||
if (value === null || typeof value !== "object") {
|
||||
return false;
|
||||
}
|
||||
const prototype = Object.getPrototypeOf(value);
|
||||
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
|
||||
return false;
|
||||
}
|
||||
if (Symbol.iterator in value) {
|
||||
return false;
|
||||
}
|
||||
if (Symbol.toStringTag in value) {
|
||||
return Object.prototype.toString.call(value) === "[object Module]";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function _defu(baseObject, defaults, namespace = ".", merger) {
|
||||
if (!isPlainObject$1(defaults)) {
|
||||
return _defu(baseObject, {}, namespace, merger);
|
||||
}
|
||||
const object = Object.assign({}, defaults);
|
||||
for (const key in baseObject) {
|
||||
if (key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const value = baseObject[key];
|
||||
if (value === null || value === void 0) {
|
||||
continue;
|
||||
}
|
||||
if (merger && merger(object, key, value, namespace)) {
|
||||
continue;
|
||||
}
|
||||
if (Array.isArray(value) && Array.isArray(object[key])) {
|
||||
object[key] = [...value, ...object[key]];
|
||||
} else if (isPlainObject$1(value) && isPlainObject$1(object[key])) {
|
||||
object[key] = _defu(
|
||||
value,
|
||||
object[key],
|
||||
(namespace ? `${namespace}.` : "") + key.toString(),
|
||||
merger
|
||||
);
|
||||
} else {
|
||||
object[key] = value;
|
||||
}
|
||||
}
|
||||
return object;
|
||||
}
|
||||
function createDefu(merger) {
|
||||
return (...arguments_) => (
|
||||
// eslint-disable-next-line unicorn/no-array-reduce
|
||||
arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
|
||||
);
|
||||
}
|
||||
const defu = createDefu();
|
||||
|
||||
function isPlainObject(obj) {
|
||||
return Object.prototype.toString.call(obj) === "[object Object]";
|
||||
}
|
||||
function isLogObj(arg) {
|
||||
if (!isPlainObject(arg)) {
|
||||
return false;
|
||||
}
|
||||
if (!arg.message && !arg.args) {
|
||||
return false;
|
||||
}
|
||||
if (arg.stack) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let paused = false;
|
||||
const queue = [];
|
||||
class Consola {
|
||||
options;
|
||||
_lastLog;
|
||||
_mockFn;
|
||||
/**
|
||||
* Creates an instance of Consola with specified options or defaults.
|
||||
*
|
||||
* @param {Partial<ConsolaOptions>} [options={}] - Configuration options for the Consola instance.
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
const types = options.types || LogTypes;
|
||||
this.options = defu(
|
||||
{
|
||||
...options,
|
||||
defaults: { ...options.defaults },
|
||||
level: _normalizeLogLevel(options.level, types),
|
||||
reporters: [...options.reporters || []]
|
||||
},
|
||||
{
|
||||
types: LogTypes,
|
||||
throttle: 1e3,
|
||||
throttleMin: 5,
|
||||
formatOptions: {
|
||||
date: true,
|
||||
colors: false,
|
||||
compact: true
|
||||
}
|
||||
}
|
||||
);
|
||||
for (const type in types) {
|
||||
const defaults = {
|
||||
type,
|
||||
...this.options.defaults,
|
||||
...types[type]
|
||||
};
|
||||
this[type] = this._wrapLogFn(defaults);
|
||||
this[type].raw = this._wrapLogFn(
|
||||
defaults,
|
||||
true
|
||||
);
|
||||
}
|
||||
if (this.options.mockFn) {
|
||||
this.mockTypes();
|
||||
}
|
||||
this._lastLog = {};
|
||||
}
|
||||
/**
|
||||
* Gets the current log level of the Consola instance.
|
||||
*
|
||||
* @returns {number} The current log level.
|
||||
*/
|
||||
get level() {
|
||||
return this.options.level;
|
||||
}
|
||||
/**
|
||||
* Sets the minimum log level that will be output by the instance.
|
||||
*
|
||||
* @param {number} level - The new log level to set.
|
||||
*/
|
||||
set level(level) {
|
||||
this.options.level = _normalizeLogLevel(
|
||||
level,
|
||||
this.options.types,
|
||||
this.options.level
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Displays a prompt to the user and returns the response.
|
||||
* Throw an error if `prompt` is not supported by the current configuration.
|
||||
*
|
||||
* @template T
|
||||
* @param {string} message - The message to display in the prompt.
|
||||
* @param {T} [opts] - Optional options for the prompt. See {@link PromptOptions}.
|
||||
* @returns {promise<T>} A promise that infer with the prompt options. See {@link PromptOptions}.
|
||||
*/
|
||||
prompt(message, opts) {
|
||||
if (!this.options.prompt) {
|
||||
throw new Error("prompt is not supported!");
|
||||
}
|
||||
return this.options.prompt(message, opts);
|
||||
}
|
||||
/**
|
||||
* Creates a new instance of Consola, inheriting options from the current instance, with possible overrides.
|
||||
*
|
||||
* @param {Partial<ConsolaOptions>} options - Optional overrides for the new instance. See {@link ConsolaOptions}.
|
||||
* @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
|
||||
*/
|
||||
create(options) {
|
||||
const instance = new Consola({
|
||||
...this.options,
|
||||
...options
|
||||
});
|
||||
if (this._mockFn) {
|
||||
instance.mockTypes(this._mockFn);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
/**
|
||||
* Creates a new Consola instance with the specified default log object properties.
|
||||
*
|
||||
* @param {InputLogObject} defaults - Default properties to include in any log from the new instance. See {@link InputLogObject}.
|
||||
* @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
|
||||
*/
|
||||
withDefaults(defaults) {
|
||||
return this.create({
|
||||
...this.options,
|
||||
defaults: {
|
||||
...this.options.defaults,
|
||||
...defaults
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates a new Consola instance with a specified tag, which will be included in every log.
|
||||
*
|
||||
* @param {string} tag - The tag to include in each log of the new instance.
|
||||
* @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
|
||||
*/
|
||||
withTag(tag) {
|
||||
return this.withDefaults({
|
||||
tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Adds a custom reporter to the Consola instance.
|
||||
* Reporters will be called for each log message, depending on their implementation and log level.
|
||||
*
|
||||
* @param {ConsolaReporter} reporter - The reporter to add. See {@link ConsolaReporter}.
|
||||
* @returns {Consola} The current Consola instance.
|
||||
*/
|
||||
addReporter(reporter) {
|
||||
this.options.reporters.push(reporter);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Removes a custom reporter from the Consola instance.
|
||||
* If no reporter is specified, all reporters will be removed.
|
||||
*
|
||||
* @param {ConsolaReporter} reporter - The reporter to remove. See {@link ConsolaReporter}.
|
||||
* @returns {Consola} The current Consola instance.
|
||||
*/
|
||||
removeReporter(reporter) {
|
||||
if (reporter) {
|
||||
const i = this.options.reporters.indexOf(reporter);
|
||||
if (i !== -1) {
|
||||
return this.options.reporters.splice(i, 1);
|
||||
}
|
||||
} else {
|
||||
this.options.reporters.splice(0);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Replaces all reporters of the Consola instance with the specified array of reporters.
|
||||
*
|
||||
* @param {ConsolaReporter[]} reporters - The new reporters to set. See {@link ConsolaReporter}.
|
||||
* @returns {Consola} The current Consola instance.
|
||||
*/
|
||||
setReporters(reporters) {
|
||||
this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
|
||||
return this;
|
||||
}
|
||||
wrapAll() {
|
||||
this.wrapConsole();
|
||||
this.wrapStd();
|
||||
}
|
||||
restoreAll() {
|
||||
this.restoreConsole();
|
||||
this.restoreStd();
|
||||
}
|
||||
/**
|
||||
* Overrides console methods with Consola logging methods for consistent logging.
|
||||
*/
|
||||
wrapConsole() {
|
||||
for (const type in this.options.types) {
|
||||
if (!console["__" + type]) {
|
||||
console["__" + type] = console[type];
|
||||
}
|
||||
console[type] = this[type].raw;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Restores the original console methods, removing Consola overrides.
|
||||
*/
|
||||
restoreConsole() {
|
||||
for (const type in this.options.types) {
|
||||
if (console["__" + type]) {
|
||||
console[type] = console["__" + type];
|
||||
delete console["__" + type];
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Overrides standard output and error streams to redirect them through Consola.
|
||||
*/
|
||||
wrapStd() {
|
||||
this._wrapStream(this.options.stdout, "log");
|
||||
this._wrapStream(this.options.stderr, "log");
|
||||
}
|
||||
_wrapStream(stream, type) {
|
||||
if (!stream) {
|
||||
return;
|
||||
}
|
||||
if (!stream.__write) {
|
||||
stream.__write = stream.write;
|
||||
}
|
||||
stream.write = (data) => {
|
||||
this[type].raw(String(data).trim());
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Restores the original standard output and error streams, removing the Consola redirection.
|
||||
*/
|
||||
restoreStd() {
|
||||
this._restoreStream(this.options.stdout);
|
||||
this._restoreStream(this.options.stderr);
|
||||
}
|
||||
_restoreStream(stream) {
|
||||
if (!stream) {
|
||||
return;
|
||||
}
|
||||
if (stream.__write) {
|
||||
stream.write = stream.__write;
|
||||
delete stream.__write;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Pauses logging, queues incoming logs until resumed.
|
||||
*/
|
||||
pauseLogs() {
|
||||
paused = true;
|
||||
}
|
||||
/**
|
||||
* Resumes logging, processing any queued logs.
|
||||
*/
|
||||
resumeLogs() {
|
||||
paused = false;
|
||||
const _queue = queue.splice(0);
|
||||
for (const item of _queue) {
|
||||
item[0]._logFn(item[1], item[2]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Replaces logging methods with mocks if a mock function is provided.
|
||||
*
|
||||
* @param {ConsolaOptions["mockFn"]} mockFn - The function to use for mocking logging methods. See {@link ConsolaOptions["mockFn"]}.
|
||||
*/
|
||||
mockTypes(mockFn) {
|
||||
const _mockFn = mockFn || this.options.mockFn;
|
||||
this._mockFn = _mockFn;
|
||||
if (typeof _mockFn !== "function") {
|
||||
return;
|
||||
}
|
||||
for (const type in this.options.types) {
|
||||
this[type] = _mockFn(type, this.options.types[type]) || this[type];
|
||||
this[type].raw = this[type];
|
||||
}
|
||||
}
|
||||
_wrapLogFn(defaults, isRaw) {
|
||||
return (...args) => {
|
||||
if (paused) {
|
||||
queue.push([this, defaults, args, isRaw]);
|
||||
return;
|
||||
}
|
||||
return this._logFn(defaults, args, isRaw);
|
||||
};
|
||||
}
|
||||
_logFn(defaults, args, isRaw) {
|
||||
if ((defaults.level || 0) > this.level) {
|
||||
return false;
|
||||
}
|
||||
const logObj = {
|
||||
date: /* @__PURE__ */ new Date(),
|
||||
args: [],
|
||||
...defaults,
|
||||
level: _normalizeLogLevel(defaults.level, this.options.types)
|
||||
};
|
||||
if (!isRaw && args.length === 1 && isLogObj(args[0])) {
|
||||
Object.assign(logObj, args[0]);
|
||||
} else {
|
||||
logObj.args = [...args];
|
||||
}
|
||||
if (logObj.message) {
|
||||
logObj.args.unshift(logObj.message);
|
||||
delete logObj.message;
|
||||
}
|
||||
if (logObj.additional) {
|
||||
if (!Array.isArray(logObj.additional)) {
|
||||
logObj.additional = logObj.additional.split("\n");
|
||||
}
|
||||
logObj.args.push("\n" + logObj.additional.join("\n"));
|
||||
delete logObj.additional;
|
||||
}
|
||||
logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
|
||||
logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
|
||||
const resolveLog = (newLog = false) => {
|
||||
const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
|
||||
if (this._lastLog.object && repeated > 0) {
|
||||
const args2 = [...this._lastLog.object.args];
|
||||
if (repeated > 1) {
|
||||
args2.push(`(repeated ${repeated} times)`);
|
||||
}
|
||||
this._log({ ...this._lastLog.object, args: args2 });
|
||||
this._lastLog.count = 1;
|
||||
}
|
||||
if (newLog) {
|
||||
this._lastLog.object = logObj;
|
||||
this._log(logObj);
|
||||
}
|
||||
};
|
||||
clearTimeout(this._lastLog.timeout);
|
||||
const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
|
||||
this._lastLog.time = logObj.date;
|
||||
if (diffTime < this.options.throttle) {
|
||||
try {
|
||||
const serializedLog = JSON.stringify([
|
||||
logObj.type,
|
||||
logObj.tag,
|
||||
logObj.args
|
||||
]);
|
||||
const isSameLog = this._lastLog.serialized === serializedLog;
|
||||
this._lastLog.serialized = serializedLog;
|
||||
if (isSameLog) {
|
||||
this._lastLog.count = (this._lastLog.count || 0) + 1;
|
||||
if (this._lastLog.count > this.options.throttleMin) {
|
||||
this._lastLog.timeout = setTimeout(
|
||||
resolveLog,
|
||||
this.options.throttle
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
resolveLog(true);
|
||||
}
|
||||
_log(logObj) {
|
||||
for (const reporter of this.options.reporters) {
|
||||
reporter.log(logObj, {
|
||||
options: this.options
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
|
||||
if (input === void 0) {
|
||||
return defaultLevel;
|
||||
}
|
||||
if (typeof input === "number") {
|
||||
return input;
|
||||
}
|
||||
if (types[input] && types[input].level !== void 0) {
|
||||
return types[input].level;
|
||||
}
|
||||
return defaultLevel;
|
||||
}
|
||||
Consola.prototype.add = Consola.prototype.addReporter;
|
||||
Consola.prototype.remove = Consola.prototype.removeReporter;
|
||||
Consola.prototype.clear = Consola.prototype.removeReporter;
|
||||
Consola.prototype.withScope = Consola.prototype.withTag;
|
||||
Consola.prototype.mock = Consola.prototype.mockTypes;
|
||||
Consola.prototype.pause = Consola.prototype.pauseLogs;
|
||||
Consola.prototype.resume = Consola.prototype.resumeLogs;
|
||||
function createConsola(options = {}) {
|
||||
return new Consola(options);
|
||||
}
|
||||
|
||||
export { Consola, LogLevels, LogTypes, createConsola };
|
||||
-651
File diff suppressed because one or more lines are too long
-72
@@ -1,72 +0,0 @@
|
||||
import { formatWithOptions } from 'node:util';
|
||||
import { sep } from 'node:path';
|
||||
|
||||
function parseStack(stack, message) {
|
||||
const cwd = process.cwd() + sep;
|
||||
const lines = stack.split("\n").splice(message.split("\n").length).map((l) => l.trim().replace("file://", "").replace(cwd, ""));
|
||||
return lines;
|
||||
}
|
||||
|
||||
function writeStream(data, stream) {
|
||||
const write = stream.__write || stream.write;
|
||||
return write.call(stream, data);
|
||||
}
|
||||
|
||||
const bracket = (x) => x ? `[${x}]` : "";
|
||||
class BasicReporter {
|
||||
formatStack(stack, message, opts) {
|
||||
const indent = " ".repeat((opts?.errorLevel || 0) + 1);
|
||||
return indent + parseStack(stack, message).join(`
|
||||
${indent}`);
|
||||
}
|
||||
formatError(err, opts) {
|
||||
const message = err.message ?? formatWithOptions(opts, err);
|
||||
const stack = err.stack ? this.formatStack(err.stack, message, opts) : "";
|
||||
const level = opts?.errorLevel || 0;
|
||||
const causedPrefix = level > 0 ? `${" ".repeat(level)}[cause]: ` : "";
|
||||
const causedError = err.cause ? "\n\n" + this.formatError(err.cause, { ...opts, errorLevel: level + 1 }) : "";
|
||||
return causedPrefix + message + "\n" + stack + causedError;
|
||||
}
|
||||
formatArgs(args, opts) {
|
||||
const _args = args.map((arg) => {
|
||||
if (arg && typeof arg.stack === "string") {
|
||||
return this.formatError(arg, opts);
|
||||
}
|
||||
return arg;
|
||||
});
|
||||
return formatWithOptions(opts, ..._args);
|
||||
}
|
||||
formatDate(date, opts) {
|
||||
return opts.date ? date.toLocaleTimeString() : "";
|
||||
}
|
||||
filterAndJoin(arr) {
|
||||
return arr.filter(Boolean).join(" ");
|
||||
}
|
||||
formatLogObj(logObj, opts) {
|
||||
const message = this.formatArgs(logObj.args, opts);
|
||||
if (logObj.type === "box") {
|
||||
return "\n" + [
|
||||
bracket(logObj.tag),
|
||||
logObj.title && logObj.title,
|
||||
...message.split("\n")
|
||||
].filter(Boolean).map((l) => " > " + l).join("\n") + "\n";
|
||||
}
|
||||
return this.filterAndJoin([
|
||||
bracket(logObj.type),
|
||||
bracket(logObj.tag),
|
||||
message
|
||||
]);
|
||||
}
|
||||
log(logObj, ctx) {
|
||||
const line = this.formatLogObj(logObj, {
|
||||
columns: ctx.options.stdout.columns || 0,
|
||||
...ctx.options.formatOptions
|
||||
});
|
||||
return writeStream(
|
||||
line + "\n",
|
||||
logObj.level < 2 ? ctx.options.stderr || process.stderr : ctx.options.stdout || process.stdout
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export { BasicReporter as B, parseStack as p };
|
||||
-288
@@ -1,288 +0,0 @@
|
||||
import * as tty from 'node:tty';
|
||||
|
||||
const {
|
||||
env = {},
|
||||
argv = [],
|
||||
platform = ""
|
||||
} = typeof process === "undefined" ? {} : process;
|
||||
const isDisabled = "NO_COLOR" in env || argv.includes("--no-color");
|
||||
const isForced = "FORCE_COLOR" in env || argv.includes("--color");
|
||||
const isWindows = platform === "win32";
|
||||
const isDumbTerminal = env.TERM === "dumb";
|
||||
const isCompatibleTerminal = tty && tty.isatty && tty.isatty(1) && env.TERM && !isDumbTerminal;
|
||||
const isCI = "CI" in env && ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env);
|
||||
const isColorSupported = !isDisabled && (isForced || isWindows && !isDumbTerminal || isCompatibleTerminal || isCI);
|
||||
function replaceClose(index, string, close, replace, head = string.slice(0, Math.max(0, index)) + replace, tail = string.slice(Math.max(0, index + close.length)), next = tail.indexOf(close)) {
|
||||
return head + (next < 0 ? tail : replaceClose(next, tail, close, replace));
|
||||
}
|
||||
function clearBleed(index, string, open, close, replace) {
|
||||
return index < 0 ? open + string + close : open + replaceClose(index, string, close, replace) + close;
|
||||
}
|
||||
function filterEmpty(open, close, replace = open, at = open.length + 1) {
|
||||
return (string) => string || !(string === "" || string === void 0) ? clearBleed(
|
||||
("" + string).indexOf(close, at),
|
||||
string,
|
||||
open,
|
||||
close,
|
||||
replace
|
||||
) : "";
|
||||
}
|
||||
function init(open, close, replace) {
|
||||
return filterEmpty(`\x1B[${open}m`, `\x1B[${close}m`, replace);
|
||||
}
|
||||
const colorDefs = {
|
||||
reset: init(0, 0),
|
||||
bold: init(1, 22, "\x1B[22m\x1B[1m"),
|
||||
dim: init(2, 22, "\x1B[22m\x1B[2m"),
|
||||
italic: init(3, 23),
|
||||
underline: init(4, 24),
|
||||
inverse: init(7, 27),
|
||||
hidden: init(8, 28),
|
||||
strikethrough: init(9, 29),
|
||||
black: init(30, 39),
|
||||
red: init(31, 39),
|
||||
green: init(32, 39),
|
||||
yellow: init(33, 39),
|
||||
blue: init(34, 39),
|
||||
magenta: init(35, 39),
|
||||
cyan: init(36, 39),
|
||||
white: init(37, 39),
|
||||
gray: init(90, 39),
|
||||
bgBlack: init(40, 49),
|
||||
bgRed: init(41, 49),
|
||||
bgGreen: init(42, 49),
|
||||
bgYellow: init(43, 49),
|
||||
bgBlue: init(44, 49),
|
||||
bgMagenta: init(45, 49),
|
||||
bgCyan: init(46, 49),
|
||||
bgWhite: init(47, 49),
|
||||
blackBright: init(90, 39),
|
||||
redBright: init(91, 39),
|
||||
greenBright: init(92, 39),
|
||||
yellowBright: init(93, 39),
|
||||
blueBright: init(94, 39),
|
||||
magentaBright: init(95, 39),
|
||||
cyanBright: init(96, 39),
|
||||
whiteBright: init(97, 39),
|
||||
bgBlackBright: init(100, 49),
|
||||
bgRedBright: init(101, 49),
|
||||
bgGreenBright: init(102, 49),
|
||||
bgYellowBright: init(103, 49),
|
||||
bgBlueBright: init(104, 49),
|
||||
bgMagentaBright: init(105, 49),
|
||||
bgCyanBright: init(106, 49),
|
||||
bgWhiteBright: init(107, 49)
|
||||
};
|
||||
function createColors(useColor = isColorSupported) {
|
||||
return useColor ? colorDefs : Object.fromEntries(Object.keys(colorDefs).map((key) => [key, String]));
|
||||
}
|
||||
const colors = createColors();
|
||||
function getColor(color, fallback = "reset") {
|
||||
return colors[color] || colors[fallback];
|
||||
}
|
||||
function colorize(color, text) {
|
||||
return getColor(color)(text);
|
||||
}
|
||||
|
||||
const ansiRegex = [
|
||||
String.raw`[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)`,
|
||||
String.raw`(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))`
|
||||
].join("|");
|
||||
function stripAnsi(text) {
|
||||
return text.replace(new RegExp(ansiRegex, "g"), "");
|
||||
}
|
||||
function centerAlign(str, len, space = " ") {
|
||||
const free = len - str.length;
|
||||
if (free <= 0) {
|
||||
return str;
|
||||
}
|
||||
const freeLeft = Math.floor(free / 2);
|
||||
let _str = "";
|
||||
for (let i = 0; i < len; i++) {
|
||||
_str += i < freeLeft || i >= freeLeft + str.length ? space : str[i - freeLeft];
|
||||
}
|
||||
return _str;
|
||||
}
|
||||
function rightAlign(str, len, space = " ") {
|
||||
const free = len - str.length;
|
||||
if (free <= 0) {
|
||||
return str;
|
||||
}
|
||||
let _str = "";
|
||||
for (let i = 0; i < len; i++) {
|
||||
_str += i < free ? space : str[i - free];
|
||||
}
|
||||
return _str;
|
||||
}
|
||||
function leftAlign(str, len, space = " ") {
|
||||
let _str = "";
|
||||
for (let i = 0; i < len; i++) {
|
||||
_str += i < str.length ? str[i] : space;
|
||||
}
|
||||
return _str;
|
||||
}
|
||||
function align(alignment, str, len, space = " ") {
|
||||
switch (alignment) {
|
||||
case "left": {
|
||||
return leftAlign(str, len, space);
|
||||
}
|
||||
case "right": {
|
||||
return rightAlign(str, len, space);
|
||||
}
|
||||
case "center": {
|
||||
return centerAlign(str, len, space);
|
||||
}
|
||||
default: {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const boxStylePresets = {
|
||||
solid: {
|
||||
tl: "\u250C",
|
||||
tr: "\u2510",
|
||||
bl: "\u2514",
|
||||
br: "\u2518",
|
||||
h: "\u2500",
|
||||
v: "\u2502"
|
||||
},
|
||||
double: {
|
||||
tl: "\u2554",
|
||||
tr: "\u2557",
|
||||
bl: "\u255A",
|
||||
br: "\u255D",
|
||||
h: "\u2550",
|
||||
v: "\u2551"
|
||||
},
|
||||
doubleSingle: {
|
||||
tl: "\u2553",
|
||||
tr: "\u2556",
|
||||
bl: "\u2559",
|
||||
br: "\u255C",
|
||||
h: "\u2500",
|
||||
v: "\u2551"
|
||||
},
|
||||
doubleSingleRounded: {
|
||||
tl: "\u256D",
|
||||
tr: "\u256E",
|
||||
bl: "\u2570",
|
||||
br: "\u256F",
|
||||
h: "\u2500",
|
||||
v: "\u2551"
|
||||
},
|
||||
singleThick: {
|
||||
tl: "\u250F",
|
||||
tr: "\u2513",
|
||||
bl: "\u2517",
|
||||
br: "\u251B",
|
||||
h: "\u2501",
|
||||
v: "\u2503"
|
||||
},
|
||||
singleDouble: {
|
||||
tl: "\u2552",
|
||||
tr: "\u2555",
|
||||
bl: "\u2558",
|
||||
br: "\u255B",
|
||||
h: "\u2550",
|
||||
v: "\u2502"
|
||||
},
|
||||
singleDoubleRounded: {
|
||||
tl: "\u256D",
|
||||
tr: "\u256E",
|
||||
bl: "\u2570",
|
||||
br: "\u256F",
|
||||
h: "\u2550",
|
||||
v: "\u2502"
|
||||
},
|
||||
rounded: {
|
||||
tl: "\u256D",
|
||||
tr: "\u256E",
|
||||
bl: "\u2570",
|
||||
br: "\u256F",
|
||||
h: "\u2500",
|
||||
v: "\u2502"
|
||||
}
|
||||
};
|
||||
const defaultStyle = {
|
||||
borderColor: "white",
|
||||
borderStyle: "rounded",
|
||||
valign: "center",
|
||||
padding: 2,
|
||||
marginLeft: 1,
|
||||
marginTop: 1,
|
||||
marginBottom: 1
|
||||
};
|
||||
function box(text, _opts = {}) {
|
||||
const opts = {
|
||||
..._opts,
|
||||
style: {
|
||||
...defaultStyle,
|
||||
..._opts.style
|
||||
}
|
||||
};
|
||||
const textLines = text.split("\n");
|
||||
const boxLines = [];
|
||||
const _color = getColor(opts.style.borderColor);
|
||||
const borderStyle = {
|
||||
...typeof opts.style.borderStyle === "string" ? boxStylePresets[opts.style.borderStyle] || boxStylePresets.solid : opts.style.borderStyle
|
||||
};
|
||||
if (_color) {
|
||||
for (const key in borderStyle) {
|
||||
borderStyle[key] = _color(
|
||||
borderStyle[key]
|
||||
);
|
||||
}
|
||||
}
|
||||
const paddingOffset = opts.style.padding % 2 === 0 ? opts.style.padding : opts.style.padding + 1;
|
||||
const height = textLines.length + paddingOffset;
|
||||
const width = Math.max(
|
||||
...textLines.map((line) => stripAnsi(line).length),
|
||||
opts.title ? stripAnsi(opts.title).length : 0
|
||||
) + paddingOffset;
|
||||
const widthOffset = width + paddingOffset;
|
||||
const leftSpace = opts.style.marginLeft > 0 ? " ".repeat(opts.style.marginLeft) : "";
|
||||
if (opts.style.marginTop > 0) {
|
||||
boxLines.push("".repeat(opts.style.marginTop));
|
||||
}
|
||||
if (opts.title) {
|
||||
const title = _color ? _color(opts.title) : opts.title;
|
||||
const left = borderStyle.h.repeat(
|
||||
Math.floor((width - stripAnsi(opts.title).length) / 2)
|
||||
);
|
||||
const right = borderStyle.h.repeat(
|
||||
width - stripAnsi(opts.title).length - stripAnsi(left).length + paddingOffset
|
||||
);
|
||||
boxLines.push(
|
||||
`${leftSpace}${borderStyle.tl}${left}${title}${right}${borderStyle.tr}`
|
||||
);
|
||||
} else {
|
||||
boxLines.push(
|
||||
`${leftSpace}${borderStyle.tl}${borderStyle.h.repeat(widthOffset)}${borderStyle.tr}`
|
||||
);
|
||||
}
|
||||
const valignOffset = opts.style.valign === "center" ? Math.floor((height - textLines.length) / 2) : opts.style.valign === "top" ? height - textLines.length - paddingOffset : height - textLines.length;
|
||||
for (let i = 0; i < height; i++) {
|
||||
if (i < valignOffset || i >= valignOffset + textLines.length) {
|
||||
boxLines.push(
|
||||
`${leftSpace}${borderStyle.v}${" ".repeat(widthOffset)}${borderStyle.v}`
|
||||
);
|
||||
} else {
|
||||
const line = textLines[i - valignOffset];
|
||||
const left = " ".repeat(paddingOffset);
|
||||
const right = " ".repeat(width - stripAnsi(line).length);
|
||||
boxLines.push(
|
||||
`${leftSpace}${borderStyle.v}${left}${line}${right}${borderStyle.v}`
|
||||
);
|
||||
}
|
||||
}
|
||||
boxLines.push(
|
||||
`${leftSpace}${borderStyle.bl}${borderStyle.h.repeat(widthOffset)}${borderStyle.br}`
|
||||
);
|
||||
if (opts.style.marginBottom > 0) {
|
||||
boxLines.push("".repeat(opts.style.marginBottom));
|
||||
}
|
||||
return boxLines.join("\n");
|
||||
}
|
||||
|
||||
export { centerAlign as a, align as b, colorize as c, box as d, colors as e, getColor as g, leftAlign as l, rightAlign as r, stripAnsi as s };
|
||||
-136
@@ -1,136 +0,0 @@
|
||||
{
|
||||
"name": "consola",
|
||||
"version": "3.4.2",
|
||||
"description": "Elegant Console Wrapper",
|
||||
"keywords": [
|
||||
"console",
|
||||
"logger",
|
||||
"reporter",
|
||||
"elegant",
|
||||
"cli",
|
||||
"universal",
|
||||
"unified",
|
||||
"prompt",
|
||||
"clack",
|
||||
"format",
|
||||
"error",
|
||||
"stacktrace"
|
||||
],
|
||||
"repository": "unjs/consola",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"node": {
|
||||
"import": {
|
||||
"types": "./dist/index.d.mts",
|
||||
"default": "./dist/index.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/index.d.cts",
|
||||
"default": "./lib/index.cjs"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"import": {
|
||||
"types": "./dist/browser.d.mts",
|
||||
"default": "./dist/browser.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/browser.d.cts",
|
||||
"default": "./dist/browser.cjs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"./browser": {
|
||||
"import": {
|
||||
"types": "./dist/browser.d.mts",
|
||||
"default": "./dist/browser.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/browser.d.cts",
|
||||
"default": "./dist/browser.cjs"
|
||||
}
|
||||
},
|
||||
"./basic": {
|
||||
"node": {
|
||||
"import": {
|
||||
"types": "./dist/basic.d.mts",
|
||||
"default": "./dist/basic.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/basic.d.cts",
|
||||
"default": "./dist/basic.cjs"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"import": {
|
||||
"types": "./dist/browser.d.mts",
|
||||
"default": "./dist/browser.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/browser.d.cts",
|
||||
"default": "./dist/browser.cjs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"./core": {
|
||||
"import": {
|
||||
"types": "./dist/core.d.mts",
|
||||
"default": "./dist/core.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/core.d.cts",
|
||||
"default": "./dist/core.cjs"
|
||||
}
|
||||
},
|
||||
"./utils": {
|
||||
"import": {
|
||||
"types": "./dist/utils.d.mts",
|
||||
"default": "./dist/utils.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/utils.d.cts",
|
||||
"default": "./dist/utils.cjs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"main": "./lib/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"browser": "./dist/browser.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist",
|
||||
"lib",
|
||||
"*.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "vitest",
|
||||
"lint": "eslint . && prettier -c src examples test",
|
||||
"lint:fix": "eslint . --fix && prettier -w src examples test",
|
||||
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
|
||||
"test": "pnpm lint && pnpm vitest run --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@clack/prompts": "^0.10.0",
|
||||
"@types/node": "^22.13.10",
|
||||
"@vitest/coverage-v8": "^3.0.9",
|
||||
"changelogen": "^0.6.1",
|
||||
"defu": "^6.1.4",
|
||||
"eslint": "^9.22.0",
|
||||
"eslint-config-unjs": "^0.4.2",
|
||||
"is-unicode-supported": "^2.1.0",
|
||||
"prettier": "^3.5.3",
|
||||
"sentencer": "^0.2.1",
|
||||
"std-env": "^3.8.1",
|
||||
"string-width": "^7.2.0",
|
||||
"typescript": "^5.8.2",
|
||||
"unbuild": "^3.5.0",
|
||||
"vitest": "^3.0.9"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^14.18.0 || >=16.10.0"
|
||||
},
|
||||
"packageManager": "pnpm@10.6.3"
|
||||
}
|
||||
Reference in New Issue
Block a user