|
| 1 | +import { cmd } from "./cmd" |
| 2 | +import * as prompts from "@clack/prompts" |
| 3 | +import { UI } from "../ui" |
| 4 | +import { Config } from "../../config/config" |
| 5 | +import { Instance } from "../../project/instance" |
| 6 | +import { Global } from "../../global" |
| 7 | +import { modify, applyEdits } from "jsonc-parser" |
| 8 | +import { resolveConfigPath } from "./mcp" |
| 9 | + |
| 10 | +async function updateDisabledPlugins(plugins: string[], configPath: string) { |
| 11 | + const file = Bun.file(configPath) |
| 12 | + |
| 13 | + let text = "{}" |
| 14 | + if (await file.exists()) { |
| 15 | + text = await file.text() |
| 16 | + } |
| 17 | + |
| 18 | + const edits = modify(text, ["disabled_plugins"], plugins, { |
| 19 | + formattingOptions: { tabSize: 2, insertSpaces: true }, |
| 20 | + }) |
| 21 | + const result = applyEdits(text, edits) |
| 22 | + |
| 23 | + await Bun.write(configPath, result) |
| 24 | +} |
| 25 | + |
| 26 | +export const PluginCommand = cmd({ |
| 27 | + command: "plugin", |
| 28 | + describe: "manage plugins", |
| 29 | + builder: (yargs) => |
| 30 | + yargs.command(PluginListCommand).command(PluginDisableCommand).command(PluginEnableCommand).demandCommand(), |
| 31 | + async handler() {}, |
| 32 | +}) |
| 33 | + |
| 34 | +export const PluginListCommand = cmd({ |
| 35 | + command: "list", |
| 36 | + aliases: ["ls"], |
| 37 | + describe: "list all configured plugins and their status", |
| 38 | + async handler() { |
| 39 | + await Instance.provide({ |
| 40 | + directory: process.cwd(), |
| 41 | + async fn() { |
| 42 | + UI.empty() |
| 43 | + prompts.intro("Plugins") |
| 44 | + |
| 45 | + const config = await Config.get() |
| 46 | + const plugins = config.plugin ?? [] |
| 47 | + const disabled = new Set(config.disabled_plugins ?? []) |
| 48 | + |
| 49 | + if (plugins.length === 0) { |
| 50 | + prompts.log.warn("No plugins configured") |
| 51 | + prompts.outro("Add plugins in opencode.json or via npm install") |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + for (const plugin of plugins) { |
| 56 | + const name = Config.getPluginName(plugin) |
| 57 | + const isDisabled = disabled.has(name) |
| 58 | + const icon = isDisabled ? "○" : "✓" |
| 59 | + const status = isDisabled ? "disabled" : "enabled" |
| 60 | + prompts.log.info(`${icon} ${name} ${UI.Style.TEXT_DIM}${status}\n ${UI.Style.TEXT_DIM}${plugin}`) |
| 61 | + } |
| 62 | + |
| 63 | + const enabledCount = plugins.filter((p) => !disabled.has(Config.getPluginName(p))).length |
| 64 | + prompts.outro(`${enabledCount}/${plugins.length} plugin(s) enabled`) |
| 65 | + }, |
| 66 | + }) |
| 67 | + }, |
| 68 | +}) |
| 69 | + |
| 70 | +export const PluginDisableCommand = cmd({ |
| 71 | + command: "disable <name>", |
| 72 | + describe: "disable a plugin", |
| 73 | + builder: (yargs) => |
| 74 | + yargs.positional("name", { |
| 75 | + describe: "name of the plugin to disable", |
| 76 | + type: "string", |
| 77 | + demandOption: true, |
| 78 | + }), |
| 79 | + async handler(args) { |
| 80 | + await Instance.provide({ |
| 81 | + directory: process.cwd(), |
| 82 | + async fn() { |
| 83 | + UI.empty() |
| 84 | + prompts.intro("Disable Plugin") |
| 85 | + |
| 86 | + const config = await Config.get() |
| 87 | + const plugins = config.plugin ?? [] |
| 88 | + const pluginName = args.name |
| 89 | + |
| 90 | + if (plugins.length === 0) { |
| 91 | + prompts.log.warn("No plugins configured") |
| 92 | + prompts.outro("Done") |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + const pluginEntry = plugins.find((p) => Config.getPluginName(p) === pluginName) |
| 97 | + if (!pluginEntry) { |
| 98 | + prompts.log.error(`Plugin not found: ${pluginName}`) |
| 99 | + prompts.outro("Done") |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + const disabled = config.disabled_plugins ?? [] |
| 104 | + if (disabled.includes(pluginName)) { |
| 105 | + prompts.log.warn(`Plugin "${pluginName}" is already disabled`) |
| 106 | + prompts.outro("Done") |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + const [projectConfigPath, globalConfigPath] = await Promise.all([ |
| 111 | + resolveConfigPath(Instance.worktree), |
| 112 | + resolveConfigPath(Global.Path.config, true), |
| 113 | + ]) |
| 114 | + |
| 115 | + const currentProject = Instance.project |
| 116 | + const configPath = currentProject.vcs === "git" ? projectConfigPath : globalConfigPath |
| 117 | + const nextDisabled = [...disabled, pluginName] |
| 118 | + await updateDisabledPlugins(nextDisabled, configPath) |
| 119 | + |
| 120 | + prompts.log.success(`Plugin "${pluginName}" disabled`) |
| 121 | + prompts.outro("Done") |
| 122 | + }, |
| 123 | + }) |
| 124 | + }, |
| 125 | +}) |
| 126 | + |
| 127 | +export const PluginEnableCommand = cmd({ |
| 128 | + command: "enable <name>", |
| 129 | + describe: "enable a previously disabled plugin", |
| 130 | + builder: (yargs) => |
| 131 | + yargs.positional("name", { |
| 132 | + describe: "name of the plugin to enable", |
| 133 | + type: "string", |
| 134 | + demandOption: true, |
| 135 | + }), |
| 136 | + async handler(args) { |
| 137 | + await Instance.provide({ |
| 138 | + directory: process.cwd(), |
| 139 | + async fn() { |
| 140 | + UI.empty() |
| 141 | + prompts.intro("Enable Plugin") |
| 142 | + |
| 143 | + const config = await Config.get() |
| 144 | + const disabled = config.disabled_plugins ?? [] |
| 145 | + const pluginName = args.name |
| 146 | + |
| 147 | + if (!disabled.includes(pluginName)) { |
| 148 | + prompts.log.warn(`Plugin "${pluginName}" is not disabled`) |
| 149 | + prompts.outro("Done") |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + const [projectConfigPath, globalConfigPath] = await Promise.all([ |
| 154 | + resolveConfigPath(Instance.worktree), |
| 155 | + resolveConfigPath(Global.Path.config, true), |
| 156 | + ]) |
| 157 | + |
| 158 | + const currentProject = Instance.project |
| 159 | + const configPath = currentProject.vcs === "git" ? projectConfigPath : globalConfigPath |
| 160 | + const nextDisabled = disabled.filter((p) => p !== pluginName) |
| 161 | + await updateDisabledPlugins(nextDisabled, configPath) |
| 162 | + |
| 163 | + prompts.log.success(`Plugin "${pluginName}" enabled`) |
| 164 | + prompts.outro("Done") |
| 165 | + }, |
| 166 | + }) |
| 167 | + }, |
| 168 | +}) |
0 commit comments