core: modularize config parsing to improve maintainability

Extract error handling, parsing logic, and variable substitution into dedicated
modules. This reduces duplication between tui.json and opencode.json parsing
and makes the config system easier to extend for future config formats.
This commit is contained in:
Dax Raad
2026-04-16 13:29:00 -04:00
parent 9dd99d19d5
commit 85ff86fe9e
11 changed files with 335 additions and 273 deletions
+9 -7
View File
@@ -1,7 +1,6 @@
import { existsSync } from "fs"
import os from "os"
import path from "path"
import { type Info, parseConfig } from "./config"
import { Log, Process } from "../util"
const log = Log.create({ service: "config" })
@@ -33,16 +32,16 @@ function managedConfigDir() {
return process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR || systemManagedConfigDir()
}
function parseManagedPlist(json: string, source: string): Info {
function parseManagedPlist(json: string): string {
const raw = JSON.parse(json)
for (const key of Object.keys(raw)) {
if (PLIST_META.has(key)) delete raw[key]
}
return parseConfig(JSON.stringify(raw), source)
return JSON.stringify(raw)
}
async function readManagedPreferences(): Promise<Info> {
if (process.platform !== "darwin") return {}
async function readManagedPreferences() {
if (process.platform !== "darwin") return
const user = os.userInfo().username
const paths = [
@@ -58,10 +57,13 @@ async function readManagedPreferences(): Promise<Info> {
log.warn("failed to convert managed preferences plist", { path: plist })
continue
}
return parseManagedPlist(result.stdout.toString(), `mobileconfig:${plist}`)
return {
source: `mobileconfig:${plist}`,
text: parseManagedPlist(result.stdout.toString()),
}
}
return {}
return
}
export const ConfigManaged = {