| 361 | } |
| 362 | |
| 363 | private resetFile(file: keyof typeof FileLocation): void { |
| 364 | const backupIndexPath = `${this.indexPath}.bak`; |
| 365 | try { |
| 366 | // Backup the original index file |
| 367 | const backupContent = fs.readFileSync(this.indexPath, 'utf-8'); |
| 368 | fs.writeFileSync(backupIndexPath, backupContent, 'utf-8'); |
| 369 | this.removeIndexEntriesByFile(file); |
| 370 | } catch (e) { |
| 371 | // Rollback by restoring the backup |
| 372 | const backupContent = fs.readFileSync(backupIndexPath, 'utf-8'); |
| 373 | fs.writeFileSync(this.indexPath, backupContent, 'utf-8'); |
| 374 | fs.rmSync(backupIndexPath); |
| 375 | throw new Error(`Error during index removal: ${e}`); |
| 376 | } |
| 377 | // Only remove file when index is successful removed |
| 378 | fs.rmSync(this.getFilePath(file)); |
| 379 | fs.rmSync(backupIndexPath); |
| 380 | this.createOrResetFile(this.getFilePath(file)); |
| 381 | } |
| 382 | |
| 383 | private getFileSize(file: keyof typeof FileLocation): number { |
| 384 | const filePath = this.getFilePath(file); |