Przeglądaj źródła

fix(config): global config validation

Andelf 2 lat temu
rodzic
commit
9637c464eb

+ 1 - 2
src/main/frontend/components/file.cljs

@@ -96,7 +96,6 @@
                                     [nil path])]
                    (when (and format (contains? (gp-config/text-formats) format))
                      (p/let [content (fs/read-file dir path)]
-                       (prn ::read-content dir path content)
                        (reset! *content (or content ""))))
                    (assoc state ::file-content *content)))
    :did-mount (fn [state]
@@ -110,7 +109,7 @@
         rel-path (when (string/starts-with? path repo-dir)
                    (path/trim-dir-prefix repo-dir path))
         original-name (db/get-file-page (or path rel-path))
-        in-db? (boolean (db/get-file (or path rel-path)))
+        in-db? (boolean (and rel-path (db/get-file (or path rel-path))))
         file-fpath (if in-db?
                      (path/path-join repo-dir path)
                      path)

+ 1 - 0
src/main/frontend/config.cljs

@@ -395,6 +395,7 @@
     (local-db? repo-url)
     (string/replace-first repo-url local-db-prefix "")
 
+    ;; unit test
     (= repo-url "test-db")
     "/test-db"
 

+ 1 - 1
src/main/frontend/fs/watcher_handler.cljs

@@ -104,7 +104,7 @@
           (and (= "change" type)
                (= dir (global-config-handler/global-config-dir)))
           (when (= path "config.edn")
-            (global-config-handler/set-global-config-state! content))
+            (file-handler/alter-global-file path content))
 
           (and (= "change" type)
                (not exists-in-db?))

+ 23 - 15
src/main/frontend/handler/file.cljs

@@ -89,8 +89,8 @@
     nil))
 
 (defn- detect-deprecations
-  [repo path content]
-  (when (or (= path (config/get-repo-config-path repo))
+  [path content]
+  (when (or (= path "logseq/config.edn")
             (and
              (config/global-config-enabled?)
              (= (path/dirname path) (global-config-handler/global-config-dir))))
@@ -123,12 +123,26 @@
 (defn alter-global-file
   "Write global file, e.g. global config"
   [path content]
-  (p/let [_ (fs/write-file! "" nil path content {:skip-compare? true})]
-    (cond
-      (and (config/global-config-enabled?)
-           (= path (global-config-handler/global-config-path)))
-      (p/let [_ (global-config-handler/restore-global-config!)]
-        (state/pub-event! [:shortcut/refresh])))))
+  (-> (p/let [repo (state/get-current-repo)
+              _ (fs/write-file! "" nil path content {:skip-compare? true})]
+        (cond
+          (and (config/global-config-enabled?)
+               (= path (global-config-handler/global-config-path)))
+          (p/do! (detect-deprecations path content)
+                 (global-config-handler/restore-global-config!)
+                 (validate-file repo path content)
+                 (state/pub-event! [:shortcut/refresh]))
+          ;; Add future global file handler here
+          ))
+      (p/catch (fn [error]
+                 (state/pub-event! [:notification/show
+                                    {:content (str "Failed to write to file " path ", error: " error)
+                                     :status :error}])
+                 (println "Write file failed, path: " path ", content: " content)
+                 (log/error :write/failed error)
+                 (state/pub-event! [:capture-error
+                                    {:error error
+                                     :payload {:type :write-file/failed-for-alter-file}}])))))
 
 (defn alter-file
   "Write any in-DB file, e.g. repo config, page, whiteboard, etc."
@@ -141,7 +155,7 @@
   (let [path (gp-util/path-normalize path)
         config-file? (= path "logseq/config.edn")
         _ (when config-file?
-            (detect-deprecations repo path content))
+            (detect-deprecations path content))
         config-valid? (and config-file? (validate-file repo path content))]
     (when (or config-valid? (not config-file?)) ; non-config file or valid config
       (let [opts {:new-graph? new-graph?
@@ -170,12 +184,6 @@
 
                 (= path "logseq/config.edn")
                 (p/let [_ (repo-config-handler/restore-repo-config! repo content)]
-                  (state/pub-event! [:shortcut/refresh]))
-
-                ;; FIXME: global config
-                (and (config/global-config-enabled?)
-                     (= path (global-config-handler/global-config-path)))
-                (p/let [_ (global-config-handler/restore-global-config!)]
                   (state/pub-event! [:shortcut/refresh]))))
             (p/catch
              (fn [error]