fix(cli): update existing plugin entry instead of skipping

Addresses cubic review feedback: installer now replaces existing
oh-my-opencode entries with the new version-aware entry, allowing
users to switch between @latest, @beta, or pinned versions.
This commit is contained in:
aw338WoWmUI
2026-01-11 13:01:50 +08:00
committed by Spark
parent c29e6f0213
commit 1a5fdb3338

View File

@@ -237,11 +237,18 @@ export async function addPluginToOpenCodeConfig(currentVersion: string): Promise
const config = parseResult.config
const plugins = config.plugin ?? []
if (plugins.some((p) => p.startsWith(PACKAGE_NAME))) {
return { success: true, configPath: path }
const existingIndex = plugins.findIndex((p) => p === PACKAGE_NAME || p.startsWith(`${PACKAGE_NAME}@`))
if (existingIndex !== -1) {
if (plugins[existingIndex] === pluginEntry) {
return { success: true, configPath: path }
}
plugins[existingIndex] = pluginEntry
} else {
plugins.push(pluginEntry)
}
config.plugin = [...plugins, pluginEntry]
config.plugin = plugins
if (format === "jsonc") {
const content = readFileSync(path, "utf-8")
@@ -249,11 +256,8 @@ export async function addPluginToOpenCodeConfig(currentVersion: string): Promise
const match = content.match(pluginArrayRegex)
if (match) {
const arrayContent = match[1].trim()
const newArrayContent = arrayContent
? `${arrayContent},\n "${pluginEntry}"`
: `"${pluginEntry}"`
const newContent = content.replace(pluginArrayRegex, `"plugin": [\n ${newArrayContent}\n ]`)
const formattedPlugins = plugins.map((p) => `"${p}"`).join(",\n ")
const newContent = content.replace(pluginArrayRegex, `"plugin": [\n ${formattedPlugins}\n ]`)
writeFileSync(path, newContent)
} else {
const newContent = content.replace(/^(\s*\{)/, `$1\n "plugin": ["${pluginEntry}"],`)