(
filePath: string,
content: string,
startMarker: string,
endMarker: string
)
| 238 | } |
| 239 | |
| 240 | static async updateFileWithMarkers( |
| 241 | filePath: string, |
| 242 | content: string, |
| 243 | startMarker: string, |
| 244 | endMarker: string |
| 245 | ): Promise<void> { |
| 246 | let existingContent = ''; |
| 247 | |
| 248 | if (await this.fileExists(filePath)) { |
| 249 | existingContent = await this.readFile(filePath); |
| 250 | |
| 251 | const startIndex = findMarkerIndex(existingContent, startMarker); |
| 252 | const endIndex = startIndex !== -1 |
| 253 | ? findMarkerIndex(existingContent, endMarker, startIndex + startMarker.length) |
| 254 | : findMarkerIndex(existingContent, endMarker); |
| 255 | |
| 256 | if (startIndex !== -1 && endIndex !== -1) { |
| 257 | if (endIndex < startIndex) { |
| 258 | throw new Error( |
| 259 | `Invalid marker state in ${filePath}. End marker appears before start marker.` |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | const before = existingContent.substring(0, startIndex); |
| 264 | const after = existingContent.substring(endIndex + endMarker.length); |
| 265 | existingContent = before + startMarker + '\n' + content + '\n' + endMarker + after; |
| 266 | } else if (startIndex === -1 && endIndex === -1) { |
| 267 | existingContent = startMarker + '\n' + content + '\n' + endMarker + '\n\n' + existingContent; |
| 268 | } else { |
| 269 | throw new Error(`Invalid marker state in ${filePath}. Found start: ${startIndex !== -1}, Found end: ${endIndex !== -1}`); |
| 270 | } |
| 271 | } else { |
| 272 | existingContent = startMarker + '\n' + content + '\n' + endMarker; |
| 273 | } |
| 274 | |
| 275 | await this.writeFile(filePath, existingContent); |
| 276 | } |
| 277 | |
| 278 | static async ensureWritePermissions(dirPath: string): Promise<boolean> { |
| 279 | try { |
no test coverage detected