瀏覽代碼

refactor(electron): use electron-log, add auto update for windows

Andelf 3 年之前
父節點
當前提交
dd2adac2b1

+ 10 - 7
src/electron/electron/core.cljs

@@ -106,8 +106,10 @@
                              (-> (. fs copy (path/join assets-from-dir filename) (path/join assets-to-dir filename))
                              (-> (. fs copy (path/join assets-from-dir filename) (path/join assets-to-dir filename))
                                  (p/catch
                                  (p/catch
                                   (fn [e]
                                   (fn [e]
-                                    (println (str "Failed to copy " (path/join assets-from-dir filename) " to " (path/join assets-to-dir filename)))
-                                    (js/console.error e)))))
+                                    (.error logger "Failed to copy"
+                                            (str {:from (path/join assets-from-dir filename)
+                                                  :to (path/join assets-to-dir filename)})
+                                            e)))))
                            asset-filenames)
                            asset-filenames)
 
 
                           (map
                           (map
@@ -165,7 +167,7 @@
                  (try
                  (try
                    (js-invoke app type args)
                    (js-invoke app type args)
                    (catch js/Error e
                    (catch js/Error e
-                     (js/console.error e)))))
+                     (.error logger (str call-app-channel " " e))))))
 
 
       (.handle call-win-channel
       (.handle call-win-channel
                (fn [^js e type & args]
                (fn [^js e type & args]
@@ -173,7 +175,7 @@
                    (try
                    (try
                      (js-invoke win type args)
                      (js-invoke win type args)
                      (catch js/Error e
                      (catch js/Error e
-                       (js/console.error e)))))))
+                       (.error logger (str call-win-channel " " e))))))))
 
 
     #(do (clear-win-effects!)
     #(do (clear-win-effects!)
          (.removeHandler ipcMain toggle-win-channel)
          (.removeHandler ipcMain toggle-win-channel)
@@ -270,11 +272,12 @@
                (win/switch-to-window! window))))
                (win/switch-to-window! window))))
 
 
       (.on app "window-all-closed" (fn []
       (.on app "window-all-closed" (fn []
+                                     (.debug logger "window-all-closed" "Quiting...")
                                      (try
                                      (try
                                        (fs-watcher/close-watcher!)
                                        (fs-watcher/close-watcher!)
                                        (search/close!)
                                        (search/close!)
                                        (catch js/Error e
                                        (catch js/Error e
-                                         (js/console.error e)))
+                                         (.error logger "window-all-closed" e)))
                                      (.quit app)))
                                      (.quit app)))
       (.on app "ready"
       (.on app "ready"
            (fn []
            (fn []
@@ -340,9 +343,9 @@
                (.on app "activate" #(when @*win (.show win)))))))))
                (.on app "activate" #(when @*win (.show win)))))))))
 
 
 (defn start []
 (defn start []
-  (js/console.log "Main - start")
+  (.debug logger "Main - start")
   (when @*setup-fn (@*setup-fn)))
   (when @*setup-fn (@*setup-fn)))
 
 
 (defn stop []
 (defn stop []
-  (js/console.log "Main - stop")
+  (.debug logger "Main - stop")
   (when @*teardown-fn (@*teardown-fn)))
   (when @*teardown-fn (@*teardown-fn)))

+ 2 - 2
src/electron/electron/exceptions.cljs

@@ -17,9 +17,9 @@
     (show-error-tip "[Main Exception]" msg stack))
     (show-error-tip "[Main Exception]" msg stack))
 
 
   ;; for debug log
   ;; for debug log
-  (js/console.error uncaughtExceptionChan e))
+  (.error utils/logger uncaughtExceptionChan (str e)))
 
 
 (defn setup-exception-listeners!
 (defn setup-exception-listeners!
   []
   []
   (js/process.on uncaughtExceptionChan app-uncaught-handler)
   (js/process.on uncaughtExceptionChan app-uncaught-handler)
-  #(js/process.off uncaughtExceptionChan app-uncaught-handler))
+  #(js/process.off uncaughtExceptionChan app-uncaught-handler))

+ 4 - 4
src/electron/electron/fs_watcher.cljs

@@ -29,8 +29,8 @@
       ;; Should only send to one window; then dbsync will do his job
       ;; Should only send to one window; then dbsync will do his job
       ;; If no window is on this graph, just ignore
       ;; If no window is on this graph, just ignore
       (let [sent? (some send-fn wins)]
       (let [sent? (some send-fn wins)]
-        (when-not sent? (prn "unhandled file event will cause uncatched file modifications!.
-                          target:" dir))))))
+        (when-not sent? (.warn utils/logger
+                               (str "unhandled file event will cause uncatched file modifications!. target:" dir)))))))
 
 
 (defn- publish-file-event!
 (defn- publish-file-event!
   [dir path event]
   [dir path event]
@@ -89,8 +89,8 @@
                               500)))
                               500)))
         (.on dir-watcher "error"
         (.on dir-watcher "error"
              (fn [path]
              (fn [path]
-               (println "Watch error happened: "
-                        {:path path})))
+               (.warn utils/logger "Watch error happened: "
+                      (str {:path path}))))
 
 
         ;; electron app extends `EventEmitter`
         ;; electron app extends `EventEmitter`
         ;; TODO check: duplicated with the logic in "window-all-closed" ?
         ;; TODO check: duplicated with the logic in "window-all-closed" ?

+ 5 - 6
src/electron/electron/handler.cljs

@@ -111,8 +111,7 @@
         (let [backup-path (try
         (let [backup-path (try
                             (backup-file/backup-file repo :backup-dir path (path/extname path) content)
                             (backup-file/backup-file repo :backup-dir path (path/extname path) content)
                             (catch :default e
                             (catch :default e
-                              (println "Backup file failed")
-                              (js/console.dir e)))]
+                              (.error utils/logger (str "Backup file failed: " e))))]
           (utils/send-to-renderer window "notification" {:type "error"
           (utils/send-to-renderer window "notification" {:type "error"
                                                          :payload (str "Write to the file " path
                                                          :payload (str "Write to the file " path
                                                                        " failed, "
                                                                        " failed, "
@@ -304,7 +303,7 @@
         (try
         (try
           (fs-extra/removeSync path)
           (fs-extra/removeSync path)
           (catch js/Error e
           (catch js/Error e
-            (js/console.error e)))))
+            (.error utils/logger (str "Clear cache: " e))))))
     (utils/send-to-renderer window "redirect" {:payload {:to :home}})))
     (utils/send-to-renderer window "redirect" {:payload {:to :home}})))
 
 
 (defmethod handle :clearCache [window _]
 (defmethod handle :clearCache [window _]
@@ -564,7 +563,7 @@
   (apply rsapi/decrypt-with-passphrase (rest args)))
   (apply rsapi/decrypt-with-passphrase (rest args)))
 
 
 (defmethod handle :default [args]
 (defmethod handle :default [args]
-  (println "Error: no ipc handler for: " (bean/->js args)))
+  (.error utils/logger "Error: no ipc handler for: " (bean/->js args)))
 
 
 (defn broadcast-persist-graph!
 (defn broadcast-persist-graph!
   "Receive graph-name (not graph path)
   "Receive graph-name (not graph path)
@@ -605,8 +604,8 @@
                    (bean/->js (handle (or (utils/get-win-from-sender event) window) message)))
                    (bean/->js (handle (or (utils/get-win-from-sender event) window) message)))
                  (catch js/Error e
                  (catch js/Error e
                    (when-not (contains? #{"mkdir" "stat"} (nth args-js 0))
                    (when-not (contains? #{"mkdir" "stat"} (nth args-js 0))
-                     (println "IPC error: " {:event event
-                                             :args args-js}
+                     (.error utils/logger "IPC error: " (str {:event event
+                                                              :args args-js})
                               e))
                               e))
                    e))))
                    e))))
     #(.removeHandler ipcMain main-channel)))
     #(.removeHandler ipcMain main-channel)))

+ 1 - 1
src/electron/electron/search.cljs

@@ -225,7 +225,7 @@
   (when-let [database (get-db repo)]
   (when-let [database (get-db repo)]
     (.close database)
     (.close database)
     (let [[db-name db-full-path db-ver-path] (get-db-version-path repo)]
     (let [[db-name db-full-path db-ver-path] (get-db-version-path repo)]
-      (println "Delete search indice: " db-full-path)
+      (.info logger "Delete search indice" (str {:path db-full-path}))
       (fs/unlinkSync db-full-path)
       (fs/unlinkSync db-full-path)
       (fs/unlinkSync db-ver-path)
       (fs/unlinkSync db-ver-path)
       (swap! databases dissoc db-name))))
       (swap! databases dissoc db-name))))

+ 8 - 7
src/electron/electron/updater.cljs

@@ -1,5 +1,5 @@
 (ns electron.updater
 (ns electron.updater
-  (:require [electron.utils :refer [mac? prod? open fetch logger *win]]
+  (:require [electron.utils :refer [mac? win32? prod? open fetch logger *win]]
             [frontend.version :refer [version]]
             [frontend.version :refer [version]]
             [clojure.string :as string]
             [clojure.string :as string]
             [promesa.core :as p]
             [promesa.core :as p]
@@ -13,7 +13,7 @@
 
 
 (def *update-ready-to-install (atom nil))
 (def *update-ready-to-install (atom nil))
 (def *update-pending (atom nil))
 (def *update-pending (atom nil))
-(def debug (partial (.-warn logger) "[updater]"))
+(def debug (partial (.-debug logger) "[updater]"))
 
 
 ;Event: 'error'
 ;Event: 'error'
 ;Event: 'checking-for-update'
 ;Event: 'checking-for-update'
@@ -30,8 +30,7 @@
 
 
 (defn get-latest-artifact-info
 (defn get-latest-artifact-info
   [repo]
   [repo]
-  (let [;endpoint "https://update.electronjs.org/xyhp915/cljs-todo/darwin-x64/0.0.4"
-        endpoint (str "https://update.electronjs.org/" repo "/" js/process.platform "-" js/process.arch "/" electron-version)]
+  (let [endpoint (str "https://update.electronjs.org/" repo "/" js/process.platform "-" js/process.arch "/" electron-version)]
     (debug "checking" endpoint)
     (debug "checking" endpoint)
     (p/catch
     (p/catch
      (p/let [res (fetch endpoint)
      (p/let [res (fetch endpoint)
@@ -42,7 +41,7 @@
            (bean/->clj info))
            (bean/->clj info))
          (throw (js/Error. (str "[" status "] " text)))))
          (throw (js/Error. (str "[" status "] " text)))))
      (fn [e]
      (fn [e]
-       (js/console.warn "[update server error] " e)
+       (.warn logger "[update server error]" e)
        (throw e)))))
        (throw e)))))
 
 
 (defn check-for-updates
 (defn check-for-updates
@@ -128,9 +127,11 @@
            ;; start auto updater
            ;; start auto updater
           (do
           (do
             (debug "Found remote version" remote-version)
             (debug "Found remote version" remote-version)
-            (when mac?
+            (when (or mac? win32?)
+              (debug "forward update to autoUpdater")
+              ;; FIXME: It seems that update-electron-app doesn't work on linux
               (when-let [f (js/require "update-electron-app")]
               (when-let [f (js/require "update-electron-app")]
-                (f #js{:notifyUser false})
+                (f #js{:notifyUser false :logger logger})
                 (.once autoUpdater "update-downloaded"
                 (.once autoUpdater "update-downloaded"
                        new-version-downloaded-cb))))
                        new-version-downloaded-cb))))
 
 

+ 1 - 1
src/electron/electron/utils.cljs

@@ -97,7 +97,7 @@
     (when (fs/existsSync path)
     (when (fs/existsSync path)
       (.toString (fs/readFileSync path)))
       (.toString (fs/readFileSync path)))
     (catch js/Error e
     (catch js/Error e
-      (js/console.error e))))
+      (.error logger (str "Read file: " e)))))
 
 
 (defn get-focused-window
 (defn get-focused-window
   []
   []

+ 1 - 1
src/main/frontend/components/settings.cljs

@@ -598,7 +598,7 @@
         developer-mode? (state/sub [:ui/developer-mode?])
         developer-mode? (state/sub [:ui/developer-mode?])
         https-agent-opts (state/sub [:electron/user-cfgs :settings/agent])]
         https-agent-opts (state/sub [:electron/user-cfgs :settings/agent])]
     [:div.panel-wrap.is-advanced
     [:div.panel-wrap.is-advanced
-     (when (and util/mac? (util/electron?)) (app-auto-update-row t))
+     (when (and (or util/mac? util/win32?) (util/electron?)) (app-auto-update-row t))
      (usage-diagnostics-row t instrument-disabled?)
      (usage-diagnostics-row t instrument-disabled?)
      (when-not (mobile-util/native-platform?) (developer-mode-row t developer-mode?))
      (when-not (mobile-util/native-platform?) (developer-mode-row t developer-mode?))
      (when (util/electron?) (https-user-agent-row https-agent-opts))
      (when (util/electron?) (https-user-agent-row https-agent-opts))