Replace env-paths with xdg-basedir for better XDG compliance and cross-platform directory handling

🤖 Generated with opencode
Co-Authored-By: opencode <noreply@opencode.ai>
This commit is contained in:
Dax Raad 2025-06-03 14:46:28 -04:00
parent 9ad0477af6
commit 8e769dcac0
7 changed files with 30 additions and 22 deletions

View file

@ -37,6 +37,7 @@
"turndown": "7.2.0",
"vscode-jsonrpc": "8.2.1",
"vscode-languageclient": "8",
"xdg-basedir": "5.1.0",
"zod": "catalog:",
"zod-openapi": "4.2.4",
},
@ -1489,6 +1490,8 @@
"wrappy": ["wrappy@1.0.2", "", {}, "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="],
"xdg-basedir": ["xdg-basedir@5.1.0", "", {}, "sha512-GCPAHLvrIH13+c0SuacwvRYj2SxJXQ4kaVTT5xgL3kPrz56XxkF21IGhjSE1+W0aw7gpBWRGXLCPnPby6lSpmQ=="],
"xml2js": ["xml2js@0.6.2", "", { "dependencies": { "sax": ">=0.6.0", "xmlbuilder": "~11.0.0" } }, "sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA=="],
"xmlbuilder": ["xmlbuilder@11.0.1", "", {}, "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA=="],

View file

@ -36,6 +36,7 @@
"turndown": "7.2.0",
"vscode-jsonrpc": "8.2.1",
"vscode-languageclient": "8",
"xdg-basedir": "5.1.0",
"zod": "catalog:",
"zod-openapi": "4.2.4"
}

View file

@ -34,7 +34,7 @@ export namespace App {
x ? path.dirname(x) : undefined,
)
const data = path.join(Global.data(), git ?? "global")
const data = path.join(Global.Path.data, git ?? "global")
const stateFile = Bun.file(path.join(data, APP_JSON))
const state = (await stateFile.json().catch(() => ({}))) as {
initialized: number

View file

@ -1,24 +1,23 @@
import envpaths from "env-paths"
import fs from "fs/promises"
const paths = envpaths("opencode", {
suffix: "",
})
import { xdgData, xdgCache, xdgConfig } from "xdg-basedir"
import path from "path"
const app = "opencode"
const data = path.join(xdgData!, app)
const cache = path.join(xdgCache!, app)
const config = path.join(xdgConfig!, app)
await Promise.all([
fs.mkdir(paths.config, { recursive: true }),
fs.mkdir(paths.cache, { recursive: true }),
fs.mkdir(data, { recursive: true }),
fs.mkdir(config, { recursive: true }),
fs.mkdir(cache, { recursive: true }),
])
export namespace Global {
export function config() {
return paths.config
}
export function cache() {
return paths.cache
}
export function data() {
return paths.data
}
export const Path = {
data,
cache,
config,
} as const
}

View file

@ -27,7 +27,7 @@ cli.command("", "Start the opencode in interactive mode").action(async () => {
let cwd = new URL("../../tui/cmd/opencode", import.meta.url).pathname
if (Bun.embeddedFiles.length > 0) {
const blob = Bun.embeddedFiles[0] as File
const binary = path.join(Global.cache(), "tui", blob.name)
const binary = path.join(Global.Path.cache, "tui", blob.name)
const file = Bun.file(binary)
if (!(await file.exists())) {
console.log("installing tui binary...")

View file

@ -95,13 +95,18 @@ export namespace Provider {
const s = await state()
if (s.sdk.has(providerID)) return s.sdk.get(providerID)!
const dir = path.join(Global.cache(), `node_modules`, `@ai-sdk`, providerID)
const dir = path.join(
Global.Path.cache,
`node_modules`,
`@ai-sdk`,
providerID,
)
if (!(await Bun.file(path.join(dir, "package.json")).exists())) {
log.info("installing", {
providerID,
})
BunProc.run(["add", `@ai-sdk/${providerID}@alpha`], {
cwd: Global.cache(),
cwd: Global.Path.cache,
})
}
const mod = await import(path.join(dir))

View file

@ -177,7 +177,7 @@ export namespace Server {
root: app.path.root,
data: app.path.data,
cwd: app.path.cwd,
config: Global.config(),
config: Global.Path.data,
})
},
)