UNPKG

route-claudecode

Version:

Advanced routing and transformation system for Claude Code outputs to multiple AI providers

153 lines 6.37 kB
"use strict"; /** * Configuration Migration Utility * Migrates configurations and logs from legacy to new path * Project owner: Jason Zhang */ Object.defineProperty(exports, "__esModule", { value: true }); exports.migrateConfiguration = migrateConfiguration; exports.removeLegacyConfiguration = removeLegacyConfiguration; exports.backupLegacyConfiguration = backupLegacyConfiguration; const fs_1 = require("fs"); const path_1 = require("path"); const config_paths_1 = require("./config-paths"); const logger_1 = require("./logger"); /** * Migrate all configurations and logs from legacy to new path */ async function migrateConfiguration() { const result = { success: false, filesTransferred: 0, errors: [] }; try { const legacyPaths = (0, config_paths_1.getLegacyConfigPaths)(); const newPaths = (0, config_paths_1.getNewConfigPaths)(); // Check if legacy directory exists if (!(0, fs_1.existsSync)(legacyPaths.configDir)) { result.errors.push('Legacy configuration directory does not exist'); return result; } // Check if new directory already exists if ((0, fs_1.existsSync)(newPaths.configDir)) { result.errors.push('New configuration directory already exists'); return result; } // Create new configuration directory (0, fs_1.mkdirSync)(newPaths.configDir, { recursive: true }); logger_1.logger.info('Created new configuration directory', { path: newPaths.configDir }); // Migrate all files and directories await copyDirectoryRecursive(legacyPaths.configDir, newPaths.configDir, result); // Mark migration as successful if no errors occurred during copying result.success = result.errors.length === 0; if (result.success) { logger_1.logger.info('Configuration migration completed successfully', { filesTransferred: result.filesTransferred, fromPath: legacyPaths.configDir, toPath: newPaths.configDir }); } else { logger_1.logger.error('Configuration migration completed with errors', { filesTransferred: result.filesTransferred, errors: result.errors }); } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); result.errors.push(`Migration failed: ${errorMessage}`); logger_1.logger.error('Configuration migration failed', { error: errorMessage }); } return result; } /** * Recursively copy directory contents */ async function copyDirectoryRecursive(sourceDir, targetDir, result) { try { const items = (0, fs_1.readdirSync)(sourceDir); for (const item of items) { const sourcePath = (0, path_1.join)(sourceDir, item); const targetPath = (0, path_1.join)(targetDir, item); const stats = (0, fs_1.statSync)(sourcePath); if (stats.isDirectory()) { // Create target directory and copy recursively (0, fs_1.mkdirSync)(targetPath, { recursive: true }); await copyDirectoryRecursive(sourcePath, targetPath, result); } else if (stats.isFile()) { // Copy file try { (0, fs_1.copyFileSync)(sourcePath, targetPath); result.filesTransferred++; logger_1.logger.debug('Migrated file', { from: sourcePath, to: targetPath }); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); result.errors.push(`Failed to copy ${sourcePath}: ${errorMessage}`); } } } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); result.errors.push(`Failed to read directory ${sourceDir}: ${errorMessage}`); } } /** * Remove legacy configuration directory after successful migration * WARNING: This permanently deletes the legacy directory */ async function removeLegacyConfiguration() { try { const legacyPaths = (0, config_paths_1.getLegacyConfigPaths)(); if (!(0, fs_1.existsSync)(legacyPaths.configDir)) { logger_1.logger.info('Legacy configuration directory does not exist, nothing to remove'); return true; } (0, fs_1.rmSync)(legacyPaths.configDir, { recursive: true, force: true }); logger_1.logger.info('Legacy configuration directory removed', { path: legacyPaths.configDir }); return true; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); logger_1.logger.error('Failed to remove legacy configuration directory', { error: errorMessage }); return false; } } /** * Create a backup of legacy configuration before migration */ async function backupLegacyConfiguration() { try { const legacyPaths = (0, config_paths_1.getLegacyConfigPaths)(); if (!(0, fs_1.existsSync)(legacyPaths.configDir)) { return null; } const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const backupPath = `${legacyPaths.configDir}-backup-${timestamp}`; const result = { success: false, filesTransferred: 0, errors: [] }; await copyDirectoryRecursive(legacyPaths.configDir, backupPath, result); // Mark as successful if no errors occurred result.success = result.errors.length === 0; if (result.success) { logger_1.logger.info('Legacy configuration backup created', { backupPath, filesTransferred: result.filesTransferred }); return backupPath; } else { logger_1.logger.error('Failed to create legacy configuration backup', { errors: result.errors }); return null; } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); logger_1.logger.error('Backup creation failed', { error: errorMessage }); return null; } } //# sourceMappingURL=migration.js.map