(params: {
change: FileChange
resolvedPath: ResolvedProjectPath
fs: CodebuffFileSystem
})
| 73 | } |
| 74 | |
| 75 | async function applyChange(params: { |
| 76 | change: FileChange |
| 77 | resolvedPath: ResolvedProjectPath |
| 78 | fs: CodebuffFileSystem |
| 79 | }): Promise<ApplyChangeResult> { |
| 80 | const { change, resolvedPath, fs } = params |
| 81 | const { content, type } = change |
| 82 | const { fullPath, relativePath } = resolvedPath |
| 83 | |
| 84 | try { |
| 85 | const exists = await fileExists({ filePath: fullPath, fs }) |
| 86 | if (!exists) { |
| 87 | const dirPath = path.dirname(fullPath) |
| 88 | await fs.mkdir(dirPath, { recursive: true }) |
| 89 | } |
| 90 | |
| 91 | if (type === 'file') { |
| 92 | await fs.writeFile(fullPath, content) |
| 93 | } else { |
| 94 | const oldContent = await fs.readFile(fullPath, 'utf-8') |
| 95 | const newContent = applyPatch(oldContent, content) |
| 96 | if (newContent === false) { |
| 97 | return { status: 'patchFailed', file: relativePath, patch: content } |
| 98 | } |
| 99 | await fs.writeFile(fullPath, newContent) |
| 100 | } |
| 101 | |
| 102 | return { status: exists ? 'modified' : 'created', file: relativePath } |
| 103 | } catch (error) { |
| 104 | console.error(`Failed to apply patch to ${relativePath}:`, error, content) |
| 105 | return { status: 'invalid', file: relativePath } |
| 106 | } |
| 107 | } |
no test coverage detected