()
| 80 | } |
| 81 | |
| 82 | export async function readSettings(): Promise<Settings> { |
| 83 | if (!existsSync(configuration.settingsFile)) { |
| 84 | return { ...defaultSettings } |
| 85 | } |
| 86 | |
| 87 | try { |
| 88 | // Read raw settings |
| 89 | const content = await readFile(configuration.settingsFile, 'utf8') |
| 90 | const raw = JSON.parse(content) |
| 91 | |
| 92 | // Check schema version (default to 1 if missing) |
| 93 | const schemaVersion = raw.schemaVersion ?? 1; |
| 94 | |
| 95 | // Warn if schema version is newer than supported |
| 96 | if (schemaVersion > SUPPORTED_SCHEMA_VERSION) { |
| 97 | logger.warn( |
| 98 | `⚠️ Settings schema v${schemaVersion} > supported v${SUPPORTED_SCHEMA_VERSION}. ` + |
| 99 | 'Update happy-cli for full functionality.' |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | // Migrate if needed |
| 104 | const migrated = migrateSettings(raw, schemaVersion); |
| 105 | |
| 106 | if (migrated.sandboxConfig !== undefined) { |
| 107 | try { |
| 108 | migrated.sandboxConfig = SandboxConfigSchema.parse(migrated.sandboxConfig); |
| 109 | } catch (error: any) { |
| 110 | logger.warn(`⚠️ Invalid sandbox config - skipping. Error: ${error.message}`); |
| 111 | migrated.sandboxConfig = undefined; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Merge with defaults to ensure all required fields exist |
| 116 | return { ...defaultSettings, ...migrated }; |
| 117 | } catch (error: any) { |
| 118 | logger.warn(`Failed to read settings: ${error.message}`); |
| 119 | // Return defaults on any error |
| 120 | return { ...defaultSettings } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | export async function writeSettings(settings: Settings): Promise<void> { |
| 125 | if (!existsSync(configuration.happyHomeDir)) { |
no test coverage detected