refactor(config-manager): remove legacy config path detection and migration logic
🤖 Generated with assistance of OhMyOpenCode
This commit is contained in:
@@ -1,13 +1,8 @@
|
|||||||
import { basename, extname, join } from "node:path"
|
|
||||||
|
|
||||||
import { getOpenCodeConfigPaths } from "../../shared"
|
import { getOpenCodeConfigPaths } from "../../shared"
|
||||||
import { detectPluginConfigFile } from "../../shared/jsonc-parser"
|
|
||||||
import { migrateLegacyConfigFile } from "../../shared/migrate-legacy-config-file"
|
|
||||||
import type {
|
import type {
|
||||||
OpenCodeBinaryType,
|
OpenCodeBinaryType,
|
||||||
OpenCodeConfigPaths,
|
OpenCodeConfigPaths,
|
||||||
} from "../../shared/opencode-config-dir-types"
|
} from "../../shared/opencode-config-dir-types"
|
||||||
import { CONFIG_BASENAME, LEGACY_CONFIG_BASENAME } from "../../shared/plugin-identity"
|
|
||||||
|
|
||||||
export interface ConfigContext {
|
export interface ConfigContext {
|
||||||
binary: OpenCodeBinaryType
|
binary: OpenCodeBinaryType
|
||||||
@@ -47,20 +42,5 @@ export function getConfigJsonc(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getOmoConfigPath(): string {
|
export function getOmoConfigPath(): string {
|
||||||
const configDir = getConfigContext().paths.configDir
|
return getConfigContext().paths.omoConfig
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
|
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 { tmpdir } from "node:os"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ describe("writeOmoConfig", () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
testConfigDir = join(tmpdir(), `omo-write-config-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
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 })
|
mkdirSync(testConfigDir, { recursive: true })
|
||||||
process.env.OPENCODE_CONFIG_DIR = testConfigDir
|
process.env.OPENCODE_CONFIG_DIR = testConfigDir
|
||||||
@@ -77,41 +77,4 @@ describe("writeOmoConfig", () => {
|
|||||||
expect(savedConfig).toHaveProperty(defaultKey)
|
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")
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import { describe, it, expect } from "bun:test"
|
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"
|
import * as config from "./config"
|
||||||
|
|
||||||
describe("config check", () => {
|
describe("config check", () => {
|
||||||
@@ -23,5 +26,34 @@ describe("config check", () => {
|
|||||||
//#then issues should be an array (possibly empty)
|
//#then issues should be an array (possibly empty)
|
||||||
expect(Array.isArray(result.issues)).toBe(true)
|
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 })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -7,7 +7,6 @@ import type {
|
|||||||
OpenCodeConfigDirOptions,
|
OpenCodeConfigDirOptions,
|
||||||
OpenCodeConfigPaths,
|
OpenCodeConfigPaths,
|
||||||
} from "./opencode-config-dir-types"
|
} from "./opencode-config-dir-types"
|
||||||
import { CONFIG_BASENAME } from "./plugin-identity"
|
|
||||||
|
|
||||||
export type {
|
export type {
|
||||||
OpenCodeBinaryType,
|
OpenCodeBinaryType,
|
||||||
@@ -98,7 +97,7 @@ export function getOpenCodeConfigPaths(options: OpenCodeConfigDirOptions): OpenC
|
|||||||
configJson: join(configDir, "opencode.json"),
|
configJson: join(configDir, "opencode.json"),
|
||||||
configJsonc: join(configDir, "opencode.jsonc"),
|
configJsonc: join(configDir, "opencode.jsonc"),
|
||||||
packageJson: join(configDir, "package.json"),
|
packageJson: join(configDir, "package.json"),
|
||||||
omoConfig: join(configDir, `${CONFIG_BASENAME}.json`),
|
omoConfig: join(configDir, "oh-my-opencode.json"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user