* Find the most recent backup file for a given config file. * Checks ~/.claude/backups/ first, then falls back to the legacy location * (next to the config file) for backwards compatibility. * Returns the full path to the most recent backup, or null if none exist.
(file: string)
| 1378 | * Returns the full path to the most recent backup, or null if none exist. |
| 1379 | */ |
| 1380 | function findMostRecentBackup(file: string): string | null { |
| 1381 | const fs = getFsImplementation() |
| 1382 | const fileBase = basename(file) |
| 1383 | const backupDir = getConfigBackupDir() |
| 1384 | |
| 1385 | // Check the new backup directory first |
| 1386 | try { |
| 1387 | const backups = fs |
| 1388 | .readdirStringSync(backupDir) |
| 1389 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1390 | .sort() |
| 1391 | |
| 1392 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1393 | if (mostRecent) { |
| 1394 | return join(backupDir, mostRecent) |
| 1395 | } |
| 1396 | } catch { |
| 1397 | // Backup dir doesn't exist yet |
| 1398 | } |
| 1399 | |
| 1400 | // Fall back to legacy location (next to the config file) |
| 1401 | const fileDir = dirname(file) |
| 1402 | |
| 1403 | try { |
| 1404 | const backups = fs |
| 1405 | .readdirStringSync(fileDir) |
| 1406 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1407 | .sort() |
| 1408 | |
| 1409 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1410 | if (mostRecent) { |
| 1411 | return join(fileDir, mostRecent) |
| 1412 | } |
| 1413 | |
| 1414 | // Check for legacy backup file (no timestamp) |
| 1415 | const legacyBackup = `${file}.backup` |
| 1416 | try { |
| 1417 | fs.statSync(legacyBackup) |
| 1418 | return legacyBackup |
| 1419 | } catch { |
| 1420 | // Legacy backup doesn't exist |
| 1421 | } |
| 1422 | } catch { |
| 1423 | // Ignore errors reading directory |
| 1424 | } |
| 1425 | |
| 1426 | return null |
| 1427 | } |
| 1428 | |
| 1429 | function getConfig<A>( |
| 1430 | file: string, |
no test coverage detected