| 72 | } |
| 73 | |
| 74 | class ClaudeCodeTarget implements AgentTarget { |
| 75 | readonly id = 'claude' as const; |
| 76 | readonly displayName = 'Claude Code'; |
| 77 | readonly docsUrl = 'https://docs.claude.com/en/docs/claude-code'; |
| 78 | |
| 79 | supportsLocation(_loc: Location): boolean { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | detect(loc: Location): DetectionResult { |
| 84 | const mcpPath = mcpJsonPath(loc); |
| 85 | const config = readJsonFile(mcpPath); |
| 86 | const alreadyConfigured = !!config.mcpServers?.codegraph; |
| 87 | // For "installed" we infer from the existence of either the dir |
| 88 | // (global) or the project marker file (local). Cheap and avoids |
| 89 | // shelling out to `claude --version`. |
| 90 | const installed = loc === 'global' |
| 91 | ? fs.existsSync(configDir(loc)) || fs.existsSync(mcpPath) |
| 92 | : fs.existsSync(mcpPath) || fs.existsSync(configDir(loc)); |
| 93 | return { installed, alreadyConfigured, configPath: mcpPath }; |
| 94 | } |
| 95 | |
| 96 | install(loc: Location, opts: InstallOptions): WriteResult { |
| 97 | const files: WriteResult['files'] = []; |
| 98 | |
| 99 | // 1. MCP server entry |
| 100 | files.push(writeMcpEntry(loc)); |
| 101 | |
| 102 | // 1b. Migrate away any stale ./.claude.json left by a pre-#207 |
| 103 | // local install, so the project isn't left with two competing |
| 104 | // (one dead) MCP configs. |
| 105 | if (loc === 'local') { |
| 106 | const migrated = cleanupLegacyLocalMcp(); |
| 107 | if (migrated) files.push(migrated); |
| 108 | } |
| 109 | |
| 110 | // 2. Permissions (only when autoAllow) |
| 111 | if (opts.autoAllow) { |
| 112 | files.push(writePermissionsEntry(loc)); |
| 113 | } |
| 114 | |
| 115 | // 2b. Strip stale auto-sync hooks left by a pre-0.8 install. Those |
| 116 | // versions wrote `codegraph mark-dirty` / `sync-if-dirty` hooks to |
| 117 | // settings.json; both subcommands are gone from the CLI, so the |
| 118 | // Stop hook now fails every turn with "unknown command |
| 119 | // 'sync-if-dirty'". Cleaning up on install makes an upgrade |
| 120 | // self-healing. Only surfaced when something was actually removed. |
| 121 | const hookCleanup = cleanupLegacyHooks(loc); |
| 122 | if (hookCleanup.action === 'removed') files.push(hookCleanup); |
| 123 | |
| 124 | // 2c. Front-load prompt hook (Claude UserPromptSubmit). Opt-in via the |
| 125 | // installer prompt (default-yes): `promptHook === true` writes it; |
| 126 | // `=== false` strips any a prior install wrote so opting out round-trips |
| 127 | // (and an upgrade re-run honors the new choice); `undefined` leaves it |
| 128 | // untouched for callers that don't manage it. |
| 129 | if (opts.promptHook === true) { |
| 130 | files.push(writePromptHookEntry(loc)); |
| 131 | } else if (opts.promptHook === false) { |
nothing calls this directly
no outgoing calls
no test coverage detected