global.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import path from "path"
  2. import fs from "fs/promises"
  3. import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir"
  4. import os from "os"
  5. import { Context, Effect, Layer } from "effect"
  6. import { Flock } from "./util/flock"
  7. const app = "opencode"
  8. const data = path.join(xdgData!, app)
  9. const cache = path.join(xdgCache!, app)
  10. const config = path.join(xdgConfig!, app)
  11. const state = path.join(xdgState!, app)
  12. const paths = {
  13. get home() {
  14. return process.env.OPENCODE_TEST_HOME ?? os.homedir()
  15. },
  16. data,
  17. bin: path.join(cache, "bin"),
  18. log: path.join(data, "log"),
  19. cache,
  20. config,
  21. state,
  22. }
  23. export const Path = paths
  24. Flock.setGlobal({ state })
  25. await Promise.all([
  26. fs.mkdir(Path.data, { recursive: true }),
  27. fs.mkdir(Path.config, { recursive: true }),
  28. fs.mkdir(Path.state, { recursive: true }),
  29. fs.mkdir(Path.log, { recursive: true }),
  30. fs.mkdir(Path.bin, { recursive: true }),
  31. ])
  32. export class Service extends Context.Service<Service, Interface>()("@opencode/Global") {}
  33. export interface Interface {
  34. readonly home: string
  35. readonly data: string
  36. readonly cache: string
  37. readonly config: string
  38. readonly state: string
  39. readonly bin: string
  40. readonly log: string
  41. }
  42. export const layer = Layer.effect(
  43. Service,
  44. Effect.gen(function* () {
  45. return Service.of({
  46. home: Path.home,
  47. data: Path.data,
  48. cache: Path.cache,
  49. config: Path.config,
  50. state: Path.state,
  51. bin: Path.bin,
  52. log: Path.log,
  53. })
  54. }),
  55. )
  56. export * as Global from "./global"