UNPKG

@iyulab/oops

Version:

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

69 lines 2.86 kB
"use strict"; /** * Diff processing for Oops */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DiffProcessor = void 0; const file_system_1 = require("./file-system"); const errors_1 = require("./errors"); class DiffProcessor { async generateDiff(originalPath, modifiedPath) { if (!(await file_system_1.FileSystem.exists(originalPath))) { throw new errors_1.FileNotFoundError(originalPath); } if (!(await file_system_1.FileSystem.exists(modifiedPath))) { throw new errors_1.FileNotFoundError(modifiedPath); } const originalContent = await file_system_1.FileSystem.readFile(originalPath); const modifiedContent = await file_system_1.FileSystem.readFile(modifiedPath); // Simple diff implementation - TODO: use proper diff algorithm const hasChanges = originalContent !== modifiedContent; if (!hasChanges) { return { hasChanges: false, addedLines: 0, removedLines: 0, modifiedLines: 0, diff: '', }; } const originalLines = originalContent.split('\n'); const modifiedLines = modifiedContent.split('\n'); // Very basic diff counting - TODO: implement proper diff let addedLines = Math.max(0, modifiedLines.length - originalLines.length); const removedLines = Math.max(0, originalLines.length - modifiedLines.length); let modifiedLineCount = 0; // Count modified lines (lines that exist in both but are different) const minLength = Math.min(originalLines.length, modifiedLines.length); for (let i = 0; i < minLength; i++) { if (originalLines[i] !== modifiedLines[i]) { modifiedLineCount++; } } // If content changed but line count is same, treat as added lines if (addedLines === 0 && removedLines === 0 && modifiedLineCount > 0) { addedLines = modifiedLineCount; } return { hasChanges: true, addedLines, removedLines, modifiedLines: modifiedLineCount, diff: `--- ${originalPath}\n+++ ${modifiedPath}\n@@ Lines changed @@\n`, // TODO: implement proper diff format }; } async hasChanges(originalPath, modifiedPath) { const result = await this.generateDiff(originalPath, modifiedPath); return result.hasChanges; } async getStats(originalPath, modifiedPath) { const result = await this.generateDiff(originalPath, modifiedPath); return { added: result.addedLines, removed: result.removedLines, modified: result.modifiedLines, }; } } exports.DiffProcessor = DiffProcessor; //# sourceMappingURL=diff.js.map