|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Preuninstall script for opencode-plugin-opencoder |
| 5 | + * |
| 6 | + * Removes agent markdown files from ~/.config/opencode/agents/ |
| 7 | + * This cleans up the agents when the plugin is uninstalled. |
| 8 | + */ |
| 9 | + |
| 10 | +import { existsSync, readdirSync, unlinkSync } from "node:fs" |
| 11 | +import { homedir } from "node:os" |
| 12 | +import { dirname, join } from "node:path" |
| 13 | +import { fileURLToPath } from "node:url" |
| 14 | + |
| 15 | +const __filename = fileURLToPath(import.meta.url) |
| 16 | +const __dirname = dirname(__filename) |
| 17 | + |
| 18 | +const AGENTS_SOURCE_DIR = join(__dirname, "agents") |
| 19 | +const AGENTS_TARGET_DIR = join(homedir(), ".config", "opencode", "agents") |
| 20 | + |
| 21 | +function main() { |
| 22 | + console.log("opencode-plugin-opencoder: Removing agents...") |
| 23 | + |
| 24 | + // Check if target directory exists |
| 25 | + if (!existsSync(AGENTS_TARGET_DIR)) { |
| 26 | + console.log(" No agents directory found, nothing to remove") |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Get list of agents we installed (from source directory) |
| 31 | + if (!existsSync(AGENTS_SOURCE_DIR)) { |
| 32 | + console.log(" Source agents directory not found, skipping cleanup") |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + const agentFiles = readdirSync(AGENTS_SOURCE_DIR).filter((f) => f.endsWith(".md")) |
| 37 | + |
| 38 | + if (agentFiles.length === 0) { |
| 39 | + console.log(" No agent files to remove") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + let removedCount = 0 |
| 44 | + |
| 45 | + for (const file of agentFiles) { |
| 46 | + const targetPath = join(AGENTS_TARGET_DIR, file) |
| 47 | + |
| 48 | + if (existsSync(targetPath)) { |
| 49 | + try { |
| 50 | + unlinkSync(targetPath) |
| 51 | + console.log(` Removed: ${file}`) |
| 52 | + removedCount++ |
| 53 | + } catch (err) { |
| 54 | + console.error(` Warning: Could not remove ${file}: ${err.message}`) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (removedCount > 0) { |
| 60 | + console.log(`\nopencode-plugin-opencoder: Removed ${removedCount} agent(s)`) |
| 61 | + } else { |
| 62 | + console.log("\nopencode-plugin-opencoder: No agents were installed, nothing removed") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +main() |
0 commit comments