claudeup 1.7.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (302) hide show
  1. package/bin/claudeup.js +20 -2
  2. package/package.json +10 -19
  3. package/src/data/cli-tools.js +123 -0
  4. package/src/data/cli-tools.ts +140 -0
  5. package/{dist → src}/data/marketplaces.js +23 -24
  6. package/src/data/marketplaces.ts +95 -0
  7. package/src/data/mcp-servers.js +509 -0
  8. package/src/data/mcp-servers.ts +526 -0
  9. package/src/data/statuslines.js +159 -0
  10. package/src/data/statuslines.ts +188 -0
  11. package/src/index.js +4 -0
  12. package/src/index.ts +5 -0
  13. package/{dist → src}/main.js +46 -47
  14. package/src/main.tsx +145 -0
  15. package/src/opentui.d.ts +191 -0
  16. package/src/prerunner/index.js +87 -0
  17. package/src/prerunner/index.ts +124 -0
  18. package/{dist → src}/services/claude-runner.js +9 -10
  19. package/src/services/claude-runner.ts +31 -0
  20. package/{dist → src}/services/claude-settings.js +72 -33
  21. package/src/services/claude-settings.ts +934 -0
  22. package/src/services/local-marketplace.js +339 -0
  23. package/src/services/local-marketplace.ts +489 -0
  24. package/{dist → src}/services/mcp-registry.js +13 -14
  25. package/src/services/mcp-registry.ts +105 -0
  26. package/{dist → src}/services/plugin-manager.js +61 -13
  27. package/src/services/plugin-manager.ts +693 -0
  28. package/{dist → src}/services/plugin-mcp-config.js +19 -18
  29. package/src/services/plugin-mcp-config.ts +242 -0
  30. package/{dist → src}/services/update-cache.js +7 -8
  31. package/src/services/update-cache.ts +78 -0
  32. package/{dist → src}/services/version-check.js +15 -14
  33. package/src/services/version-check.ts +122 -0
  34. package/src/types/index.js +1 -0
  35. package/src/types/index.ts +141 -0
  36. package/src/ui/App.js +213 -0
  37. package/src/ui/App.tsx +359 -0
  38. package/src/ui/components/CategoryHeader.js +9 -0
  39. package/src/ui/components/CategoryHeader.tsx +41 -0
  40. package/{dist → src}/ui/components/ScrollableList.js +19 -6
  41. package/src/ui/components/ScrollableList.tsx +98 -0
  42. package/src/ui/components/SearchInput.js +19 -0
  43. package/src/ui/components/SearchInput.tsx +56 -0
  44. package/src/ui/components/StyledText.js +39 -0
  45. package/src/ui/components/StyledText.tsx +70 -0
  46. package/src/ui/components/TabBar.js +38 -0
  47. package/src/ui/components/TabBar.tsx +88 -0
  48. package/src/ui/components/layout/Panel.js +6 -0
  49. package/src/ui/components/layout/Panel.tsx +62 -0
  50. package/src/ui/components/layout/ProgressBar.js +14 -0
  51. package/src/ui/components/layout/ProgressBar.tsx +47 -0
  52. package/src/ui/components/layout/ScopeTabs.js +6 -0
  53. package/src/ui/components/layout/ScopeTabs.tsx +53 -0
  54. package/src/ui/components/layout/ScreenLayout.js +21 -0
  55. package/src/ui/components/layout/ScreenLayout.tsx +147 -0
  56. package/src/ui/components/layout/index.js +4 -0
  57. package/src/ui/components/layout/index.ts +4 -0
  58. package/src/ui/components/modals/ConfirmModal.js +14 -0
  59. package/src/ui/components/modals/ConfirmModal.tsx +59 -0
  60. package/src/ui/components/modals/InputModal.js +16 -0
  61. package/src/ui/components/modals/InputModal.tsx +68 -0
  62. package/src/ui/components/modals/LoadingModal.js +14 -0
  63. package/src/ui/components/modals/LoadingModal.tsx +40 -0
  64. package/src/ui/components/modals/MessageModal.js +16 -0
  65. package/src/ui/components/modals/MessageModal.tsx +64 -0
  66. package/src/ui/components/modals/ModalContainer.js +56 -0
  67. package/src/ui/components/modals/ModalContainer.tsx +104 -0
  68. package/src/ui/components/modals/SelectModal.js +26 -0
  69. package/src/ui/components/modals/SelectModal.tsx +82 -0
  70. package/src/ui/components/modals/index.js +6 -0
  71. package/src/ui/components/modals/index.ts +6 -0
  72. package/src/ui/hooks/index.js +3 -0
  73. package/src/ui/hooks/index.ts +3 -0
  74. package/{dist → src}/ui/hooks/useAsyncData.js +21 -22
  75. package/src/ui/hooks/useAsyncData.ts +127 -0
  76. package/src/ui/hooks/useKeyboard.js +13 -0
  77. package/src/ui/hooks/useKeyboard.ts +26 -0
  78. package/src/ui/hooks/useKeyboardHandler.js +39 -0
  79. package/src/ui/hooks/useKeyboardHandler.ts +63 -0
  80. package/{dist → src}/ui/screens/CliToolsScreen.js +60 -54
  81. package/src/ui/screens/CliToolsScreen.tsx +468 -0
  82. package/src/ui/screens/EnvVarsScreen.js +154 -0
  83. package/src/ui/screens/EnvVarsScreen.tsx +269 -0
  84. package/{dist → src}/ui/screens/McpRegistryScreen.js +56 -55
  85. package/src/ui/screens/McpRegistryScreen.tsx +331 -0
  86. package/{dist → src}/ui/screens/McpScreen.js +46 -47
  87. package/src/ui/screens/McpScreen.tsx +392 -0
  88. package/src/ui/screens/ModelSelectorScreen.js +292 -0
  89. package/src/ui/screens/ModelSelectorScreen.tsx +441 -0
  90. package/{dist → src}/ui/screens/PluginsScreen.js +305 -293
  91. package/src/ui/screens/PluginsScreen.tsx +1231 -0
  92. package/src/ui/screens/StatusLineScreen.js +200 -0
  93. package/src/ui/screens/StatusLineScreen.tsx +411 -0
  94. package/src/ui/screens/index.js +7 -0
  95. package/src/ui/screens/index.ts +7 -0
  96. package/src/ui/state/AnimationContext.js +34 -0
  97. package/src/ui/state/AnimationContext.tsx +76 -0
  98. package/{dist → src}/ui/state/AppContext.js +31 -32
  99. package/src/ui/state/AppContext.tsx +235 -0
  100. package/{dist → src}/ui/state/DimensionsContext.js +16 -17
  101. package/src/ui/state/DimensionsContext.tsx +144 -0
  102. package/{dist → src}/ui/state/reducer.js +89 -90
  103. package/src/ui/state/reducer.ts +467 -0
  104. package/src/ui/state/types.js +1 -0
  105. package/src/ui/state/types.ts +273 -0
  106. package/{dist → src}/utils/command-utils.js +3 -4
  107. package/src/utils/command-utils.ts +20 -0
  108. package/{dist → src}/utils/fuzzy-search.js +2 -3
  109. package/src/utils/fuzzy-search.ts +138 -0
  110. package/{dist → src}/utils/string-utils.js +6 -6
  111. package/src/utils/string-utils.ts +88 -0
  112. package/dist/data/cli-tools.d.ts +0 -13
  113. package/dist/data/cli-tools.d.ts.map +0 -1
  114. package/dist/data/cli-tools.js +0 -124
  115. package/dist/data/cli-tools.js.map +0 -1
  116. package/dist/data/marketplaces.d.ts +0 -6
  117. package/dist/data/marketplaces.d.ts.map +0 -1
  118. package/dist/data/marketplaces.js.map +0 -1
  119. package/dist/data/mcp-servers.d.ts +0 -8
  120. package/dist/data/mcp-servers.d.ts.map +0 -1
  121. package/dist/data/mcp-servers.js +0 -503
  122. package/dist/data/mcp-servers.js.map +0 -1
  123. package/dist/data/statuslines.d.ts +0 -10
  124. package/dist/data/statuslines.d.ts.map +0 -1
  125. package/dist/data/statuslines.js +0 -160
  126. package/dist/data/statuslines.js.map +0 -1
  127. package/dist/index.d.ts +0 -3
  128. package/dist/index.d.ts.map +0 -1
  129. package/dist/index.js +0 -90
  130. package/dist/index.js.map +0 -1
  131. package/dist/main.d.ts +0 -3
  132. package/dist/main.d.ts.map +0 -1
  133. package/dist/main.js.map +0 -1
  134. package/dist/prerunner/index.d.ts +0 -7
  135. package/dist/prerunner/index.d.ts.map +0 -1
  136. package/dist/prerunner/index.js +0 -64
  137. package/dist/prerunner/index.js.map +0 -1
  138. package/dist/services/claude-runner.d.ts +0 -7
  139. package/dist/services/claude-runner.d.ts.map +0 -1
  140. package/dist/services/claude-runner.js.map +0 -1
  141. package/dist/services/claude-settings.d.ts +0 -73
  142. package/dist/services/claude-settings.d.ts.map +0 -1
  143. package/dist/services/claude-settings.js.map +0 -1
  144. package/dist/services/local-marketplace.d.ts +0 -111
  145. package/dist/services/local-marketplace.d.ts.map +0 -1
  146. package/dist/services/local-marketplace.js +0 -599
  147. package/dist/services/local-marketplace.js.map +0 -1
  148. package/dist/services/mcp-registry.d.ts +0 -10
  149. package/dist/services/mcp-registry.d.ts.map +0 -1
  150. package/dist/services/mcp-registry.js.map +0 -1
  151. package/dist/services/plugin-manager.d.ts +0 -65
  152. package/dist/services/plugin-manager.d.ts.map +0 -1
  153. package/dist/services/plugin-manager.js.map +0 -1
  154. package/dist/services/plugin-mcp-config.d.ts +0 -52
  155. package/dist/services/plugin-mcp-config.d.ts.map +0 -1
  156. package/dist/services/plugin-mcp-config.js.map +0 -1
  157. package/dist/services/update-cache.d.ts +0 -16
  158. package/dist/services/update-cache.d.ts.map +0 -1
  159. package/dist/services/update-cache.js.map +0 -1
  160. package/dist/services/version-check.d.ts +0 -20
  161. package/dist/services/version-check.d.ts.map +0 -1
  162. package/dist/services/version-check.js.map +0 -1
  163. package/dist/types/index.d.ts +0 -105
  164. package/dist/types/index.d.ts.map +0 -1
  165. package/dist/types/index.js +0 -2
  166. package/dist/types/index.js.map +0 -1
  167. package/dist/ui/InkApp.d.ts +0 -5
  168. package/dist/ui/InkApp.d.ts.map +0 -1
  169. package/dist/ui/InkApp.js +0 -188
  170. package/dist/ui/InkApp.js.map +0 -1
  171. package/dist/ui/components/CategoryHeader.d.ts +0 -16
  172. package/dist/ui/components/CategoryHeader.d.ts.map +0 -1
  173. package/dist/ui/components/CategoryHeader.js +0 -11
  174. package/dist/ui/components/CategoryHeader.js.map +0 -1
  175. package/dist/ui/components/ScrollableList.d.ts +0 -16
  176. package/dist/ui/components/ScrollableList.d.ts.map +0 -1
  177. package/dist/ui/components/ScrollableList.js.map +0 -1
  178. package/dist/ui/components/SearchInput.d.ts +0 -18
  179. package/dist/ui/components/SearchInput.d.ts.map +0 -1
  180. package/dist/ui/components/SearchInput.js +0 -30
  181. package/dist/ui/components/SearchInput.js.map +0 -1
  182. package/dist/ui/components/TabBar.d.ts +0 -8
  183. package/dist/ui/components/TabBar.d.ts.map +0 -1
  184. package/dist/ui/components/TabBar.js +0 -18
  185. package/dist/ui/components/TabBar.js.map +0 -1
  186. package/dist/ui/components/layout/Footer.d.ts +0 -14
  187. package/dist/ui/components/layout/Footer.d.ts.map +0 -1
  188. package/dist/ui/components/layout/Footer.js +0 -23
  189. package/dist/ui/components/layout/Footer.js.map +0 -1
  190. package/dist/ui/components/layout/Header.d.ts +0 -4
  191. package/dist/ui/components/layout/Header.d.ts.map +0 -1
  192. package/dist/ui/components/layout/Header.js +0 -25
  193. package/dist/ui/components/layout/Header.js.map +0 -1
  194. package/dist/ui/components/layout/Panel.d.ts +0 -22
  195. package/dist/ui/components/layout/Panel.d.ts.map +0 -1
  196. package/dist/ui/components/layout/Panel.js +0 -8
  197. package/dist/ui/components/layout/Panel.js.map +0 -1
  198. package/dist/ui/components/layout/ProgressBar.d.ts +0 -12
  199. package/dist/ui/components/layout/ProgressBar.d.ts.map +0 -1
  200. package/dist/ui/components/layout/ProgressBar.js +0 -16
  201. package/dist/ui/components/layout/ProgressBar.js.map +0 -1
  202. package/dist/ui/components/layout/ScopeTabs.d.ts +0 -12
  203. package/dist/ui/components/layout/ScopeTabs.d.ts.map +0 -1
  204. package/dist/ui/components/layout/ScopeTabs.js +0 -8
  205. package/dist/ui/components/layout/ScopeTabs.js.map +0 -1
  206. package/dist/ui/components/layout/ScreenLayout.d.ts +0 -30
  207. package/dist/ui/components/layout/ScreenLayout.d.ts.map +0 -1
  208. package/dist/ui/components/layout/ScreenLayout.js +0 -23
  209. package/dist/ui/components/layout/ScreenLayout.js.map +0 -1
  210. package/dist/ui/components/layout/index.d.ts +0 -7
  211. package/dist/ui/components/layout/index.d.ts.map +0 -1
  212. package/dist/ui/components/layout/index.js +0 -7
  213. package/dist/ui/components/layout/index.js.map +0 -1
  214. package/dist/ui/components/modals/ConfirmModal.d.ts +0 -14
  215. package/dist/ui/components/modals/ConfirmModal.d.ts.map +0 -1
  216. package/dist/ui/components/modals/ConfirmModal.js +0 -15
  217. package/dist/ui/components/modals/ConfirmModal.js.map +0 -1
  218. package/dist/ui/components/modals/InputModal.d.ts +0 -16
  219. package/dist/ui/components/modals/InputModal.d.ts.map +0 -1
  220. package/dist/ui/components/modals/InputModal.js +0 -23
  221. package/dist/ui/components/modals/InputModal.js.map +0 -1
  222. package/dist/ui/components/modals/LoadingModal.d.ts +0 -8
  223. package/dist/ui/components/modals/LoadingModal.d.ts.map +0 -1
  224. package/dist/ui/components/modals/LoadingModal.js +0 -8
  225. package/dist/ui/components/modals/LoadingModal.js.map +0 -1
  226. package/dist/ui/components/modals/MessageModal.d.ts +0 -14
  227. package/dist/ui/components/modals/MessageModal.d.ts.map +0 -1
  228. package/dist/ui/components/modals/MessageModal.js +0 -17
  229. package/dist/ui/components/modals/MessageModal.js.map +0 -1
  230. package/dist/ui/components/modals/ModalContainer.d.ts +0 -7
  231. package/dist/ui/components/modals/ModalContainer.d.ts.map +0 -1
  232. package/dist/ui/components/modals/ModalContainer.js +0 -38
  233. package/dist/ui/components/modals/ModalContainer.js.map +0 -1
  234. package/dist/ui/components/modals/SelectModal.d.ts +0 -17
  235. package/dist/ui/components/modals/SelectModal.d.ts.map +0 -1
  236. package/dist/ui/components/modals/SelectModal.js +0 -33
  237. package/dist/ui/components/modals/SelectModal.js.map +0 -1
  238. package/dist/ui/components/modals/index.d.ts +0 -7
  239. package/dist/ui/components/modals/index.d.ts.map +0 -1
  240. package/dist/ui/components/modals/index.js +0 -7
  241. package/dist/ui/components/modals/index.js.map +0 -1
  242. package/dist/ui/hooks/index.d.ts +0 -3
  243. package/dist/ui/hooks/index.d.ts.map +0 -1
  244. package/dist/ui/hooks/index.js +0 -3
  245. package/dist/ui/hooks/index.js.map +0 -1
  246. package/dist/ui/hooks/useAsyncData.d.ts +0 -40
  247. package/dist/ui/hooks/useAsyncData.d.ts.map +0 -1
  248. package/dist/ui/hooks/useAsyncData.js.map +0 -1
  249. package/dist/ui/hooks/useKeyboardNavigation.d.ts +0 -27
  250. package/dist/ui/hooks/useKeyboardNavigation.d.ts.map +0 -1
  251. package/dist/ui/hooks/useKeyboardNavigation.js +0 -82
  252. package/dist/ui/hooks/useKeyboardNavigation.js.map +0 -1
  253. package/dist/ui/screens/CliToolsScreen.d.ts +0 -4
  254. package/dist/ui/screens/CliToolsScreen.d.ts.map +0 -1
  255. package/dist/ui/screens/CliToolsScreen.js.map +0 -1
  256. package/dist/ui/screens/EnvVarsScreen.d.ts +0 -4
  257. package/dist/ui/screens/EnvVarsScreen.d.ts.map +0 -1
  258. package/dist/ui/screens/EnvVarsScreen.js +0 -145
  259. package/dist/ui/screens/EnvVarsScreen.js.map +0 -1
  260. package/dist/ui/screens/McpRegistryScreen.d.ts +0 -4
  261. package/dist/ui/screens/McpRegistryScreen.d.ts.map +0 -1
  262. package/dist/ui/screens/McpRegistryScreen.js.map +0 -1
  263. package/dist/ui/screens/McpScreen.d.ts +0 -4
  264. package/dist/ui/screens/McpScreen.d.ts.map +0 -1
  265. package/dist/ui/screens/McpScreen.js.map +0 -1
  266. package/dist/ui/screens/ModelSelectorScreen.d.ts +0 -4
  267. package/dist/ui/screens/ModelSelectorScreen.d.ts.map +0 -1
  268. package/dist/ui/screens/ModelSelectorScreen.js +0 -143
  269. package/dist/ui/screens/ModelSelectorScreen.js.map +0 -1
  270. package/dist/ui/screens/PluginsScreen.d.ts +0 -4
  271. package/dist/ui/screens/PluginsScreen.d.ts.map +0 -1
  272. package/dist/ui/screens/PluginsScreen.js.map +0 -1
  273. package/dist/ui/screens/StatusLineScreen.d.ts +0 -4
  274. package/dist/ui/screens/StatusLineScreen.d.ts.map +0 -1
  275. package/dist/ui/screens/StatusLineScreen.js +0 -197
  276. package/dist/ui/screens/StatusLineScreen.js.map +0 -1
  277. package/dist/ui/screens/index.d.ts +0 -8
  278. package/dist/ui/screens/index.d.ts.map +0 -1
  279. package/dist/ui/screens/index.js +0 -8
  280. package/dist/ui/screens/index.js.map +0 -1
  281. package/dist/ui/state/AppContext.d.ts +0 -40
  282. package/dist/ui/state/AppContext.d.ts.map +0 -1
  283. package/dist/ui/state/AppContext.js.map +0 -1
  284. package/dist/ui/state/DimensionsContext.d.ts +0 -27
  285. package/dist/ui/state/DimensionsContext.d.ts.map +0 -1
  286. package/dist/ui/state/DimensionsContext.js.map +0 -1
  287. package/dist/ui/state/reducer.d.ts +0 -4
  288. package/dist/ui/state/reducer.d.ts.map +0 -1
  289. package/dist/ui/state/reducer.js.map +0 -1
  290. package/dist/ui/state/types.d.ts +0 -266
  291. package/dist/ui/state/types.d.ts.map +0 -1
  292. package/dist/ui/state/types.js +0 -2
  293. package/dist/ui/state/types.js.map +0 -1
  294. package/dist/utils/command-utils.d.ts +0 -8
  295. package/dist/utils/command-utils.d.ts.map +0 -1
  296. package/dist/utils/command-utils.js.map +0 -1
  297. package/dist/utils/fuzzy-search.d.ts +0 -33
  298. package/dist/utils/fuzzy-search.d.ts.map +0 -1
  299. package/dist/utils/fuzzy-search.js.map +0 -1
  300. package/dist/utils/string-utils.d.ts +0 -24
  301. package/dist/utils/string-utils.d.ts.map +0 -1
  302. package/dist/utils/string-utils.js.map +0 -1
@@ -0,0 +1,934 @@
1
+ import fs from "fs-extra";
2
+ import path from "node:path";
3
+ import os from "node:os";
4
+ import type {
5
+ ClaudeSettings,
6
+ ClaudeLocalSettings,
7
+ McpServerConfig,
8
+ MarketplaceSource,
9
+ DiscoveredMarketplace,
10
+ InstalledPluginsRegistry,
11
+ InstalledPluginEntry,
12
+ } from "../types/index.js";
13
+ import { parsePluginId } from "../utils/string-utils.js";
14
+
15
+ const CLAUDE_DIR = ".claude";
16
+ const SETTINGS_FILE = "settings.json";
17
+ const LOCAL_SETTINGS_FILE = "settings.local.json";
18
+ const MCP_CONFIG_FILE = ".mcp.json";
19
+
20
+ // MCP config file types
21
+ interface McpConfigFile {
22
+ mcpServers?: Record<string, McpServerConfig>;
23
+ }
24
+
25
+ export function getClaudeDir(projectPath?: string): string {
26
+ const base = projectPath || process.cwd();
27
+ return path.join(base, CLAUDE_DIR);
28
+ }
29
+
30
+ export function getGlobalClaudeDir(): string {
31
+ return path.join(os.homedir(), CLAUDE_DIR);
32
+ }
33
+
34
+ export async function ensureClaudeDir(projectPath?: string): Promise<string> {
35
+ const claudeDir = getClaudeDir(projectPath);
36
+ await fs.ensureDir(claudeDir);
37
+ return claudeDir;
38
+ }
39
+
40
+ export async function readSettings(
41
+ projectPath?: string,
42
+ ): Promise<ClaudeSettings> {
43
+ const settingsPath = path.join(getClaudeDir(projectPath), SETTINGS_FILE);
44
+ try {
45
+ if (await fs.pathExists(settingsPath)) {
46
+ return await fs.readJson(settingsPath);
47
+ }
48
+ } catch {
49
+ // Return empty settings on error
50
+ }
51
+ return {};
52
+ }
53
+
54
+ export async function writeSettings(
55
+ settings: ClaudeSettings,
56
+ projectPath?: string,
57
+ ): Promise<void> {
58
+ const claudeDir = await ensureClaudeDir(projectPath);
59
+ const settingsPath = path.join(claudeDir, SETTINGS_FILE);
60
+ await fs.writeJson(settingsPath, settings, { spaces: 2 });
61
+ }
62
+
63
+ export async function readLocalSettings(
64
+ projectPath?: string,
65
+ ): Promise<ClaudeLocalSettings> {
66
+ const localPath = path.join(getClaudeDir(projectPath), LOCAL_SETTINGS_FILE);
67
+ try {
68
+ if (await fs.pathExists(localPath)) {
69
+ return await fs.readJson(localPath);
70
+ }
71
+ } catch {
72
+ // Return empty settings on error
73
+ }
74
+ return {};
75
+ }
76
+
77
+ export async function writeLocalSettings(
78
+ settings: ClaudeLocalSettings,
79
+ projectPath?: string,
80
+ ): Promise<void> {
81
+ const claudeDir = await ensureClaudeDir(projectPath);
82
+ const localPath = path.join(claudeDir, LOCAL_SETTINGS_FILE);
83
+ await fs.writeJson(localPath, settings, { spaces: 2 });
84
+ }
85
+
86
+ // MCP config file management (.mcp.json at project root)
87
+ export function getMcpConfigPath(projectPath?: string): string {
88
+ const base = projectPath || process.cwd();
89
+ return path.join(base, MCP_CONFIG_FILE);
90
+ }
91
+
92
+ export async function readMcpConfig(
93
+ projectPath?: string,
94
+ ): Promise<McpConfigFile> {
95
+ const mcpPath = getMcpConfigPath(projectPath);
96
+ try {
97
+ if (await fs.pathExists(mcpPath)) {
98
+ return await fs.readJson(mcpPath);
99
+ }
100
+ } catch {
101
+ // Return empty config on error
102
+ }
103
+ return {};
104
+ }
105
+
106
+ export async function writeMcpConfig(
107
+ config: McpConfigFile,
108
+ projectPath?: string,
109
+ ): Promise<void> {
110
+ const mcpPath = getMcpConfigPath(projectPath);
111
+ await fs.writeJson(mcpPath, config, { spaces: 2 });
112
+ }
113
+
114
+ export async function readGlobalSettings(): Promise<ClaudeSettings> {
115
+ const settingsPath = path.join(getGlobalClaudeDir(), SETTINGS_FILE);
116
+ try {
117
+ if (await fs.pathExists(settingsPath)) {
118
+ return await fs.readJson(settingsPath);
119
+ }
120
+ } catch {
121
+ // Return empty settings on error
122
+ }
123
+ return {};
124
+ }
125
+
126
+ export async function writeGlobalSettings(
127
+ settings: ClaudeSettings,
128
+ ): Promise<void> {
129
+ await fs.ensureDir(getGlobalClaudeDir());
130
+ const settingsPath = path.join(getGlobalClaudeDir(), SETTINGS_FILE);
131
+ await fs.writeJson(settingsPath, settings, { spaces: 2 });
132
+ }
133
+
134
+ // MCP Server management (writes to .mcp.json at project root)
135
+ export async function addMcpServer(
136
+ name: string,
137
+ config: McpServerConfig,
138
+ projectPath?: string,
139
+ ): Promise<void> {
140
+ // Extract env vars from config - they go to settings.local.json, not .mcp.json
141
+ const envVars = config.env || {};
142
+ const configWithoutEnv: McpServerConfig = { ...config };
143
+ delete configWithoutEnv.env;
144
+
145
+ // Add to .mcp.json (without env vars)
146
+ const mcpConfig = await readMcpConfig(projectPath);
147
+ mcpConfig.mcpServers = mcpConfig.mcpServers || {};
148
+ mcpConfig.mcpServers[name] = configWithoutEnv;
149
+ await writeMcpConfig(mcpConfig, projectPath);
150
+
151
+ // Enable in settings.local.json and add env vars
152
+ const localSettings = await readLocalSettings(projectPath);
153
+ const enabledServers = localSettings.enabledMcpjsonServers || [];
154
+ if (!enabledServers.includes(name)) {
155
+ enabledServers.push(name);
156
+ }
157
+ localSettings.enabledMcpjsonServers = enabledServers;
158
+ localSettings.enableAllProjectMcpServers = true;
159
+
160
+ // Add env vars to settings.local.json
161
+ if (Object.keys(envVars).length > 0) {
162
+ localSettings.env = localSettings.env || {};
163
+ for (const [key, value] of Object.entries(envVars)) {
164
+ // Only add non-reference values (references like ${VAR} don't need to be stored)
165
+ if (!value.startsWith("${") || !value.endsWith("}")) {
166
+ localSettings.env[key] = value;
167
+ }
168
+ }
169
+ }
170
+
171
+ await writeLocalSettings(localSettings, projectPath);
172
+ }
173
+
174
+ export async function removeMcpServer(
175
+ name: string,
176
+ projectPath?: string,
177
+ ): Promise<void> {
178
+ // Remove from .mcp.json
179
+ const mcpConfig = await readMcpConfig(projectPath);
180
+ if (mcpConfig.mcpServers) {
181
+ delete mcpConfig.mcpServers[name];
182
+ }
183
+ await writeMcpConfig(mcpConfig, projectPath);
184
+
185
+ // Remove from settings.local.json
186
+ const localSettings = await readLocalSettings(projectPath);
187
+ if (localSettings.enabledMcpjsonServers) {
188
+ localSettings.enabledMcpjsonServers =
189
+ localSettings.enabledMcpjsonServers.filter((s) => s !== name);
190
+ await writeLocalSettings(localSettings, projectPath);
191
+ }
192
+ }
193
+
194
+ export async function toggleMcpServer(
195
+ name: string,
196
+ enabled: boolean,
197
+ projectPath?: string,
198
+ ): Promise<void> {
199
+ // Toggle is now a remove operation since .mcp.json doesn't have enabled/disabled state
200
+ // If disabled, remove from config; if enabled, the server should already be in config
201
+ if (!enabled) {
202
+ await removeMcpServer(name, projectPath);
203
+ }
204
+ // If enabling, the server should already exist in the config
205
+ }
206
+
207
+ export async function setAllowMcp(
208
+ _allow: boolean,
209
+ _projectPath?: string,
210
+ ): Promise<void> {
211
+ // .mcp.json doesn't have an allowMcp setting - servers are either in the file or not
212
+ // This function is kept for API compatibility but is now a no-op
213
+ }
214
+
215
+ // Marketplace management - READ ONLY
216
+ // Use Claude Code CLI commands to add/remove marketplaces:
217
+ // claude marketplace add owner/repo
218
+ // claude marketplace remove name
219
+
220
+ // Plugin management
221
+ export async function enablePlugin(
222
+ pluginId: string,
223
+ enabled: boolean,
224
+ projectPath?: string,
225
+ ): Promise<void> {
226
+ const settings = await readSettings(projectPath);
227
+ settings.enabledPlugins = settings.enabledPlugins || {};
228
+ settings.enabledPlugins[pluginId] = enabled;
229
+ await writeSettings(settings, projectPath);
230
+ }
231
+
232
+ export async function getEnabledPlugins(
233
+ projectPath?: string,
234
+ ): Promise<Record<string, boolean>> {
235
+ const settings = await readSettings(projectPath);
236
+ return settings.enabledPlugins || {};
237
+ }
238
+
239
+ export async function getLocalEnabledPlugins(
240
+ projectPath?: string,
241
+ ): Promise<Record<string, boolean>> {
242
+ const settings = await readLocalSettings(projectPath);
243
+ return settings.enabledPlugins || {};
244
+ }
245
+
246
+ export async function getLocalInstalledPluginVersions(
247
+ projectPath?: string,
248
+ ): Promise<Record<string, string>> {
249
+ const settings = await readLocalSettings(projectPath);
250
+ return settings.installedPluginVersions || {};
251
+ }
252
+
253
+ // Local plugin management (writes to settings.local.json)
254
+ export async function enableLocalPlugin(
255
+ pluginId: string,
256
+ enabled: boolean,
257
+ projectPath?: string,
258
+ ): Promise<void> {
259
+ const settings = await readLocalSettings(projectPath);
260
+ settings.enabledPlugins = settings.enabledPlugins || {};
261
+ settings.enabledPlugins[pluginId] = enabled;
262
+ await writeLocalSettings(settings, projectPath);
263
+ }
264
+
265
+ export async function saveLocalInstalledPluginVersion(
266
+ pluginId: string,
267
+ version: string,
268
+ projectPath?: string,
269
+ ): Promise<void> {
270
+ const settings = await readLocalSettings(projectPath);
271
+ settings.installedPluginVersions = settings.installedPluginVersions || {};
272
+ settings.installedPluginVersions[pluginId] = version;
273
+ await writeLocalSettings(settings, projectPath);
274
+
275
+ // Update registry for local scope
276
+ await updateInstalledPluginsRegistry(
277
+ pluginId,
278
+ version,
279
+ "local",
280
+ projectPath ? path.resolve(projectPath) : undefined,
281
+ );
282
+ }
283
+
284
+ export async function removeLocalInstalledPluginVersion(
285
+ pluginId: string,
286
+ projectPath?: string,
287
+ ): Promise<void> {
288
+ const settings = await readLocalSettings(projectPath);
289
+ if (settings.installedPluginVersions) {
290
+ delete settings.installedPluginVersions[pluginId];
291
+ }
292
+ if (settings.enabledPlugins) {
293
+ delete settings.enabledPlugins[pluginId];
294
+ }
295
+ await writeLocalSettings(settings, projectPath);
296
+
297
+ // Remove from registry for local scope
298
+ await removeFromInstalledPluginsRegistry(
299
+ pluginId,
300
+ "local",
301
+ projectPath ? path.resolve(projectPath) : undefined,
302
+ );
303
+ }
304
+
305
+ // Status line management
306
+ export async function setStatusLine(
307
+ template: string,
308
+ projectPath?: string,
309
+ ): Promise<void> {
310
+ const settings = await readSettings(projectPath);
311
+ settings.statusLine = template;
312
+ await writeSettings(settings, projectPath);
313
+ }
314
+
315
+ export async function getStatusLine(
316
+ projectPath?: string,
317
+ ): Promise<string | undefined> {
318
+ const settings = await readSettings(projectPath);
319
+ return settings.statusLine;
320
+ }
321
+
322
+ // Global status line management
323
+ export async function setGlobalStatusLine(template: string): Promise<void> {
324
+ const settings = await readGlobalSettings();
325
+ settings.statusLine = template;
326
+ await writeGlobalSettings(settings);
327
+ }
328
+
329
+ export async function getGlobalStatusLine(): Promise<string | undefined> {
330
+ const settings = await readGlobalSettings();
331
+ return settings.statusLine;
332
+ }
333
+
334
+ // Get effective status line (project overrides global)
335
+ export async function getEffectiveStatusLine(projectPath?: string): Promise<{
336
+ template: string | undefined;
337
+ source: "project" | "global" | "default";
338
+ }> {
339
+ const projectStatusLine = await getStatusLine(projectPath);
340
+ if (projectStatusLine) {
341
+ return { template: projectStatusLine, source: "project" };
342
+ }
343
+
344
+ const globalStatusLine = await getGlobalStatusLine();
345
+ if (globalStatusLine) {
346
+ return { template: globalStatusLine, source: "global" };
347
+ }
348
+
349
+ return { template: undefined, source: "default" };
350
+ }
351
+
352
+ // Check if .claude directory exists
353
+ export async function hasClaudeDir(projectPath?: string): Promise<boolean> {
354
+ return fs.pathExists(getClaudeDir(projectPath));
355
+ }
356
+
357
+ // Get installed MCP servers (from .mcp.json)
358
+ export async function getInstalledMcpServers(
359
+ projectPath?: string,
360
+ ): Promise<Record<string, McpServerConfig>> {
361
+ const mcpConfig = await readMcpConfig(projectPath);
362
+ return mcpConfig.mcpServers || {};
363
+ }
364
+
365
+ // Get env vars for MCP servers (from settings.local.json)
366
+ export async function getMcpEnvVars(
367
+ projectPath?: string,
368
+ ): Promise<Record<string, string>> {
369
+ const localSettings = await readLocalSettings(projectPath);
370
+ return localSettings.env || {};
371
+ }
372
+
373
+ // Set an env var for MCP servers (in settings.local.json)
374
+ export async function setMcpEnvVar(
375
+ name: string,
376
+ value: string,
377
+ projectPath?: string,
378
+ ): Promise<void> {
379
+ const localSettings = await readLocalSettings(projectPath);
380
+ localSettings.env = localSettings.env || {};
381
+ localSettings.env[name] = value;
382
+ await writeLocalSettings(localSettings, projectPath);
383
+ }
384
+
385
+ // Remove an env var (from settings.local.json)
386
+ export async function removeMcpEnvVar(
387
+ name: string,
388
+ projectPath?: string,
389
+ ): Promise<void> {
390
+ const localSettings = await readLocalSettings(projectPath);
391
+ if (localSettings.env) {
392
+ delete localSettings.env[name];
393
+ await writeLocalSettings(localSettings, projectPath);
394
+ }
395
+ }
396
+
397
+ // Get enabled MCP servers (all servers in .mcp.json are considered enabled)
398
+ export async function getEnabledMcpServers(
399
+ projectPath?: string,
400
+ ): Promise<Record<string, boolean>> {
401
+ const mcpConfig = await readMcpConfig(projectPath);
402
+ const servers = mcpConfig.mcpServers || {};
403
+ const enabled: Record<string, boolean> = {};
404
+ for (const name of Object.keys(servers)) {
405
+ enabled[name] = true;
406
+ }
407
+ return enabled;
408
+ }
409
+
410
+ // Get all configured marketplaces
411
+ export async function getConfiguredMarketplaces(
412
+ projectPath?: string,
413
+ ): Promise<Record<string, MarketplaceSource>> {
414
+ const settings = await readSettings(projectPath);
415
+ return settings.extraKnownMarketplaces || {};
416
+ }
417
+
418
+ // Global marketplace management - READ ONLY
419
+ // Marketplaces are managed via Claude Code's native system
420
+
421
+ export async function getGlobalConfiguredMarketplaces(): Promise<
422
+ Record<string, MarketplaceSource>
423
+ > {
424
+ const settings = await readGlobalSettings();
425
+ return settings.extraKnownMarketplaces || {};
426
+ }
427
+
428
+ // Global plugin management
429
+ export async function enableGlobalPlugin(
430
+ pluginId: string,
431
+ enabled: boolean,
432
+ ): Promise<void> {
433
+ const settings = await readGlobalSettings();
434
+ settings.enabledPlugins = settings.enabledPlugins || {};
435
+ settings.enabledPlugins[pluginId] = enabled;
436
+ await writeGlobalSettings(settings);
437
+ }
438
+
439
+ export async function getGlobalEnabledPlugins(): Promise<
440
+ Record<string, boolean>
441
+ > {
442
+ const settings = await readGlobalSettings();
443
+ return settings.enabledPlugins || {};
444
+ }
445
+
446
+ export async function getGlobalInstalledPluginVersions(): Promise<
447
+ Record<string, string>
448
+ > {
449
+ const settings = await readGlobalSettings();
450
+ return settings.installedPluginVersions || {};
451
+ }
452
+
453
+ export async function saveGlobalInstalledPluginVersion(
454
+ pluginId: string,
455
+ version: string,
456
+ ): Promise<void> {
457
+ const settings = await readGlobalSettings();
458
+ settings.installedPluginVersions = settings.installedPluginVersions || {};
459
+ settings.installedPluginVersions[pluginId] = version;
460
+ await writeGlobalSettings(settings);
461
+
462
+ // Update registry for user scope
463
+ await updateInstalledPluginsRegistry(pluginId, version, "user");
464
+ }
465
+
466
+ export async function removeGlobalInstalledPluginVersion(
467
+ pluginId: string,
468
+ ): Promise<void> {
469
+ const settings = await readGlobalSettings();
470
+ if (settings.installedPluginVersions) {
471
+ delete settings.installedPluginVersions[pluginId];
472
+ }
473
+ if (settings.enabledPlugins) {
474
+ delete settings.enabledPlugins[pluginId];
475
+ }
476
+ await writeGlobalSettings(settings);
477
+
478
+ // Remove from registry for user scope
479
+ await removeFromInstalledPluginsRegistry(pluginId, "user");
480
+ }
481
+
482
+ // Shared logic for discovering marketplaces from settings
483
+ function discoverMarketplacesFromSettings(
484
+ settings: ClaudeSettings,
485
+ ): DiscoveredMarketplace[] {
486
+ const discovered = new Map<string, DiscoveredMarketplace>();
487
+
488
+ // 1. From extraKnownMarketplaces (explicitly configured)
489
+ for (const [name, config] of Object.entries(
490
+ settings.extraKnownMarketplaces || {},
491
+ )) {
492
+ discovered.set(name, { name, source: "configured", config });
493
+ }
494
+
495
+ // 2. From enabledPlugins (infer marketplace from plugin ID format: pluginName@marketplaceName)
496
+ for (const pluginId of Object.keys(settings.enabledPlugins || {})) {
497
+ const parsed = parsePluginId(pluginId);
498
+ if (parsed && !discovered.has(parsed.marketplace)) {
499
+ discovered.set(parsed.marketplace, {
500
+ name: parsed.marketplace,
501
+ source: "inferred",
502
+ });
503
+ }
504
+ }
505
+
506
+ // 3. From installedPluginVersions (same format)
507
+ for (const pluginId of Object.keys(settings.installedPluginVersions || {})) {
508
+ const parsed = parsePluginId(pluginId);
509
+ if (parsed && !discovered.has(parsed.marketplace)) {
510
+ discovered.set(parsed.marketplace, {
511
+ name: parsed.marketplace,
512
+ source: "inferred",
513
+ });
514
+ }
515
+ }
516
+
517
+ return Array.from(discovered.values());
518
+ }
519
+
520
+ // Discover all marketplaces from settings (configured + inferred from plugins)
521
+ export async function discoverAllMarketplaces(
522
+ projectPath?: string,
523
+ ): Promise<DiscoveredMarketplace[]> {
524
+ try {
525
+ const settings = await readSettings(projectPath);
526
+ return discoverMarketplacesFromSettings(settings);
527
+ } catch (error) {
528
+ // Graceful degradation - return empty array instead of crashing
529
+ console.error(
530
+ "Failed to discover project marketplaces:",
531
+ error instanceof Error ? error.message : "Unknown error",
532
+ );
533
+ return [];
534
+ }
535
+ }
536
+
537
+ // Discover all marketplaces from global settings
538
+ export async function discoverAllGlobalMarketplaces(): Promise<
539
+ DiscoveredMarketplace[]
540
+ > {
541
+ try {
542
+ const settings = await readGlobalSettings();
543
+ return discoverMarketplacesFromSettings(settings);
544
+ } catch (error) {
545
+ // Graceful degradation - return empty array instead of crashing
546
+ console.error(
547
+ "Failed to discover global marketplaces:",
548
+ error instanceof Error ? error.message : "Unknown error",
549
+ );
550
+ return [];
551
+ }
552
+ }
553
+
554
+ // installed_plugins.json registry management
555
+ const INSTALLED_PLUGINS_FILE = path.join(
556
+ os.homedir(),
557
+ ".claude",
558
+ "plugins",
559
+ "installed_plugins.json",
560
+ );
561
+
562
+ const KNOWN_MARKETPLACES_FILE = path.join(
563
+ os.homedir(),
564
+ ".claude",
565
+ "plugins",
566
+ "known_marketplaces.json",
567
+ );
568
+
569
+ interface KnownMarketplaceEntry {
570
+ source: { source: string; path?: string; repo?: string };
571
+ installLocation: string;
572
+ lastUpdated: string;
573
+ autoUpdate?: boolean;
574
+ }
575
+
576
+ type KnownMarketplaces = Record<string, KnownMarketplaceEntry>;
577
+
578
+ /**
579
+ * Write known_marketplaces.json
580
+ */
581
+ async function writeKnownMarketplaces(
582
+ marketplaces: KnownMarketplaces,
583
+ ): Promise<void> {
584
+ await fs.ensureDir(path.dirname(KNOWN_MARKETPLACES_FILE));
585
+ await fs.writeJson(KNOWN_MARKETPLACES_FILE, marketplaces, { spaces: 2 });
586
+ }
587
+
588
+ /**
589
+ * Set autoUpdate flag for a marketplace in known_marketplaces.json
590
+ * This is where Claude Code actually reads the autoUpdate setting
591
+ */
592
+ export async function setMarketplaceAutoUpdate(
593
+ marketplaceName: string,
594
+ autoUpdate: boolean,
595
+ ): Promise<boolean> {
596
+ const known = await readKnownMarketplaces();
597
+ if (known[marketplaceName]) {
598
+ known[marketplaceName].autoUpdate = autoUpdate;
599
+ await writeKnownMarketplaces(known);
600
+ return true;
601
+ }
602
+ return false; // Marketplace not yet installed by Claude Code
603
+ }
604
+
605
+ /**
606
+ * Get autoUpdate status for a marketplace
607
+ */
608
+ export async function getMarketplaceAutoUpdate(
609
+ marketplaceName: string,
610
+ ): Promise<boolean | undefined> {
611
+ const known = await readKnownMarketplaces();
612
+ return known[marketplaceName]?.autoUpdate;
613
+ }
614
+
615
+ export interface MarketplaceRecoveryResult {
616
+ enabledAutoUpdate: string[];
617
+ removed: string[];
618
+ }
619
+
620
+ /**
621
+ * Check if a marketplace is from MadAppGang
622
+ */
623
+ function isMadAppGangMarketplace(entry: KnownMarketplaceEntry): boolean {
624
+ const repo = entry.source?.repo?.toLowerCase() || "";
625
+ return repo.includes("madappgang");
626
+ }
627
+
628
+ /**
629
+ * Recover/sync marketplace settings:
630
+ * - Enable autoUpdate for MadAppGang marketplaces that don't have it set
631
+ * - Remove entries for marketplaces whose installLocation no longer exists
632
+ */
633
+ export async function recoverMarketplaceSettings(): Promise<MarketplaceRecoveryResult> {
634
+ const known = await readKnownMarketplaces();
635
+ const result: MarketplaceRecoveryResult = {
636
+ enabledAutoUpdate: [],
637
+ removed: [],
638
+ };
639
+
640
+ const updatedKnown: KnownMarketplaces = {};
641
+
642
+ for (const [name, entry] of Object.entries(known)) {
643
+ // Check if install location still exists
644
+ if (
645
+ entry.installLocation &&
646
+ !(await fs.pathExists(entry.installLocation))
647
+ ) {
648
+ result.removed.push(name);
649
+ continue;
650
+ }
651
+
652
+ // Enable autoUpdate if not set - only for MadAppGang marketplaces
653
+ if (entry.autoUpdate === undefined && isMadAppGangMarketplace(entry)) {
654
+ entry.autoUpdate = true;
655
+ result.enabledAutoUpdate.push(name);
656
+ }
657
+
658
+ updatedKnown[name] = entry;
659
+ }
660
+
661
+ // Write back if any changes were made
662
+ if (result.enabledAutoUpdate.length > 0 || result.removed.length > 0) {
663
+ await writeKnownMarketplaces(updatedKnown);
664
+ }
665
+
666
+ return result;
667
+ }
668
+
669
+ /**
670
+ * Read known_marketplaces.json to get marketplace source info
671
+ */
672
+ async function readKnownMarketplaces(): Promise<KnownMarketplaces> {
673
+ try {
674
+ if (await fs.pathExists(KNOWN_MARKETPLACES_FILE)) {
675
+ return await fs.readJson(KNOWN_MARKETPLACES_FILE);
676
+ }
677
+ } catch {
678
+ // Return empty if can't read
679
+ }
680
+ return {};
681
+ }
682
+
683
+ /**
684
+ * Get the source path for a plugin from its marketplace
685
+ * For directory-based marketplaces, returns the local directory path
686
+ * For GitHub marketplaces, returns the cloned repo path in ~/.claude/plugins/marketplaces/
687
+ */
688
+ async function getPluginSourcePath(
689
+ pluginName: string,
690
+ marketplace: string,
691
+ ): Promise<string | null> {
692
+ const known = await readKnownMarketplaces();
693
+ const mpEntry = known[marketplace];
694
+
695
+ if (!mpEntry) {
696
+ return null;
697
+ }
698
+
699
+ let basePath: string;
700
+
701
+ if (mpEntry.source.source === "directory" && mpEntry.source.path) {
702
+ // Directory-based marketplace - use the source path directly
703
+ basePath = mpEntry.source.path;
704
+ } else {
705
+ // GitHub-based marketplace - use installLocation (cloned repo path)
706
+ basePath = mpEntry.installLocation;
707
+ }
708
+
709
+ // Look for plugin in standard locations
710
+ const possiblePaths = [
711
+ path.join(basePath, "plugins", pluginName),
712
+ path.join(basePath, pluginName),
713
+ ];
714
+
715
+ for (const pluginPath of possiblePaths) {
716
+ if (await fs.pathExists(pluginPath)) {
717
+ return pluginPath;
718
+ }
719
+ }
720
+
721
+ return null;
722
+ }
723
+
724
+ /**
725
+ * Copy plugin files from source to cache
726
+ * This ensures the cache is populated with the latest plugin version
727
+ */
728
+ async function copyPluginToCache(
729
+ pluginId: string,
730
+ version: string,
731
+ marketplace: string,
732
+ ): Promise<boolean> {
733
+ const { pluginName } = parsePluginId(pluginId) || {
734
+ pluginName: pluginId.split("@")[0],
735
+ };
736
+
737
+ const sourcePath = await getPluginSourcePath(pluginName, marketplace);
738
+ if (!sourcePath) {
739
+ return false;
740
+ }
741
+
742
+ const cachePath = getPluginCachePath(pluginId, version, marketplace);
743
+
744
+ try {
745
+ // Remove existing cache directory if it exists
746
+ if (await fs.pathExists(cachePath)) {
747
+ await fs.remove(cachePath);
748
+ }
749
+
750
+ // Copy plugin files to cache
751
+ await fs.copy(sourcePath, cachePath, {
752
+ overwrite: true,
753
+ errorOnExist: false,
754
+ });
755
+
756
+ return true;
757
+ } catch (error) {
758
+ console.warn(
759
+ `Failed to copy plugin ${pluginId} to cache:`,
760
+ error instanceof Error ? error.message : "Unknown error",
761
+ );
762
+ return false;
763
+ }
764
+ }
765
+
766
+ /**
767
+ * Read installed_plugins.json registry
768
+ */
769
+ export async function readInstalledPluginsRegistry(): Promise<InstalledPluginsRegistry> {
770
+ try {
771
+ if (await fs.pathExists(INSTALLED_PLUGINS_FILE)) {
772
+ const content = await fs.readJson(INSTALLED_PLUGINS_FILE);
773
+ // Validate structure
774
+ if (!content.version || !content.plugins) {
775
+ throw new Error("Invalid registry structure");
776
+ }
777
+ return content;
778
+ }
779
+ } catch (error) {
780
+ // Backup corrupted file
781
+ if (await fs.pathExists(INSTALLED_PLUGINS_FILE)) {
782
+ try {
783
+ const backup = `${INSTALLED_PLUGINS_FILE}.backup.${Date.now()}`;
784
+ await fs.copy(INSTALLED_PLUGINS_FILE, backup);
785
+ console.warn(`Corrupted registry backed up to: ${backup}`);
786
+ } catch {
787
+ // Ignore backup errors
788
+ }
789
+ }
790
+ }
791
+ return { version: 2, plugins: {} };
792
+ }
793
+
794
+ /**
795
+ * Write installed_plugins.json registry
796
+ */
797
+ export async function writeInstalledPluginsRegistry(
798
+ registry: InstalledPluginsRegistry,
799
+ ): Promise<void> {
800
+ await fs.ensureDir(path.dirname(INSTALLED_PLUGINS_FILE));
801
+ await fs.writeJson(INSTALLED_PLUGINS_FILE, registry, { spaces: 2 });
802
+ }
803
+
804
+ /**
805
+ * Get install path for a plugin version in cache
806
+ */
807
+ function getPluginCachePath(
808
+ pluginId: string,
809
+ version: string,
810
+ marketplace: string,
811
+ ): string {
812
+ const { pluginName } = parsePluginId(pluginId) || {
813
+ pluginName: pluginId.split("@")[0],
814
+ };
815
+ return path.join(
816
+ os.homedir(),
817
+ ".claude",
818
+ "plugins",
819
+ "cache",
820
+ marketplace,
821
+ pluginName,
822
+ version,
823
+ );
824
+ }
825
+
826
+ /**
827
+ * Update installed_plugins.json when a plugin is installed/updated
828
+ * Also copies plugin files from source to cache to ensure latest version is available
829
+ */
830
+ export async function updateInstalledPluginsRegistry(
831
+ pluginId: string,
832
+ version: string,
833
+ scope: "user" | "project" | "local",
834
+ projectPath?: string,
835
+ ): Promise<void> {
836
+ try {
837
+ const registry = await readInstalledPluginsRegistry();
838
+
839
+ // Get marketplace from plugin ID
840
+ const parsed = parsePluginId(pluginId);
841
+ if (!parsed) {
842
+ console.warn(`Invalid plugin ID: ${pluginId}, skipping registry update`);
843
+ return;
844
+ }
845
+
846
+ const { marketplace } = parsed;
847
+
848
+ // Copy plugin files from source to cache
849
+ // This ensures the cache has the latest plugin version
850
+ await copyPluginToCache(pluginId, version, marketplace);
851
+
852
+ const installPath = getPluginCachePath(pluginId, version, marketplace);
853
+ const now = new Date().toISOString();
854
+
855
+ // Initialize plugin array if it doesn't exist
856
+ if (!registry.plugins[pluginId]) {
857
+ registry.plugins[pluginId] = [];
858
+ }
859
+
860
+ // Find existing entry for this scope and project
861
+ const existingIndex = registry.plugins[pluginId].findIndex((entry) => {
862
+ if (entry.scope !== scope) return false;
863
+ if (scope === "user") return true;
864
+ return entry.projectPath === projectPath;
865
+ });
866
+
867
+ const entry: InstalledPluginEntry = {
868
+ scope,
869
+ projectPath,
870
+ installPath,
871
+ version,
872
+ installedAt:
873
+ existingIndex >= 0
874
+ ? registry.plugins[pluginId][existingIndex].installedAt
875
+ : now,
876
+ lastUpdated: now,
877
+ gitCommitSha:
878
+ existingIndex >= 0
879
+ ? registry.plugins[pluginId][existingIndex].gitCommitSha
880
+ : undefined,
881
+ };
882
+
883
+ if (existingIndex >= 0) {
884
+ // Update existing entry
885
+ registry.plugins[pluginId][existingIndex] = entry;
886
+ } else {
887
+ // Add new entry
888
+ registry.plugins[pluginId].push(entry);
889
+ }
890
+
891
+ await writeInstalledPluginsRegistry(registry);
892
+ } catch (error) {
893
+ // Log warning but don't block plugin operation
894
+ console.warn(
895
+ `Failed to update registry for ${pluginId}:`,
896
+ error instanceof Error ? error.message : "Unknown error",
897
+ );
898
+ }
899
+ }
900
+
901
+ /**
902
+ * Remove plugin from installed_plugins.json registry
903
+ */
904
+ export async function removeFromInstalledPluginsRegistry(
905
+ pluginId: string,
906
+ scope: "user" | "project" | "local",
907
+ projectPath?: string,
908
+ ): Promise<void> {
909
+ try {
910
+ const registry = await readInstalledPluginsRegistry();
911
+
912
+ if (!registry.plugins[pluginId]) return;
913
+
914
+ // Remove entry matching scope and projectPath
915
+ registry.plugins[pluginId] = registry.plugins[pluginId].filter((entry) => {
916
+ if (entry.scope !== scope) return true;
917
+ if (scope === "user") return false;
918
+ return entry.projectPath !== projectPath;
919
+ });
920
+
921
+ // Remove plugin key if no entries remain
922
+ if (registry.plugins[pluginId].length === 0) {
923
+ delete registry.plugins[pluginId];
924
+ }
925
+
926
+ await writeInstalledPluginsRegistry(registry);
927
+ } catch (error) {
928
+ // Log warning but don't block plugin operation
929
+ console.warn(
930
+ `Failed to remove from registry for ${pluginId}:`,
931
+ error instanceof Error ? error.message : "Unknown error",
932
+ );
933
+ }
934
+ }