Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"jsonc-parser": "^3.3.1",
"picocolors": "^1.1.1",
"picomatch": "^4.0.2",
"smol-toml": "^1.6.0",
"vscode-jsonrpc": "^8.2.0",
"zod": "^4.1.8"
},
Expand Down
16 changes: 9 additions & 7 deletions src/cli/doctor/checks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@ import { existsSync, readFileSync } from "node:fs"
import { join } from "node:path"
import type { CheckResult, CheckDefinition, ConfigInfo } from "../types"
import { CHECK_IDS, CHECK_NAMES, PACKAGE_NAME } from "../constants"
import { parseJsonc, detectConfigFile, getOpenCodeConfigDir } from "../../../shared"
import { parseConfigContent, detectConfigFile, getOpenCodeConfigDir, type ConfigFormat } from "../../../shared"
import { OhMyOpenCodeConfigSchema } from "../../../config"

type DetectedConfigFormat = Exclude<ConfigFormat, "none">

const USER_CONFIG_DIR = getOpenCodeConfigDir({ binary: "opencode" })
const USER_CONFIG_BASE = join(USER_CONFIG_DIR, `${PACKAGE_NAME}`)
const PROJECT_CONFIG_BASE = join(process.cwd(), ".opencode", PACKAGE_NAME)

function findConfigPath(): { path: string; format: "json" | "jsonc" } | null {
function findConfigPath(): { path: string; format: DetectedConfigFormat } | null {
const projectDetected = detectConfigFile(PROJECT_CONFIG_BASE)
if (projectDetected.format !== "none") {
return { path: projectDetected.path, format: projectDetected.format as "json" | "jsonc" }
return { path: projectDetected.path, format: projectDetected.format as DetectedConfigFormat }
}

const userDetected = detectConfigFile(USER_CONFIG_BASE)
if (userDetected.format !== "none") {
return { path: userDetected.path, format: userDetected.format as "json" | "jsonc" }
return { path: userDetected.path, format: userDetected.format as DetectedConfigFormat }
}

return null
}

export function validateConfig(configPath: string): { valid: boolean; errors: string[] } {
export function validateConfig(configPath: string, format: DetectedConfigFormat): { valid: boolean; errors: string[] } {
try {
const content = readFileSync(configPath, "utf-8")
const rawConfig = parseJsonc<Record<string, unknown>>(content)
const rawConfig = parseConfigContent(content, format)
const result = OhMyOpenCodeConfigSchema.safeParse(rawConfig)

if (!result.success) {
Expand Down Expand Up @@ -68,7 +70,7 @@ export function getConfigInfo(): ConfigInfo {
}
}

const validation = validateConfig(configPath.path)
const validation = validateConfig(configPath.path, configPath.format)

return {
exists: true,
Expand Down
25 changes: 12 additions & 13 deletions src/cli/doctor/checks/model-resolution-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "node:fs"
import { join } from "node:path"
import { detectConfigFile, getOpenCodeConfigPaths, parseJsonc } from "../../../shared"
import { detectConfigFile, getOpenCodeConfigPaths, parseConfigContent, type ConfigFormat } from "../../../shared"
import type { OmoConfig } from "./model-resolution-types"

const PACKAGE_NAME = "oh-my-opencode"
Expand All @@ -10,25 +10,24 @@ const USER_CONFIG_BASE = join(
)
const PROJECT_CONFIG_BASE = join(process.cwd(), ".opencode", PACKAGE_NAME)

function loadConfigFromPath(path: string, format: Exclude<ConfigFormat, "none">): OmoConfig | null {
try {
const content = readFileSync(path, "utf-8")
return parseConfigContent<OmoConfig>(content, format)
} catch {
return null
}
}

export function loadOmoConfig(): OmoConfig | null {
const projectDetected = detectConfigFile(PROJECT_CONFIG_BASE)
if (projectDetected.format !== "none") {
try {
const content = readFileSync(projectDetected.path, "utf-8")
return parseJsonc<OmoConfig>(content)
} catch {
return null
}
return loadConfigFromPath(projectDetected.path, projectDetected.format as Exclude<ConfigFormat, "none">)
}

const userDetected = detectConfigFile(USER_CONFIG_BASE)
if (userDetected.format !== "none") {
try {
const content = readFileSync(userDetected.path, "utf-8")
return parseJsonc<OmoConfig>(content)
} catch {
return null
}
return loadConfigFromPath(userDetected.path, userDetected.format as Exclude<ConfigFormat, "none">)
}

return null
Expand Down
2 changes: 1 addition & 1 deletion src/cli/doctor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface PluginInfo {
export interface ConfigInfo {
exists: boolean
path: string | null
format: "json" | "jsonc" | null
format: "json" | "jsonc" | "toml" | null
valid: boolean
errors: string[]
}
Expand Down
19 changes: 12 additions & 7 deletions src/plugin-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import {
deepMerge,
getOpenCodeConfigDir,
addConfigLoadError,
parseJsonc,
detectConfigFile,
type ConfigFormat,
parseConfigContent,
migrateConfigFile,
type ConfigFileFormat,
} from "./shared";

export function loadConfigFromPath(
configPath: string,
format: ConfigFormat,
ctx: unknown
): OhMyOpenCodeConfig | null {
try {
if (fs.existsSync(configPath)) {
const content = fs.readFileSync(configPath, "utf-8");
const rawConfig = parseJsonc<Record<string, unknown>>(content);
const rawConfig = parseConfigContent(content, format as Exclude<ConfigFormat, "none">);

migrateConfigFile(configPath, rawConfig);
migrateConfigFile(configPath, rawConfig, format as ConfigFileFormat);

const result = OhMyOpenCodeConfigSchema.safeParse(rawConfig);

Expand Down Expand Up @@ -94,29 +97,31 @@ export function loadPluginConfig(
directory: string,
ctx: unknown
): OhMyOpenCodeConfig {
// User-level config path - prefer .jsonc over .json
// User-level config path - prefer .jsonc > .json > .toml
const configDir = getOpenCodeConfigDir({ binary: "opencode" });
const userBasePath = path.join(configDir, "oh-my-opencode");
const userDetected = detectConfigFile(userBasePath);
const userConfigPath =
userDetected.format !== "none"
? userDetected.path
: userBasePath + ".json";
const userConfigFormat = userDetected.format !== "none" ? userDetected.format : "json";

// Project-level config path - prefer .jsonc over .json
// Project-level config path - prefer .jsonc > .json > .toml
const projectBasePath = path.join(directory, ".opencode", "oh-my-opencode");
const projectDetected = detectConfigFile(projectBasePath);
const projectConfigPath =
projectDetected.format !== "none"
? projectDetected.path
: projectBasePath + ".json";
const projectConfigFormat = projectDetected.format !== "none" ? projectDetected.format : "json";

// Load user config first (base)
let config: OhMyOpenCodeConfig =
loadConfigFromPath(userConfigPath, ctx) ?? {};
loadConfigFromPath(userConfigPath, userConfigFormat, ctx) ?? {};

// Override with project config
const projectConfig = loadConfigFromPath(projectConfigPath, ctx);
const projectConfig = loadConfigFromPath(projectConfigPath, projectConfigFormat, ctx);
if (projectConfig) {
config = mergeConfigs(config, projectConfig);
}
Expand Down
118 changes: 118 additions & 0 deletions src/shared/config-detector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { describe, expect, test } from "bun:test"
import { detectConfigFile, type ConfigFormat } from "./config-detector"
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
import { join } from "node:path"

describe("detectConfigFile", () => {
const testDir = join(__dirname, ".test-config-detect")

test("prefers .jsonc over .json and .toml", () => {
// given
if (!existsSync(testDir)) mkdirSync(testDir, { recursive: true })
const basePath = join(testDir, "config")
writeFileSync(`${basePath}.json`, "{}")
writeFileSync(`${basePath}.jsonc`, "{}")
writeFileSync(`${basePath}.toml`, "key = \"value\"")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("jsonc")
expect(result.path).toBe(`${basePath}.jsonc`)

rmSync(testDir, { recursive: true, force: true })
})

test("prefers .json over .toml when .jsonc doesn't exist", () => {
// given
if (!existsSync(testDir)) mkdirSync(testDir, { recursive: true })
const basePath = join(testDir, "config")
writeFileSync(`${basePath}.json`, "{}")
writeFileSync(`${basePath}.toml`, "key = \"value\"")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("json")
expect(result.path).toBe(`${basePath}.json`)

rmSync(testDir, { recursive: true, force: true })
})

test("detects .toml when only .toml exists", () => {
// given
if (!existsSync(testDir)) mkdirSync(testDir, { recursive: true })
const basePath = join(testDir, "config")
writeFileSync(`${basePath}.toml`, "key = \"value\"")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("toml")
expect(result.path).toBe(`${basePath}.toml`)

rmSync(testDir, { recursive: true, force: true })
})

test("detects .jsonc when only .jsonc exists", () => {
// given
if (!existsSync(testDir)) mkdirSync(testDir, { recursive: true })
const basePath = join(testDir, "config")
writeFileSync(`${basePath}.jsonc`, "{}")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("jsonc")
expect(result.path).toBe(`${basePath}.jsonc`)

rmSync(testDir, { recursive: true, force: true })
})

test("detects .json when only .json exists", () => {
// given
if (!existsSync(testDir)) mkdirSync(testDir, { recursive: true })
const basePath = join(testDir, "config")
writeFileSync(`${basePath}.json`, "{}")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("json")
expect(result.path).toBe(`${basePath}.json`)

rmSync(testDir, { recursive: true, force: true })
})

test("returns none when no config files exist", () => {
// given
const basePath = join(testDir, "nonexistent")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("none")
expect(result.path).toBe(`${basePath}.json`)
})

test("returns correct path type for jsonc", () => {
// given
if (!existsSync(testDir)) mkdirSync(testDir, { recursive: true })
const basePath = join(testDir, "config")
writeFileSync(`${basePath}.jsonc`, "{}")

// when
const result = detectConfigFile(basePath)

// then
expect(result.format).toBe("jsonc" as ConfigFormat)

rmSync(testDir, { recursive: true, force: true })
})
})
31 changes: 31 additions & 0 deletions src/shared/config-detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { existsSync } from "node:fs"
import { parseJsonc } from "./jsonc-parser"
import { parseToml } from "./toml-parser"

export type ConfigFormat = "json" | "jsonc" | "toml" | "none"

const CONFIG_EXTENSIONS = ["jsonc", "json", "toml"] as const

export function detectConfigFile(basePath: string): {
format: ConfigFormat
path: string
} {
for (const ext of CONFIG_EXTENSIONS) {
const path = `${basePath}.${ext}`
if (existsSync(path)) {
return { format: ext, path }
}
}

return { format: "none", path: `${basePath}.json` }
}

export function parseConfigContent<T = Record<string, unknown>>(
content: string,
format: Exclude<ConfigFormat, "none">
): T {
if (format === "toml") {
return parseToml<T>(content)
}
return parseJsonc<T>(content)
}
2 changes: 2 additions & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export * from "./data-path"
export * from "./config-errors"
export * from "./claude-config-dir"
export * from "./jsonc-parser"
export * from "./toml-parser"
export * from "./config-detector"
export * from "./migration"
export * from "./opencode-config-dir"
export type {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/jsonc-parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from "bun:test"
import { detectConfigFile, parseJsonc, parseJsoncSafe, readJsoncFile } from "./jsonc-parser"
import { parseJsonc, parseJsoncSafe, readJsoncFile } from "./jsonc-parser"
import { detectConfigFile } from "./config-detector"
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
import { join } from "node:path"

Expand Down
18 changes: 1 addition & 17 deletions src/shared/jsonc-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync } from "node:fs"
import { readFileSync } from "node:fs"
import { parse, ParseError, printParseErrorCode } from "jsonc-parser"

export interface JsoncParseResult<T> {
Expand Down Expand Up @@ -48,19 +48,3 @@ export function readJsoncFile<T = unknown>(filePath: string): T | null {
return null
}
}

export function detectConfigFile(basePath: string): {
format: "json" | "jsonc" | "none"
path: string
} {
const jsoncPath = `${basePath}.jsonc`
const jsonPath = `${basePath}.json`

if (existsSync(jsoncPath)) {
return { format: "jsonc", path: jsoncPath }
}
if (existsSync(jsonPath)) {
return { format: "json", path: jsonPath }
}
return { format: "none", path: jsonPath }
}
2 changes: 1 addition & 1 deletion src/shared/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { AGENT_NAME_MAP, BUILTIN_AGENT_NAMES, migrateAgentNames } from "./migrat
export { HOOK_NAME_MAP, migrateHookNames } from "./migration/hook-names"
export { MODEL_VERSION_MAP, migrateModelVersions } from "./migration/model-versions"
export { MODEL_TO_CATEGORY_MAP, migrateAgentConfigToCategory, shouldDeleteAgentConfig } from "./migration/agent-category"
export { migrateConfigFile } from "./migration/config-migration"
export { migrateConfigFile, type ConfigFileFormat } from "./migration/config-migration"
Loading