UNPKG

@iyulab/oops

Version:

Core SDK for Oops - Safe text file editing with automatic backup

324 lines 13.5 kB
"use strict"; /** * Version management for Oops file tracking * Implements semantic versioning (1.0 → 1.1 → 1.2) with branching support */ 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.VersionManager = void 0; const path = __importStar(require("path")); const file_system_1 = require("./file-system"); const errors_1 = require("./errors"); class VersionManager { workspacePath; constructor(workspacePath) { this.workspacePath = workspacePath; } /** * Initialize version tracking for a file (creates version 1) */ async initializeVersioning(filePath, message) { if (!(await file_system_1.FileSystem.exists(filePath))) { throw new errors_1.FileNotFoundError(filePath); } const versionPath = this.getVersionPath(filePath); await file_system_1.FileSystem.mkdir(versionPath); // Create version 1 const version = '1.0'; const versionInfo = await this.createVersion(filePath, version, message || 'Initial version'); // Initialize version history const history = { filePath, versions: [versionInfo], currentVersion: version, branches: {}, }; await this.saveVersionHistory(filePath, history); return versionInfo; } /** * Create a new version (commit) of the file */ async createCommit(filePath, message) { const history = await this.getVersionHistory(filePath); // Calculate next version number based on current position and branching logic const nextVersion = this.calculateNextVersion(history.currentVersion, history); // Create the new version const versionInfo = await this.createVersion(filePath, nextVersion, message); // Update history history.versions.push(versionInfo); // Handle branching: if we're not at the latest sequential version, create a branch const latestSequentialVersion = this.getLatestSequentialVersion(history.versions); const isCreatingBranch = this.compareVersions(history.currentVersion, latestSequentialVersion) < 0; if (isCreatingBranch) { // Add to branches const parentVersion = history.currentVersion; if (!history.branches[parentVersion]) { history.branches[parentVersion] = []; } history.branches[parentVersion].push(nextVersion); } // Update current version history.currentVersion = nextVersion; await this.saveVersionHistory(filePath, history); return versionInfo; } /** * Checkout a specific version */ async checkout(filePath, version) { const history = await this.getVersionHistory(filePath); const versionInfo = history.versions.find(v => v.version === version); if (!versionInfo) { throw new Error(`Version ${version} not found for file ${filePath}`); } // Restore file content from version const versionFilePath = this.getVersionFilePath(filePath, version); await file_system_1.FileSystem.copyFile(versionFilePath, filePath); // Update current version in history history.currentVersion = version; await this.saveVersionHistory(filePath, history); } /** * Get version history for a file */ async getVersionHistory(filePath) { const historyPath = this.getHistoryPath(filePath); if (!(await file_system_1.FileSystem.exists(historyPath))) { throw new errors_1.FileNotTrackedError(filePath); } try { const historyContent = await file_system_1.FileSystem.readFile(historyPath); const history = JSON.parse(historyContent); // Parse dates history.versions = history.versions.map((v) => ({ ...v, timestamp: new Date(v.timestamp), })); return history; } catch (error) { throw new Error(`Failed to read version history for ${filePath}: ${error}`); } } /** * Get diff between two versions */ async diff(filePath, fromVersion, toVersion) { const history = await this.getVersionHistory(filePath); // Default to current version if toVersion not specified const targetVersion = toVersion || history.currentVersion; const fromFile = this.getVersionFilePath(filePath, fromVersion); const toFile = this.getVersionFilePath(filePath, targetVersion); if (!(await file_system_1.FileSystem.exists(fromFile))) { throw new Error(`Version ${fromVersion} not found`); } if (!(await file_system_1.FileSystem.exists(toFile))) { throw new Error(`Version ${targetVersion} not found`); } // Simple diff implementation (could be enhanced with proper diff algorithm) const fromContent = await file_system_1.FileSystem.readFile(fromFile); const toContent = await file_system_1.FileSystem.readFile(toFile); if (fromContent === toContent) { return 'No differences found'; } return this.generateSimpleDiff(fromContent, toContent, fromVersion, targetVersion); } /** * List all versions for a file */ async listVersions(filePath) { const history = await this.getVersionHistory(filePath); return history.versions.sort((a, b) => this.compareVersions(a.version, b.version)); } /** * Check if file has changes compared to current version */ async hasChanges(filePath) { try { const history = await this.getVersionHistory(filePath); const currentVersionFile = this.getVersionFilePath(filePath, history.currentVersion); if (!(await file_system_1.FileSystem.exists(currentVersionFile))) { return true; // No version file means changes } const currentContent = await file_system_1.FileSystem.readFile(filePath); const versionContent = await file_system_1.FileSystem.readFile(currentVersionFile); return currentContent !== versionContent; } catch { return true; // Error means we assume changes } } /** * Remove version tracking for a file */ async removeVersioning(filePath) { const versionPath = this.getVersionPath(filePath); if (await file_system_1.FileSystem.exists(versionPath)) { const fs = await Promise.resolve().then(() => __importStar(require('fs/promises'))); await fs.rm(versionPath, { recursive: true, force: true }); } } async createVersion(filePath, version, message) { const versionFilePath = this.getVersionFilePath(filePath, version); // Copy current file content to version file await file_system_1.FileSystem.copyFile(filePath, versionFilePath); // Calculate checksum (simple implementation) const content = await file_system_1.FileSystem.readFile(filePath); const checksum = Buffer.from(content).toString('base64').substring(0, 8); return { version, message, timestamp: new Date(), checksum, filePath, }; } /** * Calculate next version number with branching support * Sequential: 1.0 → 1.1 → 1.2 * Branching: 1.1 → 1.1.1 → 1.1.2 (when editing past versions) * Sub-branching: 1.1.1 → 1.1.1.1 → 1.1.1.2 */ calculateNextVersion(currentVersion, history) { const latestSequentialVersion = this.getLatestSequentialVersion(history.versions); // If we're at the latest sequential version, increment normally if (this.compareVersions(currentVersion, latestSequentialVersion) === 0) { return this.incrementSequentialVersion(currentVersion); } // Otherwise, we're creating a branch return this.createBranchVersion(currentVersion, history); } /** * Increment sequential version (1.0 → 1.1, 1.1 → 1.2) */ incrementSequentialVersion(version) { const parts = version.split('.'); const major = parseInt(parts[0]); const minor = parseInt(parts[1]) + 1; return `${major}.${minor}`; } /** * Create a branch version (1.1 → 1.1.1, 1.1.1 → 1.1.1.1) */ createBranchVersion(baseVersion, history) { const existingBranches = history.branches[baseVersion] || []; if (existingBranches.length === 0) { // First branch from this version return `${baseVersion}.1`; } // Find the highest branch number and increment const branchNumbers = existingBranches .filter(v => v.startsWith(baseVersion + '.')) .map(v => { const branchPart = v.substring(baseVersion.length + 1); const firstDot = branchPart.indexOf('.'); const branchNum = firstDot === -1 ? branchPart : branchPart.substring(0, firstDot); return parseInt(branchNum); }) .filter(n => !isNaN(n)); const nextBranchNum = Math.max(...branchNumbers, 0) + 1; return `${baseVersion}.${nextBranchNum}`; } /** * Get the latest sequential version (highest major.minor without branch suffix) */ getLatestSequentialVersion(versions) { const sequentialVersions = versions .map(v => v.version) .filter(v => v.split('.').length === 2) // Only major.minor versions .sort(this.compareVersions.bind(this)); return sequentialVersions[sequentialVersions.length - 1] || '1.0'; } compareVersions(a, b) { const aParts = a.split('.').map(Number); const bParts = b.split('.').map(Number); for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) { const aVal = aParts[i] || 0; const bVal = bParts[i] || 0; if (aVal !== bVal) { return aVal - bVal; } } return 0; } generateSimpleDiff(fromContent, toContent, fromVersion, toVersion) { const fromLines = fromContent.split('\n'); const toLines = toContent.split('\n'); let diff = `diff --git a/${fromVersion} b/${toVersion}\n`; diff += `index ${fromVersion}..${toVersion}\n`; diff += `--- a/${fromVersion}\n`; diff += `+++ b/${toVersion}\n`; // Simple line-by-line comparison const maxLines = Math.max(fromLines.length, toLines.length); let lineNumber = 1; for (let i = 0; i < maxLines; i++) { const fromLine = fromLines[i] || ''; const toLine = toLines[i] || ''; if (fromLine !== toLine) { diff += `@@ -${lineNumber},1 +${lineNumber},1 @@\n`; if (fromLines[i] !== undefined) { diff += `-${fromLine}\n`; } if (toLines[i] !== undefined) { diff += `+${toLine}\n`; } } lineNumber++; } return diff; } getVersionPath(filePath) { const hash = this.hashPath(filePath); return path.join(this.workspacePath, 'versions', hash); } getVersionFilePath(filePath, version) { const versionPath = this.getVersionPath(filePath); return path.join(versionPath, `${version}.txt`); } getHistoryPath(filePath) { const versionPath = this.getVersionPath(filePath); return path.join(versionPath, 'history.json'); } async saveVersionHistory(filePath, history) { const historyPath = this.getHistoryPath(filePath); await file_system_1.FileSystem.writeFile(historyPath, JSON.stringify(history, null, 2)); } hashPath(filePath) { return Buffer.from(filePath).toString('base64').replace(/[/+=]/g, '_'); } } exports.VersionManager = VersionManager; //# sourceMappingURL=version.js.map