|
| 1 | +import { Logger } from '@nestjs/common'; |
| 2 | +import { readFile, writeFile, access } from 'fs/promises'; |
| 3 | +import { constants } from 'fs'; |
| 4 | +import { join, dirname } from 'path'; |
| 5 | +import { applyPatch, parsePatch, reversePatch } from 'diff'; |
| 6 | + |
| 7 | +export interface PatchResult { |
| 8 | + targetFile: string; |
| 9 | + patch: string; |
| 10 | +} |
| 11 | + |
| 12 | +export interface ShouldApplyWithReason { |
| 13 | + shouldApply: boolean; |
| 14 | + reason: string; |
| 15 | +} |
| 16 | + |
| 17 | +// Convert interface to abstract class with default implementations |
| 18 | +export abstract class FileModification { |
| 19 | + abstract id: string; |
| 20 | + |
| 21 | + protected constructor(protected readonly logger: Logger) {} |
| 22 | + |
| 23 | + // This is the main method that child classes need to implement |
| 24 | + protected abstract generatePatch(): Promise<PatchResult>; |
| 25 | + |
| 26 | + private getPatchFilePath(targetFile: string): string { |
| 27 | + const dir = dirname(targetFile); |
| 28 | + const filename = `${this.id}.patch`; |
| 29 | + return join(dir, filename); |
| 30 | + } |
| 31 | + |
| 32 | + private async savePatch(patchResult: PatchResult): Promise<void> { |
| 33 | + const patchFile = this.getPatchFilePath(patchResult.targetFile); |
| 34 | + await writeFile(patchFile, patchResult.patch, 'utf8'); |
| 35 | + } |
| 36 | + |
| 37 | + private async loadSavedPatch(targetFile: string): Promise<string | null> { |
| 38 | + const patchFile = this.getPatchFilePath(targetFile); |
| 39 | + try { |
| 40 | + await access(patchFile, constants.R_OK); |
| 41 | + return await readFile(patchFile, 'utf8'); |
| 42 | + } catch { |
| 43 | + return null; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Default implementation of apply that uses the patch |
| 48 | + async apply(): Promise<void> { |
| 49 | + const patchResult = await this.generatePatch(); |
| 50 | + const { targetFile, patch } = patchResult; |
| 51 | + const currentContent = await readFile(targetFile, 'utf8'); |
| 52 | + const parsedPatch = parsePatch(patch)[0]; |
| 53 | + |
| 54 | + const results = applyPatch(currentContent, parsedPatch); |
| 55 | + if (results === false) { |
| 56 | + throw new Error(`Failed to apply patch to ${targetFile}`); |
| 57 | + } |
| 58 | + |
| 59 | + await writeFile(targetFile, results); |
| 60 | + await this.savePatch(patchResult); |
| 61 | + } |
| 62 | + |
| 63 | + // Update rollback to use the shared utility |
| 64 | + async rollback(): Promise<void> { |
| 65 | + const { targetFile } = await this.generatePatch(); |
| 66 | + let patch: string; |
| 67 | + |
| 68 | + // Try to load saved patch first |
| 69 | + const savedPatch = await this.loadSavedPatch(targetFile); |
| 70 | + if (savedPatch) { |
| 71 | + this.logger.debug(`Using saved patch file for ${this.id}`); |
| 72 | + patch = savedPatch; |
| 73 | + } else { |
| 74 | + this.logger.debug(`No saved patch found for ${this.id}, generating new patch`); |
| 75 | + const patchResult = await this.generatePatch(); |
| 76 | + patch = patchResult.patch; |
| 77 | + } |
| 78 | + |
| 79 | + const currentContent = await readFile(targetFile, 'utf8'); |
| 80 | + const parsedPatch = parsePatch(patch)[0]; |
| 81 | + |
| 82 | + if (!parsedPatch || !parsedPatch.hunks || parsedPatch.hunks.length === 0) { |
| 83 | + throw new Error('Invalid or empty patch content'); |
| 84 | + } |
| 85 | + |
| 86 | + const reversedPatch = reversePatch(parsedPatch); |
| 87 | + const results = applyPatch(currentContent, reversedPatch); |
| 88 | + |
| 89 | + if (results === false) { |
| 90 | + throw new Error(`Failed to rollback patch from ${targetFile}`); |
| 91 | + } |
| 92 | + |
| 93 | + await writeFile(targetFile, results); |
| 94 | + |
| 95 | + // Clean up the patch file after successful rollback |
| 96 | + try { |
| 97 | + const patchFile = this.getPatchFilePath(targetFile); |
| 98 | + await access(patchFile, constants.W_OK); |
| 99 | + await unlink(patchFile); |
| 100 | + } catch { |
| 101 | + // Ignore errors when trying to delete the patch file |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Default implementation that can be overridden if needed |
| 106 | + async shouldApply(): Promise<ShouldApplyWithReason> { |
| 107 | + return { |
| 108 | + shouldApply: true, |
| 109 | + reason: 'Default behavior is to always apply modifications', |
| 110 | + }; |
| 111 | + } |
| 112 | +} |
0 commit comments