Skip to content

Commit e826839

Browse files
authored
Merge pull request #16 from vercel-labs/fix-auto-updates
fix auto updates
2 parents 4c0d636 + b592a79 commit e826839

2 files changed

Lines changed: 111 additions & 97 deletions

File tree

‎packages/add-plugin/lib/install.ts‎

Lines changed: 110 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { homedir } from "os";
1717
import { createHash } from "crypto";
1818
import type { DiscoveredPlugin } from "./discover.js";
1919
import type { Target } from "./targets.js";
20-
import { c, step, stepDone, stepError, barLine, barEmpty, barDebug } from "./ui.js";
20+
import { c, step, stepDone, stepError, barLine, barEmpty, barDebug, warn } from "./ui.js";
2121

2222
/**
2323
* Track whether the plugin cache has already been populated (by the Claude Code
@@ -112,91 +112,19 @@ async function installToClaudeCode(
112112
repoPath: string,
113113
source: string,
114114
): Promise<void> {
115-
const marketplaceName = plugins[0]?.marketplace ?? deriveMarketplaceName(source);
116-
117-
// 1. Prepare the repo directory for Claude Code
118-
step("Preparing plugins for Claude Code...");
119-
barEmpty();
120-
await prepareForClaudeCode(plugins, repoPath, marketplaceName);
121-
122-
// 2. Add the marketplace
123-
// For official Anthropic marketplaces, pass the GitHub URL directly so the
124-
// claude CLI recognises the source as coming from the 'anthropics' org.
125-
// Otherwise it rejects the reserved marketplace name.
126-
const marketplaceSource = isAnthropicSource(source) ? normalizeGitUrl(source) : repoPath;
127-
128-
const claudePath = findClaude();
129-
step("Adding marketplace");
130-
barDebug(c.dim(`Binary: ${claudePath}`));
131-
try {
132-
const version = execSync(`${claudePath} --version`, { encoding: "utf-8", stdio: "pipe" }).trim();
133-
barDebug(c.dim(`Version: ${version}`));
134-
} catch {
135-
barDebug(c.dim(`Warning: could not get claude version`));
136-
}
137-
138-
try {
139-
const result = execSync(`${claudePath} plugin marketplace add ${marketplaceSource}`, {
140-
encoding: "utf-8",
141-
stdio: "pipe",
142-
});
143-
if (result.trim()) barDebug(c.dim(result.trim()));
144-
stepDone("Marketplace added");
145-
} catch (err: any) {
146-
const stderr = err.stderr?.toString().trim() ?? "";
147-
const stdout = err.stdout?.toString().trim() ?? "";
148-
if (stderr.includes("already") || stdout.includes("already")) {
149-
stepDone(`Marketplace ${c.dim("'"+marketplaceName+"'")} already on disk`);
150-
} else {
151-
stepError("Failed to add marketplace.");
152-
barLine(c.dim(`Command: ${claudePath} plugin marketplace add ${marketplaceSource}`));
153-
if (stdout) barLine(c.dim(`stdout: ${stdout}`));
154-
if (stderr) barLine(c.dim(`stderr: ${stderr}`));
155-
barLine(c.dim(`exit code: ${err.status}`));
156-
process.exit(1);
157-
}
158-
}
159-
160-
barEmpty();
161-
162-
// 3. Install each plugin
163-
for (const plugin of plugins) {
164-
const pluginRef = `${plugin.name}@${marketplaceName}`;
165-
step(`Installing ${c.bold(pluginRef)}...`);
166-
167-
try {
168-
execSync(`${claudePath} plugin install ${pluginRef} --scope ${scope}`, {
169-
encoding: "utf-8",
170-
stdio: "pipe",
171-
});
172-
stepDone(`Installed ${c.cyan(pluginRef)}`);
173-
} catch (err: any) {
174-
const stderr = err.stderr?.toString().trim() ?? "";
175-
const stdout = err.stdout?.toString().trim() ?? "";
176-
if (stderr.includes("already") || stdout.includes("already")) {
177-
stepDone(`${c.cyan(pluginRef)} ${c.dim("already installed")}`);
178-
} else {
179-
stepError(`Failed to install ${pluginRef}`);
180-
barLine(c.dim(`Command: ${claudePath} plugin install ${pluginRef} --scope ${scope}`));
181-
if (stdout) barLine(c.dim(`stdout: ${stdout}`));
182-
if (stderr) barLine(c.dim(`stderr: ${stderr}`));
183-
barLine(c.dim(`exit code: ${err.status}`));
184-
}
185-
}
186-
}
187-
188-
cachePopulated = true;
115+
// Install directly to the plugin cache using git SHA-based paths (matches
116+
// the official Claude installer convention). We no longer shell out to the
117+
// `claude` CLI because it uses semver-based paths instead of commit hashes.
118+
await installToPluginCache(plugins, scope, repoPath, source);
189119
}
190120

191121
// ---------------------------------------------------------------------------
192122
// Cursor installer
193123
// ---------------------------------------------------------------------------
194124
//
195125
// Cursor reads "Imported" plugins from the Claude Code plugin cache directory
196-
// (~/.claude/plugins/). When the `claude` CLI is available, we use it (same as
197-
// Claude Code). When it's not available, we write directly to the cache
198-
// directory, registering the marketplace and plugins in the JSON manifests
199-
// that Cursor reads.
126+
// (~/.claude/plugins/). We write directly to the cache using git SHA-based
127+
// paths. On Windows, Cursor reads from ~/.cursor/extensions/ instead.
200128

201129
async function installToCursor(
202130
plugins: DiscoveredPlugin[],
@@ -208,16 +136,14 @@ async function installToCursor(
208136
// populated and Cursor will pick it up — nothing more to do.
209137
if (cachePopulated) return;
210138

211-
const claudePath = findClaudeOrNull();
212-
213-
if (claudePath) {
214-
// Claude CLI available — use it (same mechanism as Claude Code target).
215-
await installToClaudeCode(plugins, scope, repoPath, source);
139+
if (process.platform === "win32") {
140+
// Windows: Cursor reads from ~/.cursor/extensions/
141+
await installToCursorExtensions(plugins, scope, repoPath, source);
216142
return;
217143
}
218144

219-
// No claude CLI — install directly to Cursor's extensions directory.
220-
await installToCursorExtensions(plugins, scope, repoPath, source);
145+
// macOS/Linux: write directly to ~/.claude/plugins/ cache with git SHA paths.
146+
await installToPluginCache(plugins, scope, repoPath, source);
221147
}
222148

223149
// ---------------------------------------------------------------------------
@@ -258,12 +184,39 @@ async function installToPluginCache(
258184
}
259185
}
260186

187+
// Determine the marketplace source format to match what the official
188+
// Claude installer writes:
189+
// - GitHub repos → { source: "github", repo: "owner/repo" }
190+
// - Other git URLs → { source: "git", url: "https://..." }
191+
// - Local paths → { source: "directory", path: "/abs/path" }
192+
const githubRepo = extractGitHubRepo(source);
193+
const marketplacesDir = join(pluginsDir, "marketplaces");
194+
const marketplaceInstallLocation = join(marketplacesDir, marketplaceName);
195+
196+
// Copy the repo to the marketplaces directory so Claude Code can find
197+
// plugins when validating against the marketplace.
198+
await mkdir(marketplacesDir, { recursive: true });
199+
if (existsSync(marketplaceInstallLocation)) {
200+
await rm(marketplaceInstallLocation, { recursive: true });
201+
}
202+
await cp(repoPath, marketplaceInstallLocation, { recursive: true });
203+
barDebug(c.dim(`Marketplace copied to ${marketplaceInstallLocation}`));
204+
261205
if (knownMarketplaces[marketplaceName]) {
262206
stepDone(`Marketplace ${c.dim("'" + marketplaceName + "'")} already registered`);
263207
} else {
208+
let marketplaceSource: Record<string, string>;
209+
if (githubRepo) {
210+
marketplaceSource = { source: "github", repo: githubRepo };
211+
} else if (isRemoteSource(source)) {
212+
const gitUrl = normalizeGitUrl(source);
213+
marketplaceSource = { source: "git", url: gitUrl.endsWith(".git") ? gitUrl : gitUrl + ".git" };
214+
} else {
215+
marketplaceSource = { source: "directory", path: repoPath };
216+
}
264217
knownMarketplaces[marketplaceName] = {
265-
source: { source: "directory", path: repoPath },
266-
installLocation: repoPath,
218+
source: marketplaceSource,
219+
installLocation: marketplaceInstallLocation,
267220
lastUpdated: new Date().toISOString(),
268221
};
269222
await writeFile(knownPath, JSON.stringify(knownMarketplaces, null, 2));
@@ -294,10 +247,13 @@ async function installToPluginCache(
294247
for (const plugin of plugins) {
295248
const pluginRef = `${plugin.name}@${marketplaceName}`;
296249
const version = plugin.version ?? "0.0.0";
250+
// Use truncated git SHA for cache path (matches official Claude installer),
251+
// falling back to semver for non-git sources.
252+
const versionKey = gitSha ? gitSha.slice(0, 12) : version;
297253
step(`Installing ${c.bold(pluginRef)}...`);
298254

299255
// Copy plugin directory to cache
300-
const cacheDest = join(cacheDir, marketplaceName, plugin.name, version);
256+
const cacheDest = join(cacheDir, marketplaceName, plugin.name, versionKey);
301257
await mkdir(cacheDest, { recursive: true });
302258
await cp(plugin.path, cacheDest, { recursive: true });
303259
barDebug(c.dim(`Cached to ${cacheDest}`));
@@ -323,6 +279,36 @@ async function installToPluginCache(
323279
await writeFile(installedPath, JSON.stringify(installedData, null, 2));
324280
barDebug(c.dim("Updated installed_plugins.json"));
325281

282+
// 4. Enable plugins in ~/.claude/settings.json
283+
// Claude Code uses the `enabledPlugins` map in settings.json to determine
284+
// which installed plugins are actually active. Without this, plugins show
285+
// in "Discover" but not in "Installed".
286+
const settingsPath = join(home, ".claude", "settings.json");
287+
let settings: Record<string, unknown> = {};
288+
let settingsCorrupted = false;
289+
if (existsSync(settingsPath)) {
290+
try {
291+
settings = JSON.parse(await readFile(settingsPath, "utf-8"));
292+
} catch {
293+
settingsCorrupted = true;
294+
}
295+
}
296+
297+
if (settingsCorrupted) {
298+
warn("Could not parse ~/.claude/settings.json — skipping enabledPlugins update to avoid overwriting existing settings.");
299+
barLine(c.dim("You may need to manually enable the plugins in Claude Code settings."));
300+
} else {
301+
const enabled = (settings.enabledPlugins ?? {}) as Record<string, boolean>;
302+
for (const plugin of plugins) {
303+
const pluginKey = `${plugin.name}@${marketplaceName}`;
304+
enabled[pluginKey] = true;
305+
}
306+
settings.enabledPlugins = enabled;
307+
308+
await writeFile(settingsPath, JSON.stringify(settings, null, 2));
309+
barDebug(c.dim("Updated settings.json enabledPlugins"));
310+
}
311+
326312
cachePopulated = true;
327313
}
328314

@@ -340,12 +326,6 @@ async function installToCursorExtensions(
340326
repoPath: string,
341327
source: string,
342328
): Promise<void> {
343-
if (process.platform !== "win32") {
344-
// macOS/Linux: use the Claude plugin cache (Cursor reads from there)
345-
await installToPluginCache(plugins, scope, repoPath, source);
346-
return;
347-
}
348-
349329
// Windows: install to ~/.cursor/extensions/
350330
const marketplaceName = plugins[0]?.marketplace ?? deriveMarketplaceName(source);
351331
const home = homedir();
@@ -380,7 +360,10 @@ async function installToCursorExtensions(
380360
for (const plugin of plugins) {
381361
const pluginRef = `${plugin.name}@${marketplaceName}`;
382362
const version = plugin.version ?? "0.0.0";
383-
const folderName = `${marketplaceName}.${plugin.name}-${version}`;
363+
// Use git SHA in folder name to match official installer conventions,
364+
// falling back to semver for non-git sources.
365+
const versionKey = gitSha ? gitSha.slice(0, 12) : version;
366+
const folderName = `${marketplaceName}.${plugin.name}-${versionKey}`;
384367
const destDir = join(extensionsDir, folderName);
385368

386369
step(`Installing ${c.bold(pluginRef)}...`);
@@ -878,6 +861,37 @@ function deriveMarketplaceName(source: string): string {
878861
return parts[parts.length - 1] ?? "plugins";
879862
}
880863

864+
/**
865+
* Extract a GitHub "owner/repo" string from a source, or null if the source
866+
* is not a GitHub reference.
867+
*/
868+
function extractGitHubRepo(source: string): string | null {
869+
// Shorthand: owner/repo
870+
const shorthand = source.match(/^([\w-]+\/[\w.-]+)$/);
871+
if (shorthand) return shorthand[1]!;
872+
// HTTPS: https://github.com/owner/repo[.git]
873+
const httpsMatch = source.match(/^https?:\/\/github\.com\/([\w.-]+\/[\w.-]+?)(?:\.git)?$/);
874+
if (httpsMatch) return httpsMatch[1]!;
875+
// SSH: git@github.com:owner/repo[.git]
876+
const sshMatch = source.match(/^git@github\.com:([\w.-]+\/[\w.-]+?)(?:\.git)?$/);
877+
if (sshMatch) return sshMatch[1]!;
878+
return null;
879+
}
880+
881+
/**
882+
* Check if a source string is a remote git source (URL or owner/repo shorthand)
883+
* rather than a local file path.
884+
*/
885+
function isRemoteSource(source: string): boolean {
886+
// GitHub shorthand: owner/repo
887+
if (source.match(/^[\w-]+\/[\w.-]+$/)) return true;
888+
// SSH URL
889+
if (source.startsWith("git@")) return true;
890+
// HTTPS/HTTP URL
891+
if (source.startsWith("https://") || source.startsWith("http://")) return true;
892+
return false;
893+
}
894+
881895
/**
882896
* Check if a source string points to the anthropics GitHub org.
883897
*/

‎packages/add-plugin/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plugins",
3-
"version": "1.2.9",
3+
"version": "1.2.10-canary.5",
44
"description": "Install open-plugin format plugins into agent tools",
55
"type": "module",
66
"bin": {

0 commit comments

Comments
 (0)