(entries: MigrationEntry[])
| 353 | * Returns the new entries plus the entries that were skipped (with a reason). |
| 354 | */ |
| 355 | export function applyMigration(entries: MigrationEntry[]): MigrationResult { |
| 356 | const filePath = path.join(getConfigDir(), "settings.json"); |
| 357 | const existing = readJson(filePath) ?? {}; |
| 358 | const servers = isPlainObject(existing.mcpServers) |
| 359 | ? { ...(existing.mcpServers as Record<string, McpServerConfig>) } |
| 360 | : {}; |
| 361 | |
| 362 | const added: MigrationEntry[] = []; |
| 363 | const skipped: { entry: MigrationEntry; reason: string }[] = []; |
| 364 | |
| 365 | for (const entry of entries) { |
| 366 | if (entry.name in servers) { |
| 367 | skipped.push({ |
| 368 | entry, |
| 369 | reason: "already exists in ~/.orbcode/settings.json", |
| 370 | }); |
| 371 | continue; |
| 372 | } |
| 373 | if (destinationHas(entry.name)) { |
| 374 | // Race-safe double-check: the in-memory snapshot is the source of |
| 375 | // truth at write time, so if it changed between our read and now |
| 376 | // we still skip. |
| 377 | skipped.push({ |
| 378 | entry, |
| 379 | reason: "already exists in ~/.orbcode/settings.json", |
| 380 | }); |
| 381 | continue; |
| 382 | } |
| 383 | servers[entry.name] = entry.config; |
| 384 | added.push(entry); |
| 385 | } |
| 386 | |
| 387 | if (added.length > 0) { |
| 388 | existing.mcpServers = servers; |
| 389 | fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 390 | fs.writeFileSync(filePath, JSON.stringify(existing, null, "\t") + "\n", { |
| 391 | mode: 0o600, |
| 392 | }); |
| 393 | } |
| 394 | |
| 395 | return { added, skipped }; |
| 396 | } |
no test coverage detected