|
|
|
@@ -17,42 +17,39 @@ import { InstallationLocal, InstallationVersion } from "@/installation/version"
|
|
|
|
|
import { makeRuntime } from "@/cli/effect/runtime"
|
|
|
|
|
import { Filesystem, Log } from "@/util"
|
|
|
|
|
|
|
|
|
|
const log = Log.create({ service: "tui.config" })
|
|
|
|
|
export namespace TuiConfig {
|
|
|
|
|
const log = Log.create({ service: "tui.config" })
|
|
|
|
|
|
|
|
|
|
export const Info = TuiInfo
|
|
|
|
|
export const Info = TuiInfo
|
|
|
|
|
|
|
|
|
|
type Acc = {
|
|
|
|
|
type Acc = {
|
|
|
|
|
result: Info
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
|
type State = {
|
|
|
|
|
config: Info
|
|
|
|
|
deps: Array<Fiber.Fiber<void, AppFileSystem.Error>>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Info = z.output<typeof Info> & {
|
|
|
|
|
export type Info = z.output<typeof Info> & {
|
|
|
|
|
// Internal resolved plugin list used by runtime loading.
|
|
|
|
|
plugin_origins?: ConfigPlugin.Origin[]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Interface {
|
|
|
|
|
export interface Interface {
|
|
|
|
|
readonly get: () => Effect.Effect<Info>
|
|
|
|
|
readonly waitForDependencies: () => Effect.Effect<void>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/TuiConfig") {}
|
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/TuiConfig") {}
|
|
|
|
|
|
|
|
|
|
function pluginScope(file: string, ctx: { directory: string }): ConfigPlugin.Scope {
|
|
|
|
|
function pluginScope(file: string, ctx: { directory: string }): ConfigPlugin.Scope {
|
|
|
|
|
if (Filesystem.contains(ctx.directory, file)) return "local"
|
|
|
|
|
// if (ctx.worktree !== "/" && Filesystem.contains(ctx.worktree, file)) return "local"
|
|
|
|
|
return "global"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function customPath() {
|
|
|
|
|
return Flag.OPENCODE_TUI_CONFIG
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalize(raw: Record<string, unknown>) {
|
|
|
|
|
function normalize(raw: Record<string, unknown>) {
|
|
|
|
|
const data = { ...raw }
|
|
|
|
|
if (!("tui" in data)) return data
|
|
|
|
|
if (!isRecord(data.tui)) {
|
|
|
|
@@ -66,17 +63,17 @@ function normalize(raw: Record<string, unknown>) {
|
|
|
|
|
...tui,
|
|
|
|
|
...data,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function resolvePlugins(config: Info, configFilepath: string) {
|
|
|
|
|
async function resolvePlugins(config: Info, configFilepath: string) {
|
|
|
|
|
if (!config.plugin) return config
|
|
|
|
|
for (let i = 0; i < config.plugin.length; i++) {
|
|
|
|
|
config.plugin[i] = await ConfigPlugin.resolvePluginSpec(config.plugin[i], configFilepath)
|
|
|
|
|
}
|
|
|
|
|
return config
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function mergeFile(acc: Acc, file: string, ctx: { directory: string }) {
|
|
|
|
|
async function mergeFile(acc: Acc, file: string, ctx: { directory: string }) {
|
|
|
|
|
const data = await loadFile(file)
|
|
|
|
|
acc.result = mergeDeep(acc.result, data)
|
|
|
|
|
if (!data.plugin?.length) return
|
|
|
|
@@ -88,33 +85,43 @@ async function mergeFile(acc: Acc, file: string, ctx: { directory: string }) {
|
|
|
|
|
])
|
|
|
|
|
acc.result.plugin = plugins.map((item) => item.spec)
|
|
|
|
|
acc.result.plugin_origins = plugins
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadState(ctx: { directory: string }) {
|
|
|
|
|
let projectFiles = Flag.OPENCODE_DISABLE_PROJECT_CONFIG ? [] : await ConfigPaths.projectFiles("tui", ctx.directory)
|
|
|
|
|
async function loadState(ctx: { directory: string }) {
|
|
|
|
|
// Every config dir we may read from: global config dir, any `.opencode`
|
|
|
|
|
// folders between cwd and home, and OPENCODE_CONFIG_DIR.
|
|
|
|
|
const directories = await ConfigPaths.directories(ctx.directory)
|
|
|
|
|
const custom = customPath()
|
|
|
|
|
await migrateTuiConfig({ directories, custom, cwd: ctx.directory })
|
|
|
|
|
// Re-compute after migration since migrateTuiConfig may have created new tui.json files
|
|
|
|
|
projectFiles = Flag.OPENCODE_DISABLE_PROJECT_CONFIG ? [] : await ConfigPaths.projectFiles("tui", ctx.directory)
|
|
|
|
|
// One-time migration: extract tui keys (theme/keybinds/tui) from existing
|
|
|
|
|
// opencode.json files into sibling tui.json files.
|
|
|
|
|
await migrateTuiConfig({ directories, cwd: ctx.directory })
|
|
|
|
|
|
|
|
|
|
const projectFiles = Flag.OPENCODE_DISABLE_PROJECT_CONFIG
|
|
|
|
|
? []
|
|
|
|
|
: await ConfigPaths.projectFiles("tui", ctx.directory)
|
|
|
|
|
|
|
|
|
|
const acc: Acc = {
|
|
|
|
|
result: {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. Global tui config (lowest precedence).
|
|
|
|
|
for (const file of ConfigPaths.fileInDirectory(Global.Path.config, "tui")) {
|
|
|
|
|
await mergeFile(acc, file, ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (custom) {
|
|
|
|
|
await mergeFile(acc, custom, ctx)
|
|
|
|
|
log.debug("loaded custom tui config", { path: custom })
|
|
|
|
|
// 2. Explicit OPENCODE_TUI_CONFIG override, if set.
|
|
|
|
|
if (Flag.OPENCODE_TUI_CONFIG) {
|
|
|
|
|
await mergeFile(acc, Flag.OPENCODE_TUI_CONFIG, ctx)
|
|
|
|
|
log.debug("loaded custom tui config", { path: Flag.OPENCODE_TUI_CONFIG })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Project tui files, applied root-first so the closest file wins.
|
|
|
|
|
for (const file of projectFiles) {
|
|
|
|
|
await mergeFile(acc, file, ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. `.opencode` directories (and OPENCODE_CONFIG_DIR) discovered while
|
|
|
|
|
// walking up the tree. Also returned below so callers can install plugin
|
|
|
|
|
// dependencies from each location.
|
|
|
|
|
const dirs = unique(directories).filter((dir) => dir.endsWith(".opencode") || dir === Flag.OPENCODE_CONFIG_DIR)
|
|
|
|
|
|
|
|
|
|
for (const dir of dirs) {
|
|
|
|
@@ -139,9 +146,9 @@ async function loadState(ctx: { directory: string }) {
|
|
|
|
|
config: acc.result,
|
|
|
|
|
dirs: acc.result.plugin?.length ? dirs : [],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const layer = Layer.effect(
|
|
|
|
|
export const layer = Layer.effect(
|
|
|
|
|
Service,
|
|
|
|
|
Effect.gen(function* () {
|
|
|
|
|
const directory = yield* CurrentWorkingDirectory
|
|
|
|
@@ -167,30 +174,30 @@ export const layer = Layer.effect(
|
|
|
|
|
)
|
|
|
|
|
return Service.of({ get, waitForDependencies })
|
|
|
|
|
}).pipe(Effect.withSpan("TuiConfig.layer")),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const defaultLayer = layer.pipe(Layer.provide(Npm.defaultLayer))
|
|
|
|
|
export const defaultLayer = layer.pipe(Layer.provide(Npm.defaultLayer))
|
|
|
|
|
|
|
|
|
|
const { runPromise } = makeRuntime(Service, defaultLayer)
|
|
|
|
|
const { runPromise } = makeRuntime(Service, defaultLayer)
|
|
|
|
|
|
|
|
|
|
export async function waitForDependencies() {
|
|
|
|
|
export async function waitForDependencies() {
|
|
|
|
|
await runPromise((svc) => svc.waitForDependencies())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function get() {
|
|
|
|
|
export async function get() {
|
|
|
|
|
return runPromise((svc) => svc.get())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadFile(filepath: string): Promise<Info> {
|
|
|
|
|
async function loadFile(filepath: string): Promise<Info> {
|
|
|
|
|
const text = await ConfigPaths.readFile(filepath)
|
|
|
|
|
if (!text) return {}
|
|
|
|
|
return load(text, filepath).catch((error) => {
|
|
|
|
|
log.warn("failed to load tui config", { path: filepath, error })
|
|
|
|
|
return {}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function load(text: string, configFilepath: string): Promise<Info> {
|
|
|
|
|
async function load(text: string, configFilepath: string): Promise<Info> {
|
|
|
|
|
return ConfigParse.load(Info, text, {
|
|
|
|
|
type: "path",
|
|
|
|
|
path: configFilepath,
|
|
|
|
@@ -208,6 +215,5 @@ async function load(text: string, configFilepath: string): Promise<Info> {
|
|
|
|
|
log.warn("invalid tui config", { path: configFilepath, error })
|
|
|
|
|
return {}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export * as TuiConfig from "./tui"
|
|
|
|
|