Skip to content

Commit e7af923

Browse files
authored
Clean up old plugin versions from cache after auto-update (#48)
1 parent 49efa88 commit e7af923

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

claude-code/bundle/session-start.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// dist/src/hooks/session-start.js
44
import { fileURLToPath } from "node:url";
55
import { dirname, join as join4 } from "node:path";
6-
import { mkdirSync as mkdirSync2, appendFileSync as appendFileSync2, readFileSync as readFileSync3 } from "node:fs";
6+
import { mkdirSync as mkdirSync2, appendFileSync as appendFileSync2, readFileSync as readFileSync3, readdirSync, rmSync } from "node:fs";
77
import { execSync as execSync2 } from "node:child_process";
88
import { homedir as homedir4 } from "node:os";
99

@@ -454,8 +454,20 @@ async function main() {
454454
log3(`autoupdate: updating ${current} \u2192 ${latest}`);
455455
try {
456456
const scopes = ["user", "project", "local", "managed"];
457-
const cmd = scopes.map((s) => `claude plugin update hivemind@hivemind --scope ${s} 2>/dev/null`).join("; ");
457+
const cmd = scopes.map((s) => `claude plugin update hivemind@hivemind --scope ${s} 2>/dev/null || true`).join("; ");
458458
execSync2(cmd, { stdio: "ignore", timeout: 6e4 });
459+
try {
460+
const cacheParent = join4(homedir4(), ".claude", "plugins", "cache", "hivemind", "hivemind");
461+
const entries = readdirSync(cacheParent, { withFileTypes: true });
462+
for (const e of entries) {
463+
if (e.isDirectory() && e.name !== latest) {
464+
rmSync(join4(cacheParent, e.name), { recursive: true, force: true });
465+
log3(`cache cleanup: removed old version ${e.name}`);
466+
}
467+
}
468+
} catch (e) {
469+
log3(`cache cleanup failed: ${e.message}`);
470+
}
459471
updateNotice = `
460472
461473
\u2705 Hivemind auto-updated: ${current} \u2192 ${latest}. Run /reload-plugins to apply.`;

esbuild.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ await build({
114114
contents: [
115115
'import { createRequire } from "node:module";',
116116
'const _f = createRequire(import.meta.url)("fs");',
117-
'export const { existsSync, writeFileSync, mkdirSync, appendFileSync } = _f;',
117+
'export const { existsSync, writeFileSync, mkdirSync, appendFileSync, unlinkSync } = _f;',
118118
'const _k = ["rea","dFile","Sync"].join("");',
119119
'export const rfs = _f[_k];',
120120
'export { rfs as readFileSync };',

src/hooks/session-start.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { fileURLToPath } from "node:url";
1010
import { dirname, join } from "node:path";
11-
import { mkdirSync, appendFileSync, readFileSync } from "node:fs";
11+
import { mkdirSync, appendFileSync, readFileSync, readdirSync, rmSync } from "node:fs";
1212
import { execSync } from "node:child_process";
1313
import { homedir } from "node:os";
1414
import { loadCredentials, saveCredentials, login } from "../commands/auth.js";
@@ -200,9 +200,22 @@ async function main(): Promise<void> {
200200
try {
201201
const scopes = ["user", "project", "local", "managed"];
202202
const cmd = scopes
203-
.map(s => `claude plugin update hivemind@hivemind --scope ${s} 2>/dev/null`)
203+
.map(s => `claude plugin update hivemind@hivemind --scope ${s} 2>/dev/null || true`)
204204
.join("; ");
205205
execSync(cmd, { stdio: "ignore", timeout: 60_000 });
206+
// Clean up old cached versions, keep only the latest
207+
try {
208+
const cacheParent = join(homedir(), ".claude", "plugins", "cache", "hivemind", "hivemind");
209+
const entries = readdirSync(cacheParent, { withFileTypes: true });
210+
for (const e of entries) {
211+
if (e.isDirectory() && e.name !== latest) {
212+
rmSync(join(cacheParent, e.name), { recursive: true, force: true });
213+
log(`cache cleanup: removed old version ${e.name}`);
214+
}
215+
}
216+
} catch (e: any) {
217+
log(`cache cleanup failed: ${e.message}`);
218+
}
206219
updateNotice = `\n\n✅ Hivemind auto-updated: ${current}${latest}. Run /reload-plugins to apply.`;
207220
process.stderr.write(`✅ Hivemind auto-updated: ${current}${latest}. Run /reload-plugins to apply.\n`);
208221
log(`autoupdate succeeded: ${current}${latest}`);

0 commit comments

Comments
 (0)