|
1 | | -export * as Global from "./global" |
| 1 | +import fs from "fs/promises" |
| 2 | +import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir" |
| 3 | +import path from "path" |
| 4 | +import os from "os" |
| 5 | +import { Filesystem } from "../util" |
| 6 | +import { Flock } from "@opencode-ai/shared/util/flock" |
| 7 | + |
| 8 | +const app = "opencode" |
| 9 | + |
| 10 | +const data = path.join(xdgData!, app) |
| 11 | +const cache = path.join(xdgCache!, app) |
| 12 | +const config = path.join(xdgConfig!, app) |
| 13 | +const state = path.join(xdgState!, app) |
| 14 | + |
| 15 | +export const Path = { |
| 16 | + // Allow override via OPENCODE_TEST_HOME for test isolation |
| 17 | + get home() { |
| 18 | + return process.env.OPENCODE_TEST_HOME || os.homedir() |
| 19 | + }, |
| 20 | + data, |
| 21 | + bin: path.join(cache, "bin"), |
| 22 | + log: path.join(data, "log"), |
| 23 | + cache, |
| 24 | + config, |
| 25 | + state, |
| 26 | +} |
| 27 | + |
| 28 | +// Initialize Flock with global state path |
| 29 | +Flock.setGlobal({ state }) |
| 30 | + |
| 31 | +await Promise.all([ |
| 32 | + fs.mkdir(Path.data, { recursive: true }), |
| 33 | + fs.mkdir(Path.config, { recursive: true }), |
| 34 | + fs.mkdir(Path.state, { recursive: true }), |
| 35 | + fs.mkdir(Path.log, { recursive: true }), |
| 36 | + fs.mkdir(Path.bin, { recursive: true }), |
| 37 | +]) |
| 38 | + |
| 39 | +const CACHE_VERSION = "21" |
| 40 | + |
| 41 | +const version = await Filesystem.readText(path.join(Path.cache, "version")).catch(() => "0") |
| 42 | + |
| 43 | +if (version !== CACHE_VERSION) { |
| 44 | + try { |
| 45 | + const contents = await fs.readdir(Path.cache) |
| 46 | + await Promise.all( |
| 47 | + contents.map((item) => |
| 48 | + fs.rm(path.join(Path.cache, item), { |
| 49 | + recursive: true, |
| 50 | + force: true, |
| 51 | + }), |
| 52 | + ), |
| 53 | + ) |
| 54 | + } catch {} |
| 55 | + await Filesystem.write(path.join(Path.cache, "version"), CACHE_VERSION) |
| 56 | +} |
| 57 | + |
| 58 | +export * as Global from "." |
0 commit comments