index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. const app = "opencode"
  6. const data = path.join(xdgData!, app)
  7. const cache = path.join(xdgCache!, app)
  8. const config = path.join(xdgConfig!, app)
  9. const state = path.join(xdgState!, app)
  10. export namespace Global {
  11. export const Path = {
  12. home: os.homedir(),
  13. data,
  14. bin: path.join(data, "bin"),
  15. log: path.join(data, "log"),
  16. cache,
  17. config,
  18. state,
  19. } as const
  20. }
  21. await Promise.all([
  22. fs.mkdir(Global.Path.data, { recursive: true }),
  23. fs.mkdir(Global.Path.config, { recursive: true }),
  24. fs.mkdir(Global.Path.state, { recursive: true }),
  25. fs.mkdir(Global.Path.log, { recursive: true }),
  26. fs.mkdir(Global.Path.bin, { recursive: true }),
  27. ])
  28. const CACHE_VERSION = "9"
  29. const version = await Bun.file(path.join(Global.Path.cache, "version"))
  30. .text()
  31. .catch(() => "0")
  32. if (version !== CACHE_VERSION) {
  33. try {
  34. const contents = await fs.readdir(Global.Path.cache)
  35. await Promise.all(
  36. contents.map((item) =>
  37. fs.rm(path.join(Global.Path.cache, item), {
  38. recursive: true,
  39. force: true,
  40. }),
  41. ),
  42. )
  43. } catch (e) {}
  44. await Bun.file(path.join(Global.Path.cache, "version")).write(CACHE_VERSION)
  45. }