( file: string, entry: Record<string, unknown>, alsoEdit: Edit = noEdit, )
| 76 | * this file, so the file is written once and reported once. |
| 77 | */ |
| 78 | export function upsertCodegraphServer( |
| 79 | file: string, |
| 80 | entry: Record<string, unknown>, |
| 81 | alsoEdit: Edit = noEdit, |
| 82 | ): FileResult { |
| 83 | const existed = fs.existsSync(file); |
| 84 | const before = existed ? fs.readFileSync(file, 'utf-8') : ''; |
| 85 | const { config, valid } = parseConfig(before); |
| 86 | const servers: unknown = config.mcpServers; |
| 87 | if (servers !== undefined && !isObject(servers)) { |
| 88 | throw new Error( |
| 89 | `${file} sets mcpServers to something other than an object, so CodeGraph can't add its server. ` + |
| 90 | 'Nothing was changed: make mcpServers an object, then run this again.', |
| 91 | ); |
| 92 | } |
| 93 | if (!valid) backupUnparseableJson(file); |
| 94 | |
| 95 | let text = valid && before.trim() !== '' ? before : '{\n}\n'; |
| 96 | const current: unknown = servers?.codegraph; |
| 97 | if (isObject(current)) { |
| 98 | for (const [key, value] of Object.entries(entry)) { |
| 99 | if (!(key in current)) text = editJson(text, [...SERVER_PATH, key], value); |
| 100 | } |
| 101 | } else { |
| 102 | text = editJson(text, SERVER_PATH, entry); |
| 103 | } |
| 104 | text = alsoEdit(text); |
| 105 | |
| 106 | if (text === before) return { path: file, action: 'unchanged' }; |
| 107 | atomicWriteFileSync(file, text); |
| 108 | return { path: file, action: existed ? 'updated' : 'created' }; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Inverse of `upsertCodegraphServer`: take out CodeGraph's server, its |
no test coverage detected