Merge pull request #3092 from code-yeongyu/fix/prepublish-legacy-config

fix(shared): close legacy config migration gaps
This commit is contained in:
YeonGyu-Kim
2026-04-04 01:46:52 +09:00
committed by GitHub
15 changed files with 243 additions and 95 deletions
@@ -0,0 +1,2 @@
export { autoMigrateLegacyPluginEntry } from "./auto-migrate"
export type { MigrationResult } from "./auto-migrate"
@@ -1,7 +1,14 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
const mockMigrateLegacyPluginEntry = mock(() => true)
mock.module("./plugin-entry-migrator", () => ({
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
}))
async function importFreshAutoMigrateModule(): Promise<typeof import("./auto-migrate")> {
return import(`./auto-migrate?test=${Date.now()}-${Math.random()}`)
}
@@ -12,6 +19,8 @@ describe("autoMigrateLegacyPluginEntry", () => {
beforeEach(() => {
testConfigDir = join(tmpdir(), `omo-legacy-migrate-${Date.now()}-${Math.random().toString(36).slice(2)}`)
mkdirSync(testConfigDir, { recursive: true })
mockMigrateLegacyPluginEntry.mockReset()
mockMigrateLegacyPluginEntry.mockReturnValue(true)
})
afterEach(() => {
@@ -35,8 +44,7 @@ describe("autoMigrateLegacyPluginEntry", () => {
expect(result.migrated).toBe(true)
expect(result.from).toBe("oh-my-opencode")
expect(result.to).toBe("oh-my-openagent")
const saved = JSON.parse(readFileSync(join(testConfigDir, "opencode.json"), "utf-8"))
expect(saved.plugin).toEqual(["oh-my-openagent"])
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith(join(testConfigDir, "opencode.json"))
})
})
@@ -57,8 +65,7 @@ describe("autoMigrateLegacyPluginEntry", () => {
expect(result.migrated).toBe(true)
expect(result.from).toBe("oh-my-opencode@3.10.0")
expect(result.to).toBe("oh-my-openagent@3.10.0")
const saved = JSON.parse(readFileSync(join(testConfigDir, "opencode.json"), "utf-8"))
expect(saved.plugin).toEqual(["oh-my-openagent@3.10.0"])
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith(join(testConfigDir, "opencode.json"))
})
})
@@ -77,8 +84,8 @@ describe("autoMigrateLegacyPluginEntry", () => {
// then
expect(result.migrated).toBe(true)
const saved = JSON.parse(readFileSync(join(testConfigDir, "opencode.json"), "utf-8"))
expect(saved.plugin).toEqual(["oh-my-openagent"])
expect(result.to).toBe("oh-my-openagent")
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith(join(testConfigDir, "opencode.json"))
})
})
@@ -93,6 +100,7 @@ describe("autoMigrateLegacyPluginEntry", () => {
// then
expect(result.migrated).toBe(false)
expect(result.from).toBeNull()
expect(mockMigrateLegacyPluginEntry).not.toHaveBeenCalled()
})
})
@@ -111,10 +119,35 @@ describe("autoMigrateLegacyPluginEntry", () => {
// then
expect(result.migrated).toBe(true)
const content = readFileSync(join(testConfigDir, "opencode.jsonc"), "utf-8")
expect(content).toContain("// my config")
expect(content).toContain("oh-my-openagent")
expect(content).not.toContain("oh-my-opencode")
expect(result.to).toBe("oh-my-openagent")
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith(join(testConfigDir, "opencode.jsonc"))
})
})
describe("#given opencode.jsonc has a nested plugin key before the root plugin array", () => {
it("#then migrates only the root plugin entry", async () => {
// given
writeFileSync(
join(testConfigDir, "opencode.jsonc"),
`{
"nested": {
"plugin": ["oh-my-opencode"]
},
"plugin": ["oh-my-opencode@latest"]
}
`,
)
const { autoMigrateLegacyPluginEntry } = await importFreshAutoMigrateModule()
// when
const result = autoMigrateLegacyPluginEntry(testConfigDir)
// then
expect(result.migrated).toBe(true)
expect(result.from).toBe("oh-my-opencode@latest")
expect(result.to).toBe("oh-my-openagent@latest")
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith(join(testConfigDir, "opencode.jsonc"))
})
})
@@ -131,8 +164,7 @@ describe("autoMigrateLegacyPluginEntry", () => {
// then
expect(result.migrated).toBe(false)
const content = readFileSync(join(testConfigDir, "opencode.json"), "utf-8")
expect(content).toBe(original)
expect(mockMigrateLegacyPluginEntry).not.toHaveBeenCalled()
})
})
})
+9 -20
View File
@@ -1,9 +1,10 @@
import { existsSync, readFileSync, writeFileSync } from "node:fs"
import { existsSync, readFileSync } from "node:fs"
import { join } from "node:path"
import { parseJsoncSafe } from "../../shared/jsonc-parser"
import { getOpenCodeConfigPaths } from "../../shared/opencode-config-dir"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
import { migrateLegacyPluginEntry } from "./plugin-entry-migrator"
export interface MigrationResult {
migrated: boolean
@@ -63,27 +64,15 @@ export function autoMigrateLegacyPluginEntry(overrideConfigDir?: string): Migrat
const hasCanonical = plugins.some(isCanonicalEntry)
const from = legacyEntries[0]
const to = toLegacyCanonical(from)
const migrated = migrateLegacyPluginEntry(configPath)
if (!migrated) return { migrated: false, from: null, to: null, configPath }
const normalized = hasCanonical
? plugins.filter((p) => !isLegacyEntry(p))
: plugins.map((p) => (isLegacyEntry(p) ? toLegacyCanonical(p) : p))
const isJsonc = configPath.endsWith(".jsonc")
if (isJsonc) {
const pluginArrayRegex = /((?:"plugin"|plugin)\s*:\s*)\[([\s\S]*?)\]/
const match = content.match(pluginArrayRegex)
if (match) {
const formattedPlugins = normalized.map((p) => `"${p}"`).join(",\n ")
const newContent = content.replace(pluginArrayRegex, `$1[\n ${formattedPlugins}\n ]`)
writeFileSync(configPath, newContent)
return { migrated: true, from, to, configPath }
}
return {
migrated: true,
from,
to: hasCanonical ? PLUGIN_NAME : to,
configPath,
}
const parsed = JSON.parse(content) as Record<string, unknown>
parsed.plugin = normalized
writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n")
return { migrated: true, from, to, configPath }
} catch {
return { migrated: false, from: null, to: null, configPath }
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
import type { MigrationResult } from "./auto-migrate"
import type { MigrationResult } from "./auto-migrate-runner"
const mockCheckForLegacyPluginEntry = mock(() => ({
hasLegacyEntry: false,
@@ -26,7 +26,7 @@ mock.module("../../shared/logger", () => ({
log: mockLog,
}))
mock.module("./auto-migrate", () => ({
mock.module("./auto-migrate-runner", () => ({
autoMigrateLegacyPluginEntry: mockAutoMigrate,
}))
+1 -1
View File
@@ -3,7 +3,7 @@ import type { PluginInput } from "@opencode-ai/plugin"
import { checkForLegacyPluginEntry } from "../../shared/legacy-plugin-warning"
import { log } from "../../shared/logger"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
import { autoMigrateLegacyPluginEntry } from "./auto-migrate"
import { autoMigrateLegacyPluginEntry } from "./auto-migrate-runner"
export function createLegacyPluginToastHook(ctx: PluginInput) {
let fired = false
@@ -0,0 +1 @@
export { migrateLegacyPluginEntry } from "../../shared/migrate-legacy-plugin-entry"