updater.ts 886 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { check, DownloadEvent } from "@tauri-apps/plugin-updater"
  2. import { relaunch } from "@tauri-apps/plugin-process"
  3. import { ask, message } from "@tauri-apps/plugin-dialog"
  4. export async function runUpdater(onDownloadEvent?: (progress: DownloadEvent) => void) {
  5. let update
  6. try {
  7. update = await check()
  8. } catch {
  9. await message("Failed to check for updates")
  10. return false
  11. }
  12. if (!update) return
  13. if (update.version <= update.currentVersion) return
  14. try {
  15. await update.download(onDownloadEvent)
  16. } catch {
  17. return false
  18. }
  19. const shouldUpdate = await ask(
  20. `Version ${update.version} of OpenCode has been downloaded, would you like to install it and relaunch?`,
  21. )
  22. if (!shouldUpdate) return
  23. try {
  24. await update.install()
  25. } catch {
  26. await message("Failed to install update")
  27. return false
  28. }
  29. await relaunch()
  30. return true
  31. }