refactor(config-manager): remove legacy config path detection and migration logic

🤖 Generated with assistance of OhMyOpenCode
This commit is contained in:
YeonGyu-Kim
2026-04-05 11:23:08 +09:00
parent 98c2f92251
commit 852859ed9c
5 changed files with 81 additions and 62 deletions
+1 -21
View File
@@ -1,13 +1,8 @@
import { basename, extname, join } from "node:path"
import { getOpenCodeConfigPaths } from "../../shared"
import { detectPluginConfigFile } from "../../shared/jsonc-parser"
import { migrateLegacyConfigFile } from "../../shared/migrate-legacy-config-file"
import type {
OpenCodeBinaryType,
OpenCodeConfigPaths,
} from "../../shared/opencode-config-dir-types"
import { CONFIG_BASENAME, LEGACY_CONFIG_BASENAME } from "../../shared/plugin-identity"
export interface ConfigContext {
binary: OpenCodeBinaryType
@@ -47,20 +42,5 @@ export function getConfigJsonc(): string {
}
export function getOmoConfigPath(): string {
const configDir = getConfigContext().paths.configDir
const detectedConfig = detectPluginConfigFile(configDir)
if (
detectedConfig.format !== "none"
&& basename(detectedConfig.path).startsWith(LEGACY_CONFIG_BASENAME)
) {
const canonicalPath = join(
configDir,
`${CONFIG_BASENAME}${extname(detectedConfig.path)}`,
)
const migrated = migrateLegacyConfigFile(detectedConfig.path)
return migrated ? canonicalPath : detectedConfig.path
}
return detectedConfig.path
return getConfigContext().paths.omoConfig
}
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
@@ -34,7 +34,7 @@ describe("writeOmoConfig", () => {
beforeEach(() => {
testConfigDir = join(tmpdir(), `omo-write-config-${Date.now()}-${Math.random().toString(36).slice(2)}`)
testConfigPath = join(testConfigDir, "oh-my-openagent.json")
testConfigPath = join(testConfigDir, "oh-my-opencode.json")
mkdirSync(testConfigDir, { recursive: true })
process.env.OPENCODE_CONFIG_DIR = testConfigDir
@@ -77,41 +77,4 @@ describe("writeOmoConfig", () => {
expect(savedConfig).toHaveProperty(defaultKey)
}
})
it("migrates legacy oh-my-opencode.json to canonical oh-my-openagent.json before merging", () => {
// given
const legacyConfigPath = join(testConfigDir, "oh-my-opencode.json")
const canonicalConfigPath = join(testConfigDir, "oh-my-openagent.json")
const legacyBackupPath = `${legacyConfigPath}.bak`
writeFileSync(
legacyConfigPath,
JSON.stringify(
{
agents: {
sisyphus: {
model: "custom/provider-model",
},
},
},
null,
2,
) + "\n",
"utf-8",
)
// when
const result = writeOmoConfig(installConfig)
// then
expect(result.success).toBe(true)
expect(result.configPath).toEndWith("oh-my-openagent.json")
expect(existsSync(legacyConfigPath)).toBe(false)
expect(existsSync(legacyBackupPath)).toBe(true)
expect(existsSync(canonicalConfigPath)).toBe(true)
const savedConfig = parseJsonc<Record<string, unknown>>(readFileSync(canonicalConfigPath, "utf-8"))
const savedAgents = getRecord(savedConfig.agents)
const savedSisyphus = getRecord(savedAgents.sisyphus)
expect(savedSisyphus.model).toBe("custom/provider-model")
})
})
+32
View File
@@ -1,4 +1,7 @@
import { describe, it, expect } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import * as config from "./config"
describe("config check", () => {
@@ -23,5 +26,34 @@ describe("config check", () => {
//#then issues should be an array (possibly empty)
expect(Array.isArray(result.issues)).toBe(true)
})
it("respects OPENCODE_CONFIG_DIR even when the env var changes after module import", async () => {
const originalConfigDir = process.env.OPENCODE_CONFIG_DIR
const testConfigDir = join(
tmpdir(),
`omo-doctor-config-${Date.now()}-${Math.random().toString(36).slice(2)}`,
)
try {
mkdirSync(testConfigDir, { recursive: true })
process.env.OPENCODE_CONFIG_DIR = testConfigDir
writeFileSync(
join(testConfigDir, "oh-my-openagent.json"),
JSON.stringify({ disabled_hooks: ["comment-checker"] }, null, 2) + "\n",
"utf-8",
)
const result = await config.checkConfig()
expect(result.details?.[0]).toEndWith("/oh-my-openagent.json")
} finally {
rmSync(testConfigDir, { recursive: true, force: true })
if (originalConfigDir === undefined) {
delete process.env.OPENCODE_CONFIG_DIR
} else {
process.env.OPENCODE_CONFIG_DIR = originalConfigDir
}
}
})
})
})
@@ -0,0 +1,45 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { loadOmoConfig } from "./model-resolution-config"
describe("model-resolution-config", () => {
let originalConfigDir: string | undefined
beforeEach(() => {
originalConfigDir = process.env.OPENCODE_CONFIG_DIR
})
afterEach(() => {
if (originalConfigDir === undefined) {
delete process.env.OPENCODE_CONFIG_DIR
} else {
process.env.OPENCODE_CONFIG_DIR = originalConfigDir
}
})
it("respects OPENCODE_CONFIG_DIR even when the env var changes after module import", () => {
const testConfigDir = join(
tmpdir(),
`omo-model-resolution-config-${Date.now()}-${Math.random().toString(36).slice(2)}`,
)
try {
mkdirSync(testConfigDir, { recursive: true })
process.env.OPENCODE_CONFIG_DIR = testConfigDir
writeFileSync(
join(testConfigDir, "oh-my-openagent.json"),
JSON.stringify({ agents: { atlas: { model: "opencode-go/kimi-k2.5" } } }, null, 2) + "\n",
"utf-8",
)
const config = loadOmoConfig()
expect(config?.agents?.atlas?.model).toBe("opencode-go/kimi-k2.5")
} finally {
rmSync(testConfigDir, { recursive: true, force: true })
}
})
})
+1 -2
View File
@@ -7,7 +7,6 @@ import type {
OpenCodeConfigDirOptions,
OpenCodeConfigPaths,
} from "./opencode-config-dir-types"
import { CONFIG_BASENAME } from "./plugin-identity"
export type {
OpenCodeBinaryType,
@@ -98,7 +97,7 @@ export function getOpenCodeConfigPaths(options: OpenCodeConfigDirOptions): OpenC
configJson: join(configDir, "opencode.json"),
configJsonc: join(configDir, "opencode.jsonc"),
packageJson: join(configDir, "package.json"),
omoConfig: join(configDir, `${CONFIG_BASENAME}.json`),
omoConfig: join(configDir, "oh-my-opencode.json"),
}
}