( dirPath: string, cutoffDate: Date, isMessagePath: boolean, )
| 53 | } |
| 54 | |
| 55 | async function cleanupOldFilesInDirectory( |
| 56 | dirPath: string, |
| 57 | cutoffDate: Date, |
| 58 | isMessagePath: boolean, |
| 59 | ): Promise<CleanupResult> { |
| 60 | const result: CleanupResult = { messages: 0, errors: 0 } |
| 61 | |
| 62 | try { |
| 63 | const files = await getFsImplementation().readdir(dirPath) |
| 64 | |
| 65 | for (const file of files) { |
| 66 | try { |
| 67 | // Convert filename format where all ':.' were replaced with '-' |
| 68 | const timestamp = convertFileNameToDate(file.name) |
| 69 | if (timestamp < cutoffDate) { |
| 70 | await getFsImplementation().unlink(join(dirPath, file.name)) |
| 71 | // Increment the appropriate counter |
| 72 | if (isMessagePath) { |
| 73 | result.messages++ |
| 74 | } else { |
| 75 | result.errors++ |
| 76 | } |
| 77 | } |
| 78 | } catch (error) { |
| 79 | // Log but continue processing other files |
| 80 | logError(error as Error) |
| 81 | } |
| 82 | } |
| 83 | } catch (error: unknown) { |
| 84 | // Ignore if directory doesn't exist |
| 85 | if (error instanceof Error && 'code' in error && error.code !== 'ENOENT') { |
| 86 | logError(error) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return result |
| 91 | } |
| 92 | |
| 93 | export async function cleanupOldMessageFiles(): Promise<CleanupResult> { |
| 94 | const fsImpl = getFsImplementation() |
no test coverage detected