1
0
Эх сурвалжийг харах

electron: add node fs record

Tienson Qin 4 жил өмнө
parent
commit
b59b7f8c69

+ 49 - 3
src/electron/electron/handler.cljs

@@ -1,8 +1,54 @@
 (ns electron.handler
-  (:require ["electron" :refer [ipcMain]]))
+  (:require ["electron" :refer [ipcMain]]
+            [cljs-bean.core :as bean]
+            ["fs" :as fs]
+            ["path" :as path]))
+
+(defmulti handle (fn [args] (keyword (first args))))
+
+(defmethod handle :mkdir [[_ dir]]
+  (fs/mkdirSync dir))
+
+(defn- ls-dir [dir]
+  (->> (tree-seq
+        (fn [f] (.isDirectory (.statSync fs f) ()))
+        (fn [d] (map #(.join path d %) (.readdirSync fs d)))
+        dir)
+       (apply concat)
+       (doall)))
+
+(defmethod handle :readdir [[_ dir]]
+  (->> (tree-seq
+        (fn [f] (.isDirectory (fs/statSync f) ()))
+        (fn [d] (map #(.join path d %) (fs/readdirSync d)))
+        dir)
+       (doall)))
+
+(defmethod handle :unlink [[_ path]]
+  (fs/unlinkSync path))
+
+(defmethod handle :readFile [[_ path]]
+  (.toString (fs/readFileSync path)))
+
+(defmethod handle :writeFile [[_ path content]]
+  (fs/writeFileSync path content))
+
+(defmethod handle :rename [[_ old-path new-path]]
+  (fs/renameSync old-path new-path))
+
+(defmethod handle :stat [[_ path]]
+  (fs/statSync path))
+
+(defmethod handle :default [args]
+  (println "Error: no ipc handler for: " (bean/->js args)))
 
 (defn set-ipc-handler! [window]
   (.handle ipcMain "main"
            (fn [event args-js]
-             (prn "receive event: " args-js)
-             args-js)))
+             (println "received: " args-js)
+             (try
+               (let [message (bean/->clj args-js)]
+                 (bean/->js (handle message)))
+               (catch js/Error e
+                 (println "IPC error: " e)
+                 e)))))

+ 1 - 0
src/main/electron/ipc.cljs

@@ -2,6 +2,7 @@
   (:require [cljs-bean.core :as bean]
             [promesa.core :as p]))
 
+;; TODO: handle errors
 (defn ipc
   [& args]
   (js/window.api.doAction (bean/->js args)))

+ 8 - 24
src/main/frontend/fs/node.cljs

@@ -5,37 +5,21 @@
             [promesa.core :as p]
             [electron.ipc :as ipc]))
 
-;; (defonce fs (when (util/electron?)
-;;               (js/require "fs")))
-
-;; (defonce path (when (util/electron?)
-;;                 (js/require "path")))
-
-;; (defn ls-dir [dir]
-;;   (->> (tree-seq
-;;         (fn [f] (.isDirectory (.statSync fs f) ()))
-;;         (fn [d] (map #(.join path d %) (.readdirSync fs d)))
-;;         dir)
-;;        (apply concat)
-;;        (doall)))
-
 (defrecord Node []
   protocol/Fs
   (mkdir! [this dir]
-    (ipc/ipc "mkdir" {:path dir}))
+    (ipc/ipc "mkdir" dir))
   (readdir [this dir]                   ; recursive
-    (ipc/ipc "readdir" {:dir dir}))
+    (ipc/ipc "readdir" dir))
   (unlink! [this path _opts]
-    (ipc/ipc "unlink" {:path path}))
+    (ipc/ipc "unlink" path))
   (rmdir! [this dir]
     nil)
   (read-file [this dir path]
-    (ipc/ipc "readFile" {:path (str dir "/" path)}))
-  (write-file! [this repo dir path content opts]
-    (ipc/ipc "writeFile" {:path (str dir "/" path)
-                          :content content}))
+    (ipc/ipc "readFile" (str dir "/" path)))
+  (write-file! [this repo dir path content _opts]
+    (ipc/ipc "writeFile" (str dir "/" path) content))
   (rename! [this repo old-path new-path]
-    (ipc/ipc "rename" {:old-path old-path
-                       :new-path new-path}))
+    (ipc/ipc "rename" old-path new-path))
   (stat [this dir path]
-    (ipc/ipc "stat" {:path (str dir path)})))
+    (ipc/ipc "stat" (str dir path))))