Browse Source

fix(watcher): handle init load error

Andelf 2 years ago
parent
commit
f6961b4b1c

+ 6 - 3
src/main/frontend/fs/memory_fs.cljs

@@ -52,14 +52,17 @@
     (when js/window.pfs
       (let [fpath (path/url-to-path dir)]
         (-> (js/window.pfs.mkdir fpath)
-            (p/catch (fn [error] (println "Mkdir error: " error)))))))
+            (p/catch (fn [error] (println "(memory-fs)Mkdir error: " error)))))))
   (readdir [_this dir]
     (when js/window.pfs
       (let [fpath (path/url-to-path dir)]
         (-> (<readdir fpath)
             (p/then (fn [rpaths]
                       (prn ::debug rpaths)
-                      (mapv #(path/path-join "memory://" %) rpaths)))))))
+                      (mapv #(path/path-join "memory://" %) rpaths)))
+            (p/catch (fn [error]
+                       (println "(memory-fs)Readdir error: " error)
+                       (p/rejected error)))))))
 
   (unlink! [_this _repo path opts]
     (when js/window.pfs
@@ -88,7 +91,7 @@
   (stat [_this fpath]
     (let [fpath (path/url-to-path fpath)]
       (js/window.pfs.stat fpath)))
-  
+
   (open-dir [_this _dir]
     nil)
   (get-files [_this _path-or-handle]

+ 25 - 10
src/main/frontend/fs/watcher_handler.cljs

@@ -129,14 +129,29 @@
 (defn load-graph-files!
   [graph]
   (when graph
-    (let [dir (config/get-repo-dir graph)
+    (let [repo-dir (config/get-repo-dir graph)
           db-files (->> (db/get-files graph)
-                        (map first)
-                        (filter #(string/starts-with? % (config/get-repo-dir graph))))]
-      (p/let [files (fs/readdir dir :path-only? true)
-              files (map #(path/relative-path dir %) files)
-              files (remove #(fs-util/ignored-path? dir %) files)
-              deleted-files (set/difference (set db-files) (set files))]
+                        (map first))]
+      ;; read all files in the repo dir, notify if readdir error
+      (p/let [[files deleted-files]
+              (-> (fs/readdir repo-dir :path-only? true)
+                  (p/chain (fn [files]
+                             (->> files
+                                  (map #(path/relative-path repo-dir %))
+                                  (remove #(fs-util/ignored-path? repo-dir %))))
+                           (fn [files]
+                             (let [deleted-files (set/difference (set db-files) (set files))]
+                               [files deleted-files])))
+                  (p/catch (fn [error]
+                             (when-not (config/demo-graph? graph)
+                               (js/console.error "reading" graph)
+                               (state/pub-event! [:notification/show
+                                                  {:content (str "The graph " graph " can not be read:" error)
+                                                   :status :error
+                                                   :clear? false}]))
+                             [nil nil])))]
+        (prn ::init-watcher repo-dir {:deleted (count deleted-files)
+                                      :total (count files)})
         (when (seq deleted-files)
           (let [delete-tx-data (->> (db/delete-files deleted-files)
                                     (concat (db/delete-blocks graph deleted-files nil))
@@ -146,13 +161,13 @@
           (prn ::init-watcher file-rpath)
           (when-let [_ext (util/get-file-ext file-rpath)]
             (->
-             (p/let [content (fs/read-file dir file-rpath)
-                     stat (fs/stat dir file-rpath)
+             (p/let [content (fs/read-file repo-dir file-rpath)
+                     stat (fs/stat repo-dir file-rpath)
                      type (if (db/file-exists? graph file-rpath)
                             "change"
                             "add")]
                (handle-changed! type
-                                {:dir dir
+                                {:dir repo-dir
                                  :path file-rpath
                                  :content content
                                  :stat stat}))

+ 2 - 2
src/main/frontend/handler.cljs

@@ -79,7 +79,6 @@
 (defn restore-and-setup!
   [repos]
   (when-let [repo (or (state/get-current-repo) (:url (first repos)))]
-    (prn "restore-and-setup!" repo)
     (-> (db/restore! repo)
         (p/then
          (fn []
@@ -90,7 +89,8 @@
             (p/do! (repo-config-handler/start {:repo repo})
                    (when (config/global-config-enabled?)
                      (global-config-handler/start {:repo repo}))
-                   (when (config/plugin-config-enabled?) (plugin-config-handler/start)))
+                   (when (config/plugin-config-enabled?)
+                     (plugin-config-handler/start)))
             (p/finally
               (fn []
                 ;; install after config is restored

+ 2 - 1
src/main/frontend/handler/events.cljs

@@ -390,8 +390,9 @@
         (state/pub-event! [:graph/dir-gone dir]))))
   ;; FIXME: an ugly implementation for redirecting to page on new window is restored
   (repo-handler/graph-ready! repo)
+  ;; Replace initial fs watcher
   (fs-watcher/load-graph-files! repo)
-  ;; TODO: Notify user to update filename format when the UX is smooth enough
+  ;; TODO(junyi): Notify user to update filename format when the UX is smooth enough
   ;; (when-not config/test?
   ;;   (js/setTimeout
   ;;    (fn []

+ 1 - 3
src/main/frontend/handler/file.cljs

@@ -139,9 +139,7 @@
                            from-disk? false
                            skip-compare? false}}]
   (let [path (gp-util/path-normalize path)
-        _ (prn ::alter-file path)
-        ;; _ (js/console.trace)
-        config-file? (= path "logseq/config.edn") ; (string/ends-with? path config/config-file)
+        config-file? (= path "logseq/config.edn")
         _ (when config-file?
             (detect-deprecations repo path content))
         config-valid? (and config-file? (validate-file repo path content))]

+ 5 - 6
src/main/frontend/handler/plugin_config.cljs

@@ -4,7 +4,7 @@
   global-config and plugin components are enabled. plugin.edn is automatically updated
 when a plugin is installed, updated or removed"
   (:require [frontend.handler.global-config :as global-config-handler]
-            ["path" :as path]
+            [logseq.common.path :as path]
             [promesa.core :as p]
             [borkdude.rewrite-edn :as rewrite]
             [frontend.fs :as fs]
@@ -23,7 +23,7 @@ when a plugin is installed, updated or removed"
 (defn plugin-config-path
   "Full path to plugins.edn"
   []
-  (path/join (global-config-handler/global-config-dir) "plugins.edn"))
+  (path/path-join (global-config-handler/global-config-dir) "plugins.edn"))
 
 (def common-plugin-keys
   "Vec of plugin keys to store in plugins.edn and to compare with installed-plugins state"
@@ -32,7 +32,7 @@ when a plugin is installed, updated or removed"
 (defn add-or-update-plugin
   "Adds or updates a plugin from plugin.edn"
   [{:keys [id] :as plugin}]
-  (p/let [content (fs/read-file "" (plugin-config-path))
+  (p/let [content (fs/read-file nil (plugin-config-path))
           updated-content (-> content
                               rewrite/parse-string
                               (rewrite/assoc (keyword id) (select-keys plugin common-plugin-keys))
@@ -52,8 +52,7 @@ when a plugin is installed, updated or removed"
                     (update-vals #(select-keys % common-plugin-keys))
                     pprint/pprint
                     with-out-str)]
-    (prn ::plugin-dir @global-config-handler/root-dir (plugin-config-path))
-    (fs/create-if-not-exists nil @global-config-handler/root-dir (plugin-config-path) content)))
+    (fs/create-if-not-exists "" nil (plugin-config-path) content)))
 
 (defn- determine-plugins-to-change
   "Given installed plugins state and plugins from plugins.edn,
@@ -76,7 +75,7 @@ returns map of plugins to install and uninstall"
 (defn open-replace-plugins-modal
   []
   (p/catch
-   (p/let [edn-plugins* (fs/read-file "" (plugin-config-path))
+   (p/let [edn-plugins* (fs/read-file nil (plugin-config-path))
            edn-plugins (edn/read-string edn-plugins*)]
           (if-let [errors (->> edn-plugins (m/explain plugin-config-schema/Plugins-edn) me/humanize)]
             (do

+ 1 - 3
src/main/frontend/handler/ui.cljs

@@ -122,9 +122,7 @@
   (when-let [style (or
                     (state/get-custom-css-link)
                     (some-> (db-model/get-custom-css)
-                            (config/expand-relative-assets-path))
-                    ;; (state/get-custom-css-link)
-                    )]
+                            (config/expand-relative-assets-path)))]
     (util/add-style! style)))
 (defn reset-custom-css!
   []