UNPKG

package-versioner

Version:

A lightweight yet powerful CLI tool for automated semantic versioning based on Git history and conventional commits.

123 lines (118 loc) 2.92 kB
// src/utils/logging.ts import chalk from "chalk"; import figlet from "figlet"; // src/utils/jsonOutput.ts var _jsonOutputMode = false; var _jsonData = { dryRun: false, updates: [], tags: [] }; function enableJsonOutput(dryRun = false) { _jsonOutputMode = true; _jsonData.dryRun = dryRun; _jsonData.updates = []; _jsonData.tags = []; _jsonData.commitMessage = void 0; } function isJsonOutputMode() { return _jsonOutputMode; } function addPackageUpdate(packageName, newVersion, filePath) { if (!_jsonOutputMode) return; _jsonData.updates.push({ packageName, newVersion, filePath }); } function addTag(tag) { if (!_jsonOutputMode) return; _jsonData.tags.push(tag); } function setCommitMessage(message) { if (!_jsonOutputMode) return; _jsonData.commitMessage = message; } function printJsonOutput() { if (_jsonOutputMode) { console.log(JSON.stringify(_jsonData, null, 2)); } } // src/utils/logging.ts function log(message, level = "info") { const showDebug = process.env.DEBUG === "true" || process.env.DEBUG === "1"; if (level === "debug" && !showDebug) { return; } let chalkFn; switch (level) { case "success": chalkFn = chalk.green; break; case "warning": chalkFn = chalk.yellow; break; case "error": chalkFn = chalk.red; break; case "debug": chalkFn = chalk.gray; break; default: chalkFn = chalk.blue; } if (isJsonOutputMode()) { if (level === "error") { chalkFn(message); console.error(message); } return; } const formattedMessage = level === "debug" ? `[DEBUG] ${message}` : message; if (level === "error") { console.error(chalkFn(formattedMessage)); } else { console.log(chalkFn(formattedMessage)); } } // src/errors/baseError.ts var BasePackageVersionerError = class _BasePackageVersionerError extends Error { constructor(message, code, suggestions) { super(message); this.code = code; this.suggestions = suggestions; this.name = this.constructor.name; } /** * Log the error with consistent formatting and optional suggestions * This centralizes all error output formatting and behavior */ logError() { var _a; log(this.message, "error"); if ((_a = this.suggestions) == null ? void 0 : _a.length) { log("\nSuggested solutions:", "info"); this.suggestions.forEach((suggestion, i) => { log(`${i + 1}. ${suggestion}`, "info"); }); } } /** * Type guard to check if an error is a package-versioner error * @param error Error to check * @returns true if error is a BasePackageVersionerError */ static isPackageVersionerError(error) { return error instanceof _BasePackageVersionerError; } }; export { enableJsonOutput, addPackageUpdate, addTag, setCommitMessage, printJsonOutput, log, BasePackageVersionerError };