(
options: SyncOpencodeConfigOptions = {},
)
| 125 | } |
| 126 | |
| 127 | export async function syncOpencodeConfig( |
| 128 | options: SyncOpencodeConfigOptions = {}, |
| 129 | ): Promise<SyncOpencodeConfigResult> { |
| 130 | const { command, dryRun } = options; |
| 131 | const removed: string[] = []; |
| 132 | const targetConfigPath = options.configPath ?? configPath(); |
| 133 | const targetLegacyConfigPath = options.legacyConfigPath ?? legacyConfigPath(); |
| 134 | |
| 135 | if (command) { |
| 136 | const legacyFailure = await preflightLegacyRemoval(targetLegacyConfigPath, 'migration'); |
| 137 | if (legacyFailure) return { ...legacyFailure, dryRun }; |
| 138 | |
| 139 | const currentExisted = existsSync(targetConfigPath); |
| 140 | const wrote = await writeJsonConfig({ |
| 141 | path: targetConfigPath, |
| 142 | keyPath: [...MCP_KEY_PATH], |
| 143 | entry: buildEntry(command), |
| 144 | allowJsonc: true, |
| 145 | requireBackup: true, |
| 146 | refuseSymlink: true, |
| 147 | dryRun, |
| 148 | }); |
| 149 | if (!wrote.ok) { |
| 150 | return { |
| 151 | ok: false, |
| 152 | code: wrote.code, |
| 153 | message: `OpenCode config write failed: ${wrote.message ?? wrote.code}`, |
| 154 | backupPath: wrote.backupPath, |
| 155 | removed, |
| 156 | dryRun, |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | const migrated = await removeJsonConfigEntry({ |
| 161 | path: targetLegacyConfigPath, |
| 162 | keyPath: [...MCP_KEY_PATH], |
| 163 | dryRun, |
| 164 | allowJsonc: true, |
| 165 | requireBackup: true, |
| 166 | refuseSymlink: true, |
| 167 | }); |
| 168 | if (!migrated.ok) { |
| 169 | const rollbackFailure = await rollbackConfig( |
| 170 | targetConfigPath, |
| 171 | currentExisted, |
| 172 | true, |
| 173 | wrote.backupPath, |
| 174 | wrote.writtenContentHash, |
| 175 | dryRun, |
| 176 | ); |
| 177 | return { |
| 178 | ok: false, |
| 179 | code: migrated.code, |
| 180 | message: [ |
| 181 | `legacy OpenCode config migration failed: ${migrated.message ?? migrated.code}`, |
| 182 | rollbackFailure, |
| 183 | ].filter(Boolean).join('; '), |
| 184 | backupPath: wrote.backupPath ?? migrated.backupPath, |
no test coverage detected