Explorar el Código

refactor(fs): simplify path/absolute?

Andelf hace 2 años
padre
commit
62b4f0a253
Se han modificado 2 ficheros con 9 adiciones y 7 borrados
  1. 8 7
      deps/common/src/logseq/common/path.cljs
  2. 1 0
      src/main/frontend/util/fs.cljs

+ 8 - 7
deps/common/src/logseq/common/path.cljs

@@ -1,9 +1,7 @@
 (ns logseq.common.path
   "Path manipulation functions, use '/' sep on all platforms.
    Also handles URL paths."
-  (:require [clojure.string :as string]
-            ["path" :as path]
-            ["/frontend/utils" :as utils]))
+  (:require [clojure.string :as string]))
 
 (defn- safe-decode-uri-component
   [uri]
@@ -239,8 +237,8 @@
       (if is-url?
         (safe-decode-uri-component (string/replace (subs sub-path (count base-path)) #"^/+", ""))
         (string/replace (subs sub-path (count base-path)) #"^/+", ""))
-       ;; append as many ..
-      ;; NOTE: buggy impl
+      ;; append as many ..
+      ;; NOTE: This is a buggy impl, relative-path is different when base-path is a file or a dir
       (let [base-segs (string/split base-path #"/" -1)
             path-segs (string/split sub-path #"/" -1)
             common-segs (take-while #(= (first %) (second %)) (map vector base-segs path-segs))
@@ -305,5 +303,8 @@
 (defn absolute?
   "Whether path `p` is absolute."
   [p]
-  (or (.isAbsolute path p)
-      (utils/win32 p)))
+  (let [p (path-normalize p)]
+    (boolean (or (is-file-url? p)
+                 (string/starts-with? p "/")
+                 ;; is windows dir
+                 (re-matches #"^[a-zA-Z]:[/\\]" p)))))

+ 1 - 0
src/main/frontend/util/fs.cljs

@@ -48,6 +48,7 @@
   [root]
   (when (string? root)
     (p/let [exists? (fs/file-exists? root "logseq/graphs-txid.edn")]
+      (prn ::ex exists?)
       (when exists?
         (-> (p/let [txid-str (fs/read-file root "logseq/graphs-txid.edn")
                     txid-meta (and txid-str (reader/read-string txid-str))]