Files
oh-my-openagent/src/shared/data-path.ts
YeonGyu-Kim 919f7e4092 fix(data-path): writable directory fallback for data/cache paths
getDataDir() and getCacheDir() now verify the directory is writable and
fall back to os.tmpdir() if not.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 11:46:07 +09:00

63 lines
1.9 KiB
TypeScript

import * as path from "node:path"
import * as os from "node:os"
import { accessSync, constants, mkdirSync } from "node:fs"
function resolveWritableDirectory(preferredDir: string, fallbackSuffix: string): string {
try {
mkdirSync(preferredDir, { recursive: true })
accessSync(preferredDir, constants.W_OK)
return preferredDir
} catch {
const fallbackDir = path.join(os.tmpdir(), fallbackSuffix)
mkdirSync(fallbackDir, { recursive: true })
return fallbackDir
}
}
/**
* Returns the user-level data directory.
* Matches OpenCode's behavior via xdg-basedir:
* - All platforms: XDG_DATA_HOME or ~/.local/share
*
* Note: OpenCode uses xdg-basedir which returns ~/.local/share on ALL platforms
* including Windows, so we match that behavior exactly.
*/
export function getDataDir(): string {
const preferredDir = process.env.XDG_DATA_HOME ?? path.join(os.homedir(), ".local", "share")
return resolveWritableDirectory(preferredDir, "opencode-data")
}
/**
* Returns the OpenCode storage directory path.
* All platforms: ~/.local/share/opencode/storage
*/
export function getOpenCodeStorageDir(): string {
return path.join(getDataDir(), "opencode", "storage")
}
/**
* Returns the user-level cache directory.
* Matches OpenCode's behavior via xdg-basedir:
* - All platforms: XDG_CACHE_HOME or ~/.cache
*/
export function getCacheDir(): string {
const preferredDir = process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), ".cache")
return resolveWritableDirectory(preferredDir, "opencode-cache")
}
/**
* Returns the oh-my-opencode cache directory.
* All platforms: ~/.cache/oh-my-opencode
*/
export function getOmoOpenCodeCacheDir(): string {
return path.join(getCacheDir(), "oh-my-opencode")
}
/**
* Returns the OpenCode cache directory (for reading OpenCode's cache).
* All platforms: ~/.cache/opencode
*/
export function getOpenCodeCacheDir(): string {
return path.join(getCacheDir(), "opencode")
}