From 705f792e87ac695b64879cda18f2f18d3ace68e3 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Sat, 25 Apr 2026 13:51:37 -0400 Subject: core: move Global module to @opencode-ai/core for centralized path management Move the Global module from packages/opencode/src/global to packages/core/src/global to provide a unified location for managing XDG directories and application paths. This eliminates duplicate path definitions across packages and ensures consistent access to data, config, cache, state, log, and bin directories throughout the codebase. --- packages/core/src/global.ts | 70 +++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 28 deletions(-) (limited to 'packages/core') diff --git a/packages/core/src/global.ts b/packages/core/src/global.ts index bf605618f..0c83e3a1f 100644 --- a/packages/core/src/global.ts +++ b/packages/core/src/global.ts @@ -1,7 +1,9 @@ import path from "path" +import fs from "fs/promises" import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir" import os from "os" import { Context, Effect, Layer } from "effect" +import { Flock } from "./util/flock" const app = "opencode" const data = path.join(xdgData!, app) @@ -9,7 +11,7 @@ const cache = path.join(xdgCache!, app) const config = path.join(xdgConfig!, app) const state = path.join(xdgState!, app) -export const Path = { +const paths = { get home() { return process.env.OPENCODE_TEST_HOME ?? os.homedir() }, @@ -21,31 +23,43 @@ export const Path = { state, } -export namespace Global { - export class Service extends Context.Service()("@opencode/Global") {} - - export interface Interface { - readonly home: string - readonly data: string - readonly cache: string - readonly config: string - readonly state: string - readonly bin: string - readonly log: string - } - - export const layer = Layer.effect( - Service, - Effect.gen(function* () { - return Service.of({ - home: Path.home, - data: Path.data, - cache: Path.cache, - config: Path.config, - state: Path.state, - bin: Path.bin, - log: Path.log, - }) - }), - ) +export const Path = paths + +Flock.setGlobal({ state }) + +await Promise.all([ + fs.mkdir(Path.data, { recursive: true }), + fs.mkdir(Path.config, { recursive: true }), + fs.mkdir(Path.state, { recursive: true }), + fs.mkdir(Path.log, { recursive: true }), + fs.mkdir(Path.bin, { recursive: true }), +]) + +export class Service extends Context.Service()("@opencode/Global") {} + +export interface Interface { + readonly home: string + readonly data: string + readonly cache: string + readonly config: string + readonly state: string + readonly bin: string + readonly log: string } + +export const layer = Layer.effect( + Service, + Effect.gen(function* () { + return Service.of({ + home: Path.home, + data: Path.data, + cache: Path.cache, + config: Path.config, + state: Path.state, + bin: Path.bin, + log: Path.log, + }) + }), +) + +export * as Global from "./global" -- cgit v1.2.3