MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / loadConfigSafely

Function loadConfigSafely

apps/desktop/src/main/vault.ts:490–524  ·  view source on GitHub ↗

Internal: reads the persisted config and reports whether the bytes on * disk were actually readable. Distinguishes three states: * - `{ readable: true, config }` — a valid config was read (primary or backup) * - `{ readable: true, config: defaults }` — neither file exists (first run)

()

Source from the content-addressed store, hash-verified

488 * `readable: false` is the dangerous state: returning defaults here lets a
489 * subsequent `saveConfig` clobber the (recoverable) on-disk vault path. */
490async function loadConfigSafely(): Promise<
491 { readable: true; config: PersistedConfig } | { readable: false }
492> {
493 const target = configPath()
494 const backup = configBackupPath()
495 try {
496 const primary = await readConfigFile(target)
497 if (primary) return { readable: true, config: primary }
498 } catch (err) {
499 // Primary file exists but is unreadable/corrupt. Try the backup before
500 // giving up — losing settings is bad, losing the vault is worse.
501 console.error('Failed to read primary config; trying backup', err)
502 try {
503 const fromBackup = await readConfigFile(backup)
504 if (fromBackup) {
505 console.warn('Restored config from backup after primary read failure')
506 return { readable: true, config: fromBackup }
507 }
508 } catch (backupErr) {
509 console.error('Backup config also unreadable', backupErr)
510 }
511 return { readable: false }
512 }
513 // Primary missing or empty. Try backup as a last resort (e.g. crash mid-rename).
514 try {
515 const fromBackup = await readConfigFile(backup)
516 if (fromBackup) {
517 console.warn('Primary config missing; restored from backup')
518 return { readable: true, config: fromBackup }
519 }
520 } catch (backupErr) {
521 console.error('Backup config unreadable', backupErr)
522 }
523 return { readable: true, config: { ...DEFAULT_CONFIG } }
524}
525
526/** Reads the persisted config, returning defaults if nothing readable is on
527 * disk. Read callers (createWindow, listLocalVaults, etc.) tolerate a stale

Callers 2

loadConfigFunction · 0.85
updateConfigFunction · 0.85

Calls 3

configPathFunction · 0.85
configBackupPathFunction · 0.85
readConfigFileFunction · 0.70

Tested by

no test coverage detected