|
|
@@ -21,6 +21,7 @@
|
|
|
[promesa.core :as p]
|
|
|
[frontend.mobile.util :as mobile-util]
|
|
|
[logseq.graph-parser.config :as gp-config]
|
|
|
+ [clojure.string :as string]
|
|
|
["path" :as path]))
|
|
|
|
|
|
;; TODO: extract all git ops using a channel
|
|
|
@@ -104,8 +105,7 @@
|
|
|
:else
|
|
|
true))
|
|
|
|
|
|
-(defn- validate-and-write-file
|
|
|
- "Validates and if valid writes file. Returns boolean indicating if file content was valid"
|
|
|
+(defn- write-file-aux!
|
|
|
[repo path content write-file-options]
|
|
|
(let [original-content (db/get-file repo path)
|
|
|
path-dir (if (and
|
|
|
@@ -117,12 +117,7 @@
|
|
|
(config/get-repo-dir repo))
|
|
|
write-file-options' (merge write-file-options
|
|
|
(when original-content {:old-content original-content}))]
|
|
|
- (p/do!
|
|
|
- (if (validate-file repo path content)
|
|
|
- (do
|
|
|
- (fs/write-file! repo path-dir path content write-file-options')
|
|
|
- true)
|
|
|
- false))))
|
|
|
+ (fs/write-file! repo path-dir path content write-file-options')))
|
|
|
|
|
|
;; TODO: Remove this function in favor of `alter-files`
|
|
|
(defn alter-file
|
|
|
@@ -133,58 +128,58 @@
|
|
|
from-disk? false
|
|
|
skip-compare? false}}]
|
|
|
(let [path (gp-util/path-normalize path)
|
|
|
- write-file! (if from-disk?
|
|
|
- #(p/promise (validate-file repo path content))
|
|
|
- #(validate-and-write-file repo path content {:skip-compare? skip-compare?}))
|
|
|
- opts {:new-graph? new-graph?
|
|
|
- :from-disk? from-disk?
|
|
|
- :skip-db-transact? skip-db-transact?}
|
|
|
- result (if reset?
|
|
|
- (do
|
|
|
- (when-not skip-db-transact?
|
|
|
- (when-let [page-id (db/get-file-page-id path)]
|
|
|
- (db/transact! repo
|
|
|
- [[:db/retract page-id :block/alias]
|
|
|
- [:db/retract page-id :block/tags]]
|
|
|
- opts)))
|
|
|
- (file-common-handler/reset-file! repo path content (merge opts
|
|
|
- (when (some? verbose) {:verbose verbose}))))
|
|
|
- (db/set-file-content! repo path content opts))]
|
|
|
- (util/p-handle (write-file!)
|
|
|
- (fn [valid-result?]
|
|
|
- (when re-render-root? (ui-handler/re-render-root!))
|
|
|
-
|
|
|
- (cond
|
|
|
- (= path (config/get-custom-css-path repo))
|
|
|
- (ui-handler/add-style-if-exists!)
|
|
|
-
|
|
|
- (and (= path (config/get-repo-config-path repo))
|
|
|
- valid-result?)
|
|
|
- (p/let [_ (repo-config-handler/restore-repo-config! repo content)]
|
|
|
- (state/pub-event! [:shortcut/refresh]))
|
|
|
-
|
|
|
- (and (config/global-config-enabled?)
|
|
|
- (= path (global-config-handler/global-config-path))
|
|
|
- valid-result?)
|
|
|
- (p/let [_ (global-config-handler/restore-global-config!)]
|
|
|
- (state/pub-event! [:shortcut/refresh]))))
|
|
|
- (fn [error]
|
|
|
- (when (and (config/global-config-enabled?)
|
|
|
- ;; Global-config not started correctly but don't
|
|
|
- ;; know root cause yet
|
|
|
- ;; https://sentry.io/organizations/logseq/issues/3587411237/events/4b5da8b8e58b4f929bd9e43562213d32/events/?cursor=0%3A0%3A1&project=5311485&statsPeriod=14d
|
|
|
- (global-config-handler/global-config-dir-exists?)
|
|
|
- (= path (global-config-handler/global-config-path)))
|
|
|
- (state/pub-event! [:notification/show
|
|
|
- {:content (str "Failed to write to file " path)
|
|
|
- :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}}])))
|
|
|
- result))
|
|
|
+ config-file? (string/ends-with? path config/config-file)
|
|
|
+ config-valid? (and config-file? (validate-file repo path content))]
|
|
|
+ (when-not (and config-file? (not config-valid?)) ; non-config file or valid config
|
|
|
+ (let [opts {:new-graph? new-graph?
|
|
|
+ :from-disk? from-disk?
|
|
|
+ :skip-db-transact? skip-db-transact?}
|
|
|
+ result (if reset?
|
|
|
+ (do
|
|
|
+ (when-not skip-db-transact?
|
|
|
+ (when-let [page-id (db/get-file-page-id path)]
|
|
|
+ (db/transact! repo
|
|
|
+ [[:db/retract page-id :block/alias]
|
|
|
+ [:db/retract page-id :block/tags]]
|
|
|
+ opts)))
|
|
|
+ (file-common-handler/reset-file!
|
|
|
+ repo path content (merge opts
|
|
|
+ (when (some? verbose) {:verbose verbose}))))
|
|
|
+ (db/set-file-content! repo path content opts))]
|
|
|
+ (-> (p/let [_ (when-not from-disk?
|
|
|
+ (write-file-aux! repo path content {:skip-compare? skip-compare?}))]
|
|
|
+ (when re-render-root? (ui-handler/re-render-root!))
|
|
|
+
|
|
|
+ (cond
|
|
|
+ (= path (config/get-custom-css-path repo))
|
|
|
+ (ui-handler/add-style-if-exists!)
|
|
|
+
|
|
|
+ (= path (config/get-repo-config-path repo))
|
|
|
+ (p/let [_ (repo-config-handler/restore-repo-config! repo content)]
|
|
|
+ (state/pub-event! [:shortcut/refresh]))
|
|
|
+
|
|
|
+ (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]
|
|
|
+ (when (and (config/global-config-enabled?)
|
|
|
+ ;; Global-config not started correctly but don't
|
|
|
+ ;; know root cause yet
|
|
|
+ ;; https://sentry.io/organizations/logseq/issues/3587411237/events/4b5da8b8e58b4f929bd9e43562213d32/events/?cursor=0%3A0%3A1&project=5311485&statsPeriod=14d
|
|
|
+ (global-config-handler/global-config-dir-exists?)
|
|
|
+ (= path (global-config-handler/global-config-path)))
|
|
|
+ (state/pub-event! [:notification/show
|
|
|
+ {:content (str "Failed to write to file " path)
|
|
|
+ :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}}]))))
|
|
|
+ result))))
|
|
|
|
|
|
(defn set-file-content!
|
|
|
[repo path new-content]
|