node.cljs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. (ns frontend.fs.node
  2. "Implementation of fs protocol for Electron, based on nodejs"
  3. (:require [cljs-bean.core :as bean]
  4. [clojure.string :as string]
  5. [electron.ipc :as ipc]
  6. [frontend.config :as config]
  7. [frontend.db :as db]
  8. [frontend.fs.protocol :as protocol]
  9. [frontend.util :as util]
  10. [lambdaisland.glogi :as log]
  11. [logseq.common.path :as path]
  12. [promesa.core :as p]))
  13. (defn- <contents-matched?
  14. [disk-content db-content]
  15. (when (and (string? disk-content) (string? db-content))
  16. (p/resolved (= (string/trim disk-content) (string/trim db-content)))))
  17. (defn- write-file-impl!
  18. [repo dir rpath content {:keys [ok-handler error-handler old-content skip-compare?]} stat]
  19. (let [file-fpath (path/path-join dir rpath)]
  20. (if skip-compare?
  21. (p/catch
  22. (p/let [result (ipc/ipc "writeFile" repo file-fpath content)]
  23. (when ok-handler
  24. (ok-handler repo rpath result)))
  25. (fn [error]
  26. (if error-handler
  27. (error-handler error)
  28. (log/error :write-file-failed error))))
  29. (p/let [disk-content (when (not= stat :not-found)
  30. (-> (ipc/ipc "readFile" file-fpath)
  31. (p/then bean/->clj)
  32. (p/catch (fn [error]
  33. (js/console.error error)
  34. nil))))
  35. disk-content (or disk-content "")
  36. db-content (or old-content (db/get-file repo rpath) "")
  37. contents-matched? (<contents-matched? disk-content db-content)]
  38. (->
  39. (p/let [result (ipc/ipc "writeFile" repo file-fpath content)]
  40. (when-not contents-matched?
  41. (ipc/ipc "backupDbFile" (config/get-local-dir repo) rpath disk-content content))
  42. (when ok-handler
  43. (ok-handler repo rpath result))
  44. result)
  45. (p/catch (fn [error]
  46. (if error-handler
  47. (error-handler error)
  48. (log/error :write-file-failed error)))))))))
  49. (defn- open-dir
  50. "Open a new directory"
  51. [dir]
  52. (p/let [dir-path (or dir (util/mocked-open-dir-path))
  53. result (if dir-path
  54. (do
  55. (println "NOTE: Using mocked dir" dir-path)
  56. (ipc/ipc "getFiles" dir-path))
  57. (ipc/ipc "openDir" {}))
  58. result (bean/->clj result)]
  59. result))
  60. (defn- wrap-throw-ex-info
  61. [p]
  62. (p/catch p (fn [e] (throw (ex-info (str e) {})))))
  63. (defrecord Node []
  64. protocol/Fs
  65. (mkdir! [_this dir]
  66. (-> (ipc/ipc "mkdir" dir)
  67. (p/then (fn [_] (js/console.log (str "Directory created: " dir))))
  68. (p/catch (fn [error]
  69. (when-not (string/includes? (str error) "EEXIST")
  70. (js/console.error (str "Error creating directory: " dir) error))))))
  71. (mkdir-recur! [_this dir]
  72. (ipc/ipc "mkdir-recur" dir))
  73. (readdir [_this dir] ; recursive
  74. (wrap-throw-ex-info
  75. (p/then (ipc/ipc "readdir" dir)
  76. bean/->clj)))
  77. (unlink! [_this repo path _opts]
  78. (ipc/ipc "unlink"
  79. (config/get-repo-dir repo)
  80. path))
  81. (rmdir! [_this _dir]
  82. ;; !Too dangerous! We'll never implement this.
  83. nil)
  84. (read-file [_this dir path _options]
  85. (let [path (if (nil? dir)
  86. path
  87. (path/path-join dir path))]
  88. (wrap-throw-ex-info (ipc/ipc "readFile" path))))
  89. (read-file-raw [_this dir path _options]
  90. (let [path (if (nil? dir)
  91. path
  92. (path/path-join dir path))]
  93. (wrap-throw-ex-info (ipc/ipc "readFileRaw" path))))
  94. (write-file! [this repo dir path content opts]
  95. (p/let [fpath (path/path-join dir path)
  96. stat (p/catch
  97. (protocol/stat this fpath)
  98. (fn [_e] :not-found))
  99. parent-dir (path/parent fpath)
  100. _ (protocol/mkdir-recur! this parent-dir)]
  101. (write-file-impl! repo dir path content opts stat)))
  102. (rename! [_this _repo old-path new-path]
  103. (ipc/ipc "rename" old-path new-path))
  104. ;; copy with overwrite, without confirmation
  105. (copy! [_this repo old-path new-path]
  106. (ipc/ipc "copyFile" repo old-path new-path))
  107. (stat [_this fpath]
  108. (-> (ipc/ipc "stat" fpath)
  109. (p/then bean/->clj)))
  110. (open-dir [_this dir]
  111. (open-dir dir))
  112. (get-files [_this dir]
  113. (-> (ipc/ipc "getFiles" dir)
  114. (p/then (fn [result]
  115. (:files (bean/->clj result))))))
  116. (watch-dir! [_this dir options]
  117. (ipc/ipc "addDirWatcher" dir options))
  118. (unwatch-dir! [_this dir]
  119. (ipc/ipc "unwatchDir" dir)))