updater.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 { invoke } from "@tauri-apps/api/core"
  5. import { type as ostype } from "@tauri-apps/plugin-os"
  6. export const UPDATER_ENABLED = window.__OPENCODE__?.updaterEnabled ?? false
  7. export async function runUpdater({ alertOnFail }: { alertOnFail: boolean }) {
  8. let update
  9. try {
  10. update = await check()
  11. } catch {
  12. if (alertOnFail) await message("Failed to check for updates", { title: "Update Check Failed" })
  13. return
  14. }
  15. if (!update) {
  16. if (alertOnFail)
  17. await message("You are already using the latest version of OpenCode", { title: "No Update Available" })
  18. return
  19. }
  20. try {
  21. await update.download()
  22. } catch {
  23. if (alertOnFail) await message("Failed to download update", { title: "Update Failed" })
  24. return
  25. }
  26. const shouldUpdate = await ask(
  27. `Version ${update.version} of OpenCode has been downloaded, would you like to install it and relaunch?`,
  28. { title: "Update Downloaded" },
  29. )
  30. if (!shouldUpdate) return
  31. try {
  32. if (ostype() === "windows") await invoke("kill_sidecar")
  33. await update.install()
  34. } catch {
  35. await message("Failed to install update", { title: "Update Failed" })
  36. return
  37. }
  38. await invoke("kill_sidecar")
  39. await relaunch()
  40. }