* Find the most recent backup file for a given config file. * Checks ~/.ncode/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)
| 1444 | * Returns the full path to the most recent backup, or null if none exist. |
| 1445 | */ |
| 1446 | function findMostRecentBackup(file: string): string | null { |
| 1447 | const fs = getFsImplementation() |
| 1448 | const fileBase = basename(file) |
| 1449 | const backupDir = getConfigBackupDir() |
| 1450 | |
| 1451 | // Check the new backup directory first |
| 1452 | try { |
| 1453 | const backups = fs |
| 1454 | .readdirStringSync(backupDir) |
| 1455 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1456 | .sort() |
| 1457 | |
| 1458 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1459 | if (mostRecent) { |
| 1460 | return join(backupDir, mostRecent) |
| 1461 | } |
| 1462 | } catch { |
| 1463 | // Backup dir doesn't exist yet |
| 1464 | } |
| 1465 | |
| 1466 | // Fall back to legacy location (next to the config file) |
| 1467 | const fileDir = dirname(file) |
| 1468 | |
| 1469 | try { |
| 1470 | const backups = fs |
| 1471 | .readdirStringSync(fileDir) |
| 1472 | .filter(f => f.startsWith(`${fileBase}.backup.`)) |
| 1473 | .sort() |
| 1474 | |
| 1475 | const mostRecent = backups.at(-1) // Timestamps sort lexicographically |
| 1476 | if (mostRecent) { |
| 1477 | return join(fileDir, mostRecent) |
| 1478 | } |
| 1479 | |
| 1480 | // Check for legacy backup file (no timestamp) |
| 1481 | const legacyBackup = `${file}.backup` |
| 1482 | try { |
| 1483 | fs.statSync(legacyBackup) |
| 1484 | return legacyBackup |
| 1485 | } catch { |
| 1486 | // Legacy backup doesn't exist |
| 1487 | } |
| 1488 | } catch { |
| 1489 | // Ignore errors reading directory |
| 1490 | } |
| 1491 | |
| 1492 | return null |
| 1493 | } |
| 1494 | |
| 1495 | function getConfig<A>( |
| 1496 | file: string, |
no test coverage detected