UNPKG

worktree-tool

Version:

A command-line tool for managing Git worktrees with integrated tmux/shell session management

126 lines 3.5 kB
import chalk from "chalk"; /** * Logger class for handling different levels of output. * Supports quiet, normal, and verbose modes. */ export class Logger { level; /** * Creates a new Logger instance. * * @param options - Global options to determine log level */ constructor(options) { if (options?.quiet) { this.level = "quiet"; } else if (options?.verbose) { this.level = "verbose"; } else { this.level = "normal"; } } /** * Log an error message (always shown) */ error(message) { console.error(chalk.red(`✗ ${message}`)); } /** * Log a success message (shown in normal and verbose modes) */ success(message) { if (this.level !== "quiet") { // eslint-disable-next-line no-console console.log(chalk.green(`✓ ${message}`)); } } /** * Log an info message (shown in normal and verbose modes) */ info(message) { if (this.level !== "quiet") { // eslint-disable-next-line no-console console.log(chalk.blue(`ℹ ${message}`)); } } /** * Log a warning message (shown in normal and verbose modes) */ warn(message) { if (this.level !== "quiet") { console.warn(chalk.yellow(`⚠ ${message}`)); } } /** * Log a verbose message (only shown in verbose mode) */ verbose(message) { if (this.level === "verbose") { // eslint-disable-next-line no-console console.log(chalk.gray(`• ${message}`)); } } /** * Log a plain message (shown in normal and verbose modes) */ log(message) { if (this.level !== "quiet") { // eslint-disable-next-line no-console console.log(message); } } /** * Start a progress indicator (shown in normal and verbose modes) * @returns A function to stop the progress indicator */ progress(message) { if (this.level === "quiet") { // eslint-disable-next-line @typescript-eslint/no-empty-function return () => { }; } const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; let i = 0; const interval = setInterval(() => { process.stdout.write(`\r${chalk.cyan(frames[i])} ${message}`); i = (i + 1) % frames.length; }, 80); return () => { clearInterval(interval); process.stdout.write(`\r${" ".repeat(message.length + 3)}\r`); }; } /** * Get the current log level */ getLevel() { return this.level; } } // Singleton instance let instance; /** * Get or create the logger instance. * Uses singleton pattern to ensure consistent logging throughout the application. * * @param options - Global options to configure the logger * @returns The Logger instance * * @example * ```typescript * const logger = getLogger({ verbose: true }); * logger.info("This is a verbose message"); * * const quietLogger = getLogger({ quiet: true }); * quietLogger.error("Only errors are shown in quiet mode"); * ``` */ export function getLogger(options) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!instance || options) { instance = new Logger(options); } return instance; } //# sourceMappingURL=logger.js.map