MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / upsertCodeGraphServer

Function upsertCodeGraphServer

src/installer/targets/hermes.ts:318–353  ·  view source on GitHub ↗

* Add the codegraph server when the file has none. An existing entry is * the user's: nothing in it is changed, except that a block-style entry * missing `command` or `args` (it can't start without them) gets CodeGraph's.

(lines: string[])

Source from the content-addressed store, hash-verified

316 * missing `command` or `args` (it can't start without them) gets CodeGraph's.
317 */
318function upsertCodeGraphServer(lines: string[]): 'changed' | 'unchanged' | 'left-as-written' | 'blocked' {
319 const top = findTop(lines, 'mcp_servers');
320 if (!top) {
321 while (lines.length > 0 && (lines[lines.length - 1] ?? '').trim() === '') lines.pop();
322 if (lines.length > 0) lines.push('');
323 lines.push('mcp_servers:', ...renderCodeGraphServer(2));
324 return 'changed';
325 }
326
327 if (top.value !== '') {
328 if (findCodeGraphServer(lines)) return 'left-as-written';
329 // An empty one-line value (`{}`, `null`, `~`) is rewritten as a block; any
330 // other one-line mapping holds the user's servers and is never rewritten.
331 if (!/^(\{\s*\}|null|Null|NULL|~)$/.test(top.value)) return 'blocked';
332 lines.splice(top.line, 1, 'mcp_servers:', ...renderCodeGraphServer(2));
333 return 'changed';
334 }
335
336 const { indent, children } = childrenOf(lines, top.line, top.end);
337 const existing = children.find((c) => c.key === 'codegraph');
338 if (!existing) {
339 lines.splice(contentEnd(lines, top.line, top.end), 0, ...renderCodeGraphServer(indent ?? 2));
340 return 'changed';
341 }
342 if (existing.value !== '') return 'unchanged';
343
344 const own = childrenOf(lines, existing.line, existing.end);
345 const keyIndent = own.indent ?? (indent ?? 2) * 2;
346 const has = (key: string) => own.children.some((c) => c.key === key);
347 const missing: string[] = [];
348 if (!has('command')) missing.push(`${' '.repeat(keyIndent)}command: codegraph`);
349 if (!has('args')) missing.push(`${' '.repeat(keyIndent)}args: [serve, --mcp]`);
350 if (missing.length === 0) return 'unchanged';
351 lines.splice(contentEnd(lines, existing.line, existing.end), 0, ...missing);
352 return 'changed';
353}
354
355// ---------------------------------------------------------------------------
356// platform_toolsets.cli

Callers 1

writeHermesConfigFunction · 0.85

Calls 7

findTopFunction · 0.85
renderCodeGraphServerFunction · 0.85
findCodeGraphServerFunction · 0.85
childrenOfFunction · 0.85
contentEndFunction · 0.85
pushMethod · 0.80
hasFunction · 0.70

Tested by

no test coverage detected