* Generic helper for cleaning up old files in a single directory * @param dirPath Path to the directory to clean * @param extension File extension to filter (e.g., '.md', '.jsonl') * @param removeEmptyDir Whether to remove the directory if empty after cleanup
( dirPath: string, extension: string, removeEmptyDir: boolean = true, )
| 264 | * @param removeEmptyDir Whether to remove the directory if empty after cleanup |
| 265 | */ |
| 266 | async function cleanupSingleDirectory( |
| 267 | dirPath: string, |
| 268 | extension: string, |
| 269 | removeEmptyDir: boolean = true, |
| 270 | ): Promise<CleanupResult> { |
| 271 | const cutoffDate = getCutoffDate() |
| 272 | const result: CleanupResult = { messages: 0, errors: 0 } |
| 273 | const fsImpl = getFsImplementation() |
| 274 | |
| 275 | let dirents |
| 276 | try { |
| 277 | dirents = await fsImpl.readdir(dirPath) |
| 278 | } catch { |
| 279 | return result |
| 280 | } |
| 281 | |
| 282 | for (const dirent of dirents) { |
| 283 | if (!dirent.isFile() || !dirent.name.endsWith(extension)) continue |
| 284 | try { |
| 285 | if (await unlinkIfOld(join(dirPath, dirent.name), cutoffDate, fsImpl)) { |
| 286 | result.messages++ |
| 287 | } |
| 288 | } catch { |
| 289 | result.errors++ |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (removeEmptyDir) { |
| 294 | await tryRmdir(dirPath, fsImpl) |
| 295 | } |
| 296 | |
| 297 | return result |
| 298 | } |
| 299 | |
| 300 | export function cleanupOldPlanFiles(): Promise<CleanupResult> { |
| 301 | const plansDir = join(getClaudeConfigHomeDir(), 'plans') |
no test coverage detected