global_config.cljs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (ns frontend.handler.global-config
  2. "This ns is a system component that encapsulates global config functionality.
  3. Unlike repo config, this also manages a directory for configuration. This
  4. component depends on a repo."
  5. (:require [frontend.fs :as fs]
  6. [frontend.handler.common.file :as file-common-handler]
  7. [frontend.state :as state]
  8. [cljs.reader :as reader]
  9. [promesa.core :as p]
  10. [shadow.resource :as rc]
  11. [electron.ipc :as ipc]
  12. ["path" :as path]))
  13. ;; Use defonce to avoid broken state on dev reload
  14. ;; Also known as home directory a.k.a. '~'
  15. (defonce root-dir
  16. (atom nil))
  17. (defn global-config-dir
  18. []
  19. (path/join @root-dir "config"))
  20. (defn global-config-path
  21. []
  22. (path/join @root-dir "config" "config.edn"))
  23. (defn- set-global-config-state!
  24. [content]
  25. (let [config (reader/read-string content)]
  26. (state/set-global-config! config)
  27. config))
  28. (def default-content (rc/inline "global-config.edn"))
  29. (defn- create-global-config-file-if-not-exists
  30. [repo-url]
  31. (let [config-dir (global-config-dir)
  32. config-path (global-config-path)]
  33. (p/let [_ (fs/mkdir-if-not-exists config-dir)
  34. file-exists? (fs/create-if-not-exists repo-url config-dir config-path default-content)]
  35. (when-not file-exists?
  36. (file-common-handler/reset-file! repo-url config-path default-content)
  37. (set-global-config-state! default-content)))))
  38. (defn restore-global-config!
  39. "Sets global config state from config file"
  40. []
  41. (let [config-dir (global-config-dir)
  42. config-path (global-config-path)]
  43. (p/let [config-content (fs/read-file config-dir config-path)]
  44. (set-global-config-state! config-content))))
  45. (defn start
  46. "This component has four responsibilities on start:
  47. - Fetch root-dir for later use with config paths
  48. - Manage ui state of global config
  49. - Create a global config dir and file if it doesn't exist
  50. - Start a file watcher for global config dir if it's not already started.
  51. Watcher ensures client db is seeded with correct file data."
  52. [{:keys [repo]}]
  53. (p/let [root-dir' (ipc/ipc "getLogseqDotDirRoot")
  54. _ (reset! root-dir root-dir')
  55. _ (restore-global-config!)
  56. _ (create-global-config-file-if-not-exists repo)
  57. _ (fs/watch-dir! (global-config-dir) {:global-dir true})]))