UNPKG

revit-cli

Version:

A scalable CLI tool for Revit communication and data manipulation

260 lines 8.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.logger = exports.Logger = void 0; const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); const chalk_1 = __importDefault(require("chalk")); /** * Logger utility for consistent logging across the CLI */ class Logger { constructor(level, logFile) { this.level = 'info'; this.levels = { debug: 0, info: 1, warn: 2, error: 3 }; if (level) { this.setLevel(level); } if (logFile) { this.setLogFile(logFile); } } /** * Set the logging level */ setLevel(level) { if (level in this.levels) { this.level = level; } else { console.warn(`Invalid log level: ${level}. Using 'info' instead.`); this.level = 'info'; } } /** * Set the log file path */ async setLogFile(filePath) { try { await fs.ensureDir(path.dirname(filePath)); this.logFile = filePath; } catch (error) { console.error(`Failed to set log file: ${error}`); } } /** * Check if a message should be logged based on current level */ shouldLog(messageLevel) { return this.levels[messageLevel] >= this.levels[this.level]; } /** * Format a log message with timestamp and level */ formatMessage(level, message, ...args) { const timestamp = new Date().toISOString(); const formattedArgs = args.length > 0 ? ' ' + args.map(arg => typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)).join(' ') : ''; return `[${timestamp}] [${level.toUpperCase()}] ${message}${formattedArgs}`; } /** * Write message to log file if configured */ async writeToFile(message) { if (this.logFile) { try { await fs.appendFile(this.logFile, message + '\n'); } catch (error) { console.error(`Failed to write to log file: ${error}`); } } } /** * Log a debug message */ debug(message, ...args) { if (!this.shouldLog('debug')) return; const formattedMessage = this.formatMessage('debug', message, ...args); console.log(chalk_1.default.gray(formattedMessage)); this.writeToFile(formattedMessage); } /** * Log an info message */ info(message, ...args) { if (!this.shouldLog('info')) return; const formattedMessage = this.formatMessage('info', message, ...args); console.log(chalk_1.default.blue(formattedMessage)); this.writeToFile(formattedMessage); } /** * Log a warning message */ warn(message, ...args) { if (!this.shouldLog('warn')) return; const formattedMessage = this.formatMessage('warn', message, ...args); console.log(chalk_1.default.yellow(formattedMessage)); this.writeToFile(formattedMessage); } /** * Log an error message */ error(message, ...args) { if (!this.shouldLog('error')) return; const formattedMessage = this.formatMessage('error', message, ...args); console.error(chalk_1.default.red(formattedMessage)); this.writeToFile(formattedMessage); } /** * Log a success message (always shown) */ success(message, ...args) { const formattedMessage = this.formatMessage('success', message, ...args); console.log(chalk_1.default.green(formattedMessage)); this.writeToFile(formattedMessage); } /** * Log a message with custom color */ custom(color, level, message, ...args) { const formattedMessage = this.formatMessage(level, message, ...args); const colorFunction = chalk_1.default[color] || chalk_1.default.white; console.log(colorFunction(formattedMessage)); this.writeToFile(formattedMessage); } /** * Create a child logger with a prefix */ child(prefix) { const childLogger = new Logger(this.level, this.logFile); // Override methods to include prefix const originalMethods = ['debug', 'info', 'warn', 'error', 'success']; originalMethods.forEach(method => { const originalMethod = childLogger[method].bind(childLogger); childLogger[method] = (message, ...args) => { originalMethod(`[${prefix}] ${message}`, ...args); }; }); return childLogger; } /** * Log a table of data */ table(data) { if (!this.shouldLog('info')) return; console.table(data); // Also write to file in a readable format if (this.logFile) { const tableString = Array.isArray(data) ? data.map(row => JSON.stringify(row)).join('\n') : JSON.stringify(data, null, 2); const formattedMessage = this.formatMessage('table', tableString); this.writeToFile(formattedMessage); } } /** * Log a progress indicator */ progress(message, current, total) { if (!this.shouldLog('info')) return; const percentage = Math.round((current / total) * 100); const progressBar = '█'.repeat(Math.floor(percentage / 5)) + '░'.repeat(20 - Math.floor(percentage / 5)); const progressMessage = `${message} [${progressBar}] ${percentage}% (${current}/${total})`; // Use \r to overwrite the same line process.stdout.write(`\r${chalk_1.default.cyan(progressMessage)}`); // If complete, add newline if (current === total) { process.stdout.write('\n'); } } /** * Clear the current line (useful after progress) */ clearLine() { process.stdout.write('\r\x1b[K'); } /** * Log a separator line */ separator(char = '-', length = 50) { if (!this.shouldLog('info')) return; const line = char.repeat(length); console.log(chalk_1.default.gray(line)); this.writeToFile(line); } /** * Log a header with decorative formatting */ header(title) { if (!this.shouldLog('info')) return; const line = '='.repeat(title.length + 4); const header = `\n${line}\n ${title} \n${line}\n`; console.log(chalk_1.default.bold.blue(header)); this.writeToFile(header); } /** * Get current log level */ getLevel() { return this.level; } /** * Get log file path */ getLogFile() { return this.logFile; } } exports.Logger = Logger; // Export a default logger instance exports.logger = new Logger(); //# sourceMappingURL=logger.js.map