fix(fs): impl mkdir-recur! for memory-fs

This commit is contained in:
Andelf
2023-11-23 00:35:23 +08:00
committed by Tienson Qin
parent a09d158de5
commit d8c8b90143
2 changed files with 30 additions and 5 deletions

View File

@@ -438,10 +438,7 @@
(defn get-repo-fpath
[repo-url path]
(if (and (or (util/electron?) (mobile-util/native-platform?))
(local-db? repo-url))
(path/path-join (get-repo-dir repo-url) path)
(util/node-path.join (get-repo-dir repo-url) path)))
(path/path-join (get-repo-dir repo-url) path))
(defn get-repo-config-path
[]

View File

@@ -46,6 +46,34 @@
(js/window.pfs.mkdir dir)))))
(defn- <exists?
"dir is path, without memory:// prefix for simplicity"
[dir]
(-> (js/window.pfs.stat dir)
(p/then (fn [stat]
(not (nil? stat))))
(p/catch (fn [_]
nil))))
(defn- <mkdir-recur!
"mkdir, recursively create parent directories if not exist
lightning-fs does not support's :recursive in mkdir options"
[dir]
(p/let [fpath (path/url-to-path dir)
sub-dirs (p/loop [top-parent fpath
remains []]
(p/let [exists? (<exists? top-parent)]
(if exists?
(reverse remains) ;; top-parent is the first non-exist dir
(p/recur (path/parent top-parent)
(conj remains top-parent)))))]
(p/loop [remains sub-dirs]
(if (empty? remains)
(p/resolved nil)
(p/do! (js/window.pfs.mkdir (first remains))
(p/recur (rest remains)))))))
(defrecord MemoryFs []
protocol/Fs
(mkdir! [_this dir]
@@ -57,7 +85,7 @@
(mkdir-recur! [_this dir]
(when js/window.pfs
(let [fpath (path/url-to-path dir)]
(-> (js/window.pfs.mkdir fpath #js {:recursive true})
(-> (<mkdir-recur! fpath)
(p/catch (fn [error] (println "(memory-fs)Mkdir-recur error: " error)))))))
(readdir [_this dir]