fix: readdir

also, fix warnings on persist var
This commit is contained in:
Tienson Qin
2022-09-13 13:19:14 +08:00
committed by Andelf
parent 8915818c3f
commit c074465b8a
5 changed files with 61 additions and 52 deletions

View File

@@ -55,7 +55,7 @@
(defn- <readdir [path]
(-> (p/chain (.readdir Filesystem (clj->js {:path path}))
#(js->clj % :keywordize-keys true)
#(get % "files" nil))
:files)
(p/catch (fn [error]
(js/console.error "readdir Error: " path ": " error)
nil))))
@@ -83,25 +83,26 @@
files (<readdir d)
files (->> files
(remove (fn [file]
(let [uri (:uri file)]
(or (string/starts-with? uri ".")
(= file "bak"))))))
files (map (fn [file]
;; TODO: use uri-join
(update file :uri
(fn [uri]
(str (string/replace d #"/+$" "")
"/"
(if (mobile-util/native-ios?)
(js/encodeURI uri)
uri)))))
files)
files-dir (->> files
(or (string/starts-with? file ".")
(and (mobile-util/native-android?)
(or (string/includes? file "#")
(string/includes? file "%")))
(= file "bak")))))
files (->> files
(map (fn [file]
;; TODO: use uri-join
(str (string/replace d #"/+$" "")
"/"
(if (mobile-util/native-ios?)
(js/encodeURI file)
file)))))
files-with-stats (p/all (mapv <stat files))
files-dir (->> files-with-stats
(filterv #(= (:type %) "directory"))
(mapv :uri))
files-result
(p/all
(->> files
(->> files-with-stats
(filter #(= (:type %) "file"))
(filter
(fn [{:keys [uri]}]

View File

@@ -80,7 +80,7 @@
(do
(state/set-state! :user/info result)
(let [status (if (user-handler/alpha-user?) :welcome :unavailable)]
(when (= status :welcome)
(when (and (= status :welcome) (user-handler/logged-in?))
(async/<! (file-sync-handler/load-session-graphs))
(p/let [repos (repo-handler/refresh-repos!)]
(when-let [repo (state/get-current-repo)]

View File

@@ -13,7 +13,8 @@
[goog.object :as gobj]
[clojure.string :as string]
[rum.core :as rum]
[electron.ipc :as ipc]))
[electron.ipc :as ipc]
[promesa.core :as p]))
(defn- get-css-var-value
[var-name]
@@ -154,16 +155,19 @@
(when (or (not should-ask?)
(ask-allow))
(load href #(do (js/console.log "[custom js]" href) (execed))))
(util/p-handle
(fs/read-file (if (util/electron?) "" (config/get-repo-dir (state/get-current-repo))) href)
#(when-let [scripts (and % (string/trim %))]
(when-not (string/blank? scripts)
(when (or (not should-ask?) (ask-allow))
(try
(js/eval scripts)
(execed)
(catch js/Error e
(js/console.error "[custom js]" e))))))))))))
(let [dir (if (util/electron?) "" (config/get-repo-dir (state/get-current-repo)))]
(p/let [exists? (fs/file-exists? dir href)]
(when exists?
(util/p-handle
(fs/read-file dir href)
#(when-let [scripts (and % (string/trim %))]
(when-not (string/blank? scripts)
(when (or (not should-ask?) (ask-allow))
(try
(js/eval scripts)
(execed)
(catch js/Error e
(js/console.error "[custom js]" e)))))))))))))))
(defn toggle-wide-mode!
[]

View File

@@ -38,12 +38,14 @@
(defn read-graphs-txid-info
[root]
(when (string? root)
(-> (p/let [txid-str (fs/read-file root "logseq/graphs-txid.edn")
txid-meta (and txid-str (reader/read-string txid-str))]
txid-meta)
(p/catch
(fn [^js e]
(js/console.error "[fs read txid data error]" e))))))
(p/let [exists? (fs/file-exists? root "logseq/graphs-txid.edn")]
(when exists?
(-> (p/let [txid-str (fs/read-file root "logseq/graphs-txid.edn")
txid-meta (and txid-str (reader/read-string txid-str))]
txid-meta)
(p/catch
(fn [^js e]
(js/console.error "[fs read txid data error]" e))))))))
(defn inflate-graphs-info
[graphs]

View File

@@ -32,24 +32,26 @@
(let [repo (state/get-current-repo)
dir (config/get-repo-dir repo)
path (load-path location)]
(-> (p/chain (fs/stat dir path)
(fn [stat]
(when stat
(fs/read-file dir path)))
(fn [content]
(when (not-empty content)
(try (cljs.reader/read-string content)
(catch js/Error e
(println (util/format "read persist-var failed: %s" (load-path location)))
(js/console.dir e)))))
(fn [value]
(when (some? value)
(swap! *value (fn [o]
(-> o
(assoc-in [repo :loaded?] true)
(assoc-in [repo :value] value)))))))
(p/catch (fn [e]
(println (util/format "load persist-var failed: %s: %s" (load-path location) e))))))))
(p/let [file-exists? (fs/file-exists? dir path)]
(when file-exists?
(-> (p/chain (fs/stat dir path)
(fn [stat]
(when stat
(fs/read-file dir path)))
(fn [content]
(when (not-empty content)
(try (cljs.reader/read-string content)
(catch js/Error e
(println (util/format "read persist-var failed: %s" (load-path location)))
(js/console.dir e)))))
(fn [value]
(when (some? value)
(swap! *value (fn [o]
(-> o
(assoc-in [repo :loaded?] true)
(assoc-in [repo :value] value)))))))
(p/catch (fn [e]
(println (util/format "load persist-var failed: %s: %s" (load-path location) e))))))))))
(-loaded? [_]
(get-in @*value [(state/get-current-repo) :loaded?]))