plugin.cljs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (ns frontend.handler.common.plugin
  2. "Common plugin related fns for handlers and api"
  3. (:require [frontend.state :as state]
  4. [frontend.util :as util]
  5. [promesa.core :as p]
  6. [cljs-bean.core :as bean]
  7. [electron.ipc :as ipc]))
  8. (defn get-web-plugin-checker-url!
  9. ([repo] (get-web-plugin-checker-url! repo ""))
  10. ([repo version]
  11. (util/node-path.join "https://plugins.logseq.io/r2"
  12. repo (if (not (string? version)) "" version))))
  13. (defn fetch-web-plugin-entry-info
  14. [repo version]
  15. (p/let [url (get-web-plugin-checker-url! repo version)
  16. ^js res (js/window.fetch url)]
  17. (if (and (.-ok res)
  18. (= (.-status res) 200))
  19. (-> (.json res)
  20. (p/then #(bean/->clj %)))
  21. (-> (.text res)
  22. (p/then
  23. (fn [error-text]
  24. (throw (js/Error. (str "web-plugin-entry-error:" error-text)))))))))
  25. (defn installed?
  26. "For the given plugin id, returns boolean indicating if it is installed"
  27. [id]
  28. (contains? (:plugin/installed-plugins @state/state) (keyword id)))
  29. (defn emit-lsp-updates!
  30. [payload]
  31. (js/console.log "debug:lsp-updates:" payload)
  32. (js/window.apis.emit (name :lsp-updates) (bean/->js payload)))
  33. (defn async-install-or-update-for-web!
  34. [{:keys [version repo only-check] :as manifest}]
  35. (js/console.log "[plugin]" (if only-check "Checking" "Installing") " #" repo)
  36. (-> (fetch-web-plugin-entry-info repo (if only-check "" version))
  37. (p/then (fn [web-pkg]
  38. (let [web-pkg (merge web-pkg (dissoc manifest :stat))
  39. latest-version (:version web-pkg)
  40. valid-latest-version (when only-check
  41. (let [coerced-current-version (.coerce util/sem-ver version)
  42. coerced-latest-version (.coerce util/sem-ver latest-version)]
  43. (if (and coerced-current-version
  44. coerced-latest-version
  45. (util/sem-ver.lt coerced-current-version coerced-latest-version))
  46. latest-version
  47. (throw (js/Error. :no-new-version)))))]
  48. (emit-lsp-updates!
  49. {:status :completed
  50. :only-check only-check
  51. :payload (if only-check
  52. (assoc manifest :latest-version valid-latest-version :latest-notes "TODO: update notes")
  53. (assoc manifest :dst repo :version latest-version :web-pkg web-pkg))}))))
  54. (p/catch (fn [^js e]
  55. (emit-lsp-updates!
  56. {:status :error
  57. :only-check only-check
  58. :payload (assoc manifest :error-code (.-message e))})))))
  59. (defn install-marketplace-plugin!
  60. "Installs plugin given plugin map with id"
  61. [{:keys [id] :as manifest}]
  62. (when-not (and (:plugin/installing @state/state)
  63. (installed? id))
  64. (p/create
  65. (fn [resolve]
  66. (state/set-state! :plugin/installing manifest)
  67. (if (util/electron?)
  68. (ipc/ipc :installMarketPlugin manifest)
  69. (async-install-or-update-for-web! manifest))
  70. (resolve id)))))
  71. (defn unregister-plugin
  72. "Unregister and uninstall plugin given plugin id"
  73. [id]
  74. (js/LSPluginCore.unregister id))