plugin.cljs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. (and (contains? (:plugin/installed-plugins @state/state) (keyword id))
  29. (get-in @state/state [:plugin/installed-plugins (keyword id) :iir])))
  30. (defn emit-lsp-updates!
  31. [payload]
  32. (js/console.log "debug:lsp-updates:" payload)
  33. (js/window.apis.emit (name :lsp-updates) (bean/->js payload)))
  34. (defn async-install-or-update-for-web!
  35. [{:keys [version repo only-check _plugin-action] :as mft}]
  36. (js/console.log "debug:install-or-update" mft)
  37. (-> (fetch-web-plugin-entry-info repo version)
  38. (p/then (fn [{:keys [_version] :as web-pkg}]
  39. (let [web-pkg (merge web-pkg (dissoc mft :stat))]
  40. (emit-lsp-updates!
  41. {:status :completed
  42. :only-check only-check
  43. :payload (if only-check
  44. (assoc mft :latest-version version :latest-notes "TODO: update notes")
  45. (assoc mft :dst repo :installed-version version :web-pkg web-pkg))}))))
  46. (p/catch (fn [^js e]
  47. (emit-lsp-updates!
  48. {:status :error
  49. :only-check only-check
  50. :payload (assoc mft :error-code (.-message e))})))))
  51. (defn install-marketplace-plugin!
  52. "Installs plugin given plugin map with id"
  53. [{:keys [id] :as mft}]
  54. (when-not (and (:plugin/installing @state/state)
  55. (installed? id))
  56. (p/create
  57. (fn [resolve]
  58. (state/set-state! :plugin/installing mft)
  59. (if (util/electron?)
  60. (ipc/ipc :installMarketPlugin mft)
  61. (async-install-or-update-for-web! mft))
  62. (resolve id)))))
  63. (defn unregister-plugin
  64. "Unregister and uninstall plugin given plugin id"
  65. [id]
  66. (js/LSPluginCore.unregister id))