UNPKG

pgit-cli

Version:

Private file tracking with dual git repositories

227 lines 8.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GlobalPresetManager = exports.GlobalPresetValidationError = exports.GlobalPresetNotFoundError = exports.GlobalPresetError = void 0; const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const fs_1 = require("fs"); const base_error_1 = require("../errors/base.error"); const logger_service_1 = require("../utils/logger.service"); /** * Global preset management errors */ class GlobalPresetError extends base_error_1.BaseError { constructor() { super(...arguments); this.code = 'GLOBAL_PRESET_ERROR'; this.recoverable = true; } } exports.GlobalPresetError = GlobalPresetError; class GlobalPresetNotFoundError extends base_error_1.BaseError { constructor() { super(...arguments); this.code = 'GLOBAL_PRESET_NOT_FOUND'; this.recoverable = false; } } exports.GlobalPresetNotFoundError = GlobalPresetNotFoundError; class GlobalPresetValidationError extends base_error_1.BaseError { constructor() { super(...arguments); this.code = 'GLOBAL_PRESET_VALIDATION_ERROR'; this.recoverable = true; } } exports.GlobalPresetValidationError = GlobalPresetValidationError; /** * Manager for handling global user-defined presets * These presets are stored in the user's home directory and are available globally */ class GlobalPresetManager { constructor() { this.cachedPresets = null; // Store global presets in ~/.pgit/presets.json this.globalConfigDir = path_1.default.join(os_1.default.homedir(), '.pgit'); this.globalPresetsFile = path_1.default.join(this.globalConfigDir, 'presets.json'); } /** * Get a specific global user preset by name */ getPreset(name) { const presets = this.loadGlobalPresets(); return presets.presets[name]; } /** * Get all global user presets */ getAllPresets() { const presets = this.loadGlobalPresets(); return presets.presets; } /** * Save a global user-defined preset */ savePreset(name, preset) { try { // Validate preset name if (!name || name.trim().length === 0) { throw new GlobalPresetValidationError('Preset name cannot be empty'); } if (name.length > 50) { throw new GlobalPresetValidationError('Preset name too long (max 50 characters)'); } // Ensure global config directory exists this.ensureGlobalConfigDir(); // Load existing presets const globalPresets = this.loadGlobalPresets(); // Add created timestamp if not present const presetToSave = { ...preset, created: preset.created || new Date(), }; // Save the preset globalPresets.presets[name] = presetToSave; // Write to file this.saveGlobalPresets(globalPresets); // Clear cache this.cachedPresets = null; logger_service_1.logger.debug(`Global preset '${name}' saved to ${this.globalPresetsFile}`); } catch (error) { if (error instanceof base_error_1.BaseError) { throw error; } throw new GlobalPresetError(`Failed to save global preset '${name}': ${error}`); } } /** * Remove a global user-defined preset */ removePreset(name) { try { const globalPresets = this.loadGlobalPresets(); if (!globalPresets.presets[name]) { return false; } delete globalPresets.presets[name]; // Write to file this.saveGlobalPresets(globalPresets); // Clear cache this.cachedPresets = null; logger_service_1.logger.debug(`Global preset '${name}' removed from ${this.globalPresetsFile}`); return true; } catch (error) { throw new GlobalPresetError(`Failed to remove global preset '${name}': ${error}`); } } /** * Check if a global preset exists */ presetExists(name) { const presets = this.loadGlobalPresets(); return presets.presets[name] !== undefined; } /** * Update last used timestamp for a global preset */ markPresetUsed(name) { try { const globalPresets = this.loadGlobalPresets(); if (globalPresets.presets[name]) { globalPresets.presets[name].lastUsed = new Date(); this.saveGlobalPresets(globalPresets); // Clear cache this.cachedPresets = null; } } catch (error) { // Don't throw error for timestamp update failures logger_service_1.logger.warn(`Failed to update last used timestamp for global preset '${name}': ${error}`); } } /** * Get the global presets file path */ getPresetsFilePath() { return this.globalPresetsFile; } /** * Load global presets from file */ loadGlobalPresets() { if (this.cachedPresets) { return this.cachedPresets; } try { if (!(0, fs_1.existsSync)(this.globalPresetsFile)) { // Return empty preset structure if file doesn't exist const emptyPresets = { version: '1.0.0', presets: {}, }; this.cachedPresets = emptyPresets; return emptyPresets; } const content = (0, fs_1.readFileSync)(this.globalPresetsFile, 'utf-8'); const presets = JSON.parse(content); // Validate structure if (!presets.version || !presets.presets) { throw new Error('Invalid global presets file structure'); } // Convert date strings back to Date objects for (const preset of Object.values(presets.presets)) { if (preset.created && typeof preset.created === 'string') { preset.created = new Date(preset.created); } if (preset.lastUsed && typeof preset.lastUsed === 'string') { preset.lastUsed = new Date(preset.lastUsed); } } this.cachedPresets = presets; return presets; } catch (error) { logger_service_1.logger.warn(`Failed to load global presets from ${this.globalPresetsFile}: ${error}`); // Return empty preset structure on error const emptyPresets = { version: '1.0.0', presets: {}, }; this.cachedPresets = emptyPresets; return emptyPresets; } } /** * Save global presets to file */ saveGlobalPresets(presets) { try { this.ensureGlobalConfigDir(); const content = JSON.stringify(presets, null, 2); (0, fs_1.writeFileSync)(this.globalPresetsFile, content, 'utf-8'); } catch (error) { throw new GlobalPresetError(`Failed to save global presets to ${this.globalPresetsFile}: ${error}`); } } /** * Ensure global config directory exists */ ensureGlobalConfigDir() { try { if (!(0, fs_1.existsSync)(this.globalConfigDir)) { (0, fs_1.mkdirSync)(this.globalConfigDir, { recursive: true }); logger_service_1.logger.debug(`Created global config directory: ${this.globalConfigDir}`); } } catch (error) { throw new GlobalPresetError(`Failed to create global config directory ${this.globalConfigDir}: ${error}`); } } } exports.GlobalPresetManager = GlobalPresetManager; //# sourceMappingURL=global-preset.manager.js.map