浏览代码

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

Andelf 1 年之前
父节点
当前提交
d8c8b90143
共有 2 个文件被更改,包括 30 次插入5 次删除
  1. 1 4
      src/main/frontend/config.cljs
  2. 29 1
      src/main/frontend/fs/memory_fs.cljs

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

@@ -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
   []

+ 29 - 1
src/main/frontend/fs/memory_fs.cljs

@@ -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]