(loc: Location)
| 211 | } |
| 212 | |
| 213 | function writeMcpEntry(loc: Location): WriteResult['files'][number] { |
| 214 | const file = configPath(loc); |
| 215 | const existed = fs.existsSync(file); |
| 216 | let text = readConfigText(file); |
| 217 | |
| 218 | // Seed a minimal opencode config when the file is brand-new so |
| 219 | // the result is a complete, schema-tagged file (not just a bare |
| 220 | // `{ "mcp": {...} }`). |
| 221 | if (!text.trim()) { |
| 222 | text = '{\n "$schema": "https://opencode.ai/config.json"\n}\n'; |
| 223 | } |
| 224 | |
| 225 | const config = parseConfig(text); |
| 226 | const before = config.mcp?.servers?.codegraph; |
| 227 | const after = getOpencodeServerEntry(); |
| 228 | const hasLegacy = !!config.mcp?.codegraph; |
| 229 | |
| 230 | // Native entry already matches and no v1 leftover → nothing to do. |
| 231 | if (jsonDeepEqual(before, after) && !hasLegacy) { |
| 232 | return { path: file, action: 'unchanged' }; |
| 233 | } |
| 234 | |
| 235 | // Add $schema if the user's existing file is missing it. |
| 236 | if (!config.$schema) { |
| 237 | const schemaEdits = modify(text, ['$schema'], 'https://opencode.ai/config.json', { |
| 238 | formattingOptions: FORMATTING, |
| 239 | }); |
| 240 | text = applyEdits(text, schemaEdits); |
| 241 | } |
| 242 | |
| 243 | // Migrate pre-#1698 `mcp.codegraph` (+ enabled) off the file so OpenCode 2 |
| 244 | // keeps only the native entry where `codemode` survives normalization. |
| 245 | if (hasLegacy) { |
| 246 | const legacyEdits = modify(text, ['mcp', 'codegraph'], undefined, { |
| 247 | formattingOptions: FORMATTING, |
| 248 | }); |
| 249 | text = applyEdits(text, legacyEdits); |
| 250 | } |
| 251 | |
| 252 | // Surgical edit — preserves comments, formatting, and order of |
| 253 | // every key we don't touch. |
| 254 | const edits = modify(text, ['mcp', 'servers', 'codegraph'], after, { |
| 255 | formattingOptions: FORMATTING, |
| 256 | }); |
| 257 | const updated = applyEdits(text, edits); |
| 258 | atomicWriteFileSync(file, updated); |
| 259 | |
| 260 | return { path: file, action: existed ? 'updated' : 'created' }; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Surgically drop our CodeGraph entry from one config file — either the |
no test coverage detected