updater.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { check } from "@tauri-apps/plugin-updater"
  2. import { relaunch } from "@tauri-apps/plugin-process"
  3. import { ask, message } from "@tauri-apps/plugin-dialog"
  4. import { type as ostype } from "@tauri-apps/plugin-os"
  5. import { initI18n, t } from "./i18n"
  6. import { commands } from "./bindings"
  7. export const UPDATER_ENABLED = window.__OPENCODE__?.updaterEnabled ?? false
  8. export async function runUpdater({ alertOnFail }: { alertOnFail: boolean }) {
  9. await initI18n()
  10. let update
  11. try {
  12. update = await check()
  13. } catch {
  14. if (alertOnFail)
  15. await message(t("desktop.updater.checkFailed.message"), { title: t("desktop.updater.checkFailed.title") })
  16. return
  17. }
  18. if (!update) {
  19. if (alertOnFail) await message(t("desktop.updater.none.message"), { title: t("desktop.updater.none.title") })
  20. return
  21. }
  22. try {
  23. await update.download()
  24. } catch {
  25. if (alertOnFail)
  26. await message(t("desktop.updater.downloadFailed.message"), { title: t("desktop.updater.downloadFailed.title") })
  27. return
  28. }
  29. const shouldUpdate = await ask(t("desktop.updater.downloaded.prompt", { version: update.version }), {
  30. title: t("desktop.updater.downloaded.title"),
  31. })
  32. if (!shouldUpdate) return
  33. try {
  34. if (ostype() === "windows") await commands.killSidecar()
  35. await update.install()
  36. } catch {
  37. await message(t("desktop.updater.installFailed.message"), { title: t("desktop.updater.installFailed.title") })
  38. return
  39. }
  40. await commands.killSidecar()
  41. await relaunch()
  42. }