@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
85 lines (84 loc) • 3.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
class Logger {
constructor() {
this.setLevel = (level) => this._level = level;
this.log = (message, level) => {
const myLevel = level !== null && level !== void 0 ? level : this._level;
if (myLevel !== 'debug')
return;
console.log(chalk_1.default.cyanBright('DEBUG'), message);
};
this.info = (message) => {
console.info(message);
};
this.error = (error) => {
console.error(error);
};
this.formatObject = (obj, depth = 0) => {
if (obj === null || obj === undefined)
return chalk_1.default.gray(String(obj));
if (typeof obj !== 'object')
return String(obj);
if (Array.isArray(obj))
return this.formatList(obj);
try {
const keys = Object.keys(obj);
if (keys.length === 0)
return chalk_1.default.gray('{}');
const indent = ' '.repeat(depth);
// Calculate column widths
const keyWidth = Math.max(...keys.map(key => key.length), 8) + 2;
const valueWidth = 40;
const rows = keys.map(key => {
const value = obj[key];
const formattedKey = chalk_1.default.yellow(key.padEnd(keyWidth));
if (value === null || value === undefined) {
const formattedValue = chalk_1.default.gray(String(value));
return `${indent} ${formattedKey} : ${formattedValue}`;
}
if (typeof value === 'object' && !Array.isArray(value)) {
const nestedObject = this.formatObject(value, depth + 1);
const lines = nestedObject.split('\n');
const firstLine = `${indent} ${formattedKey} : ${''.padEnd(valueWidth)}`;
const nestedLines = lines.map(line => `${indent} ${''.padEnd(keyWidth)} : ${line.padEnd(valueWidth)}`);
return [firstLine, ...nestedLines].join('\n');
}
if (Array.isArray(value)) {
const arrayData = this.formatList(value);
const nested = `${arrayData}`;
return `${indent} ${formattedKey} : ${nested}`;
}
const formattedValue = String(value).padEnd(valueWidth);
return `${indent} ${formattedKey} : ${formattedValue}`;
});
const result = rows.join('\n');
return result;
}
catch (error) {
return chalk_1.default.red('Error formatting object: ') + String(error) + '\n' + String(obj);
}
};
this.formatList = (list) => {
if (!Array.isArray(list)) {
return chalk_1.default.yellow('Warning: formatList called with non-array') + '\n' + this.formatObject(list);
}
const header = chalk_1.default.blue(`Array (${list.length} items):`);
if (list.length === 0)
return header;
const items = list.map((item, index) => {
const formattedItem = this.formatObject(item);
const indentedItem = formattedItem.split('\n').map(line => ' ' + line).join('\n');
return chalk_1.default.cyan(` [${index}]:`) + '\n' + indentedItem;
});
return header + '\n' + items.join('\n');
};
this._level = 'prod';
}
}
const logger = new Logger();
exports.default = logger;