fix(fs-watcher): notify all files, skip read non-text files

This commit is contained in:
Andelf
2022-05-20 11:52:35 +08:00
committed by Tienson Qin
parent 04df7fa881
commit 1e3a628c31
4 changed files with 41 additions and 34 deletions

View File

@@ -4,7 +4,6 @@
["chokidar" :as watcher]
[electron.utils :as utils]
["electron" :refer [app]]
[frontend.util.fs :as util-fs]
[electron.window :as window]))
;; TODO: explore different solutions for different platforms
@@ -30,10 +29,15 @@
(defn- publish-file-event!
[dir path event]
(send-file-watcher! dir event {:dir (utils/fix-win-path! dir)
:path (utils/fix-win-path! path)
:content (utils/read-file path)
:stat (fs/statSync path)}))
(let [content (when (and (not= event "unlink")
(utils/should-read-content? path))
(utils/read-file path))
stat (when (not= event "unlink")
(fs/statSync path))]
(send-file-watcher! dir event {:dir (utils/fix-win-path! dir)
:path (utils/fix-win-path! path)
:content content
:stat stat})))
(defn watch-dir!
"Watch a directory if no such file watcher exists"
@@ -43,7 +47,7 @@
(let [watcher (.watch watcher dir
(clj->js
{:ignored (fn [path]
(util-fs/ignored-path? dir path))
(utils/ignored-path? dir path))
:ignoreInitial false
:ignorePermissionErrors true
:interval polling-interval
@@ -63,9 +67,7 @@
(publish-file-event! dir path "change")))
(.on watcher "unlink"
(fn [path]
(send-file-watcher! dir "unlink"
{:dir (utils/fix-win-path! dir)
:path (utils/fix-win-path! path)})))
(publish-file-event! dir path "unlink")))
(.on watcher "error"
(fn [path]
(println "Watch error happened: "

View File

@@ -62,27 +62,27 @@
(when-let [agent (cfgs/get-item :settings/agent)]
(set-fetch-agent agent)))
;; keep same as ignored-path? in src/main/frontend/util/fs.cljs
;; TODO: merge them
(defn ignored-path?
"Ignore given path from file-watcher notification"
[dir path]
(when (string? path)
(or
(some #(string/starts-with? path (str dir "/" %))
["." ".recycle" "assets" "node_modules" "logseq/bak"])
["." ".recycle" "node_modules" "logseq/bak" "version-files"])
(some #(string/includes? path (str "/" % "/"))
["." ".recycle" "assets" "node_modules" "logseq/bak"])
(string/ends-with? path ".DS_Store")
["." ".recycle" "node_modules" "logseq/bak" "version-files"])
(some #(string/ends-with? path %)
[".DS_Store" "logseq/graphs-txid.edn" "logseq/broken-config.edn"])
;; hidden directory or file
(let [relpath (path/relative dir path)]
(or (re-find #"/\.[^.]+" relpath)
(re-find #"^\.[^.]+" relpath)))
(let [path (string/lower-case path)]
(and
(not (string/blank? (path/extname path)))
(not
(some #(string/ends-with? path %)
[".md" ".markdown" ".org" ".js" ".edn" ".css"])))))))
(re-find #"^\.[^.]+" relpath))))))
(defn should-read-content?
"Skip reading content of file while using file-watcher"
[path]
(let [ext (string/lower-case (path/extname path))]
(contains? #{".md" ".markdown" ".org" ".js" ".edn" ".css"} ext)))
(defn fix-win-path!
[path]
@@ -131,4 +131,4 @@
(defn normalize-lc
[s]
(normalize (string/lower-case s)))
(normalize (string/lower-case s)))

View File

@@ -26,12 +26,12 @@
[frontend.encrypt :as encrypt]))
(defn remove-ignore-files
[files]
[files dir-name nfs?]
(let [files (remove (fn [f]
(let [path (:file/path f)]
(or (string/starts-with? path ".git/")
(string/includes? path ".git/")
(and (util-fs/ignored-path? "" path)
(and (util-fs/ignored-path? (if nfs? "" dir-name) path)
(not= (:file/name f) ".gitignore")))))
files)]
(if-let [ignore-file (some #(when (= (:file/name %) ".gitignore")
@@ -55,7 +55,7 @@
:file/last-modified-at mtime
:file/size size
:file/content content})
result)
result)
electron?
(map (fn [{:keys [path stat content]}]
@@ -64,7 +64,7 @@
:file/last-modified-at mtime
:file/size size
:file/content content}))
result)
result)
:else
(let [result (flatten (bean/->clj result))]
@@ -147,7 +147,7 @@
(nfs/add-nfs-file-handle! root-handle-path root-handle))
result (nth result 1)
files (-> (->db-files mobile-native? electron? dir-name result)
remove-ignore-files)
(remove-ignore-files dir-name nfs?))
_ (when nfs?
(let [file-paths (set (map :file/path files))]
(swap! path-handles (fn [handles]
@@ -297,7 +297,7 @@
(when nfs?
(swap! path-handles assoc path handle))))
new-files (-> (->db-files mobile-native? electron? dir-name files-result)
remove-ignore-files)
(remove-ignore-files dir-name nfs?))
_ (when nfs?
(let [file-paths (set (map :file/path new-files))]
(swap! path-handles (fn [handles]

View File

@@ -4,17 +4,22 @@
;; TODO: move all file path related util functions to here
;; keep same as ignored-path? in src/electron/electron/utils.cljs
;; TODO: merge them
;; NOTE: This is not the same ignored-path? as src/electron/electron/utils.cljs.
;; The assets directory is ignored.
;;
;; When in nfs-mode, dir is "", path is relative path to graph dir.
;; When in native-mode, dir and path are absolute paths.
(defn ignored-path?
"Ignore path for ls-dir-files-with-handler! and reload-dir!"
[dir path]
(when (string? path)
(or
(some #(string/starts-with? path (str dir "/" %))
["." ".recycle" "assets" "node_modules" "logseq/bak"])
["." ".recycle" "assets" "node_modules" "logseq/bak" "version-files"])
(some #(string/includes? path (str "/" % "/"))
["." ".recycle" "assets" "node_modules" "logseq/bak"])
(string/ends-with? path ".DS_Store")
["." ".recycle" "assets" "node_modules" "logseq/bak" "version-files"])
(some #(string/ends-with? path %)
[".DS_Store" "logseq/graphs-txid.edn" "logseq/broken-config.edn"])
;; hidden directory or file
(let [relpath (path/relative dir path)]
(or (re-find #"/\.[^.]+" relpath)
@@ -24,4 +29,4 @@
(not (string/blank? (path/extname path)))
(not
(some #(string/ends-with? path %)
[".md" ".markdown" ".org" ".js" ".edn" ".css"])))))))
[".md" ".markdown" ".org" ".js" ".edn" ".css"])))))))