summaryrefslogtreecommitdiffhomepage
path: root/packages/tauri/src/updater.ts
blob: 0d3737d1eb04b784ab64314a6cfa03fcac7734b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { check, DownloadEvent } from "@tauri-apps/plugin-updater"
import { relaunch } from "@tauri-apps/plugin-process"
import { ask, message } from "@tauri-apps/plugin-dialog"

export async function runUpdater(onDownloadEvent?: (progress: DownloadEvent) => void) {
  let update
  try {
    update = await check()
  } catch {
    await message("Failed to check for updates")
    return false
  }

  if (!update) return
  if (update.version <= update.currentVersion) return

  try {
    await update.download(onDownloadEvent)
  } catch {
    return false
  }

  const shouldUpdate = await ask(
    `Version ${update.version} of OpenCode has been downloaded, would you like to install it and relaunch?`,
  )
  if (!shouldUpdate) return

  try {
    await update.install()
  } catch {
    await message("Failed to install update")
    return false
  }

  await relaunch()

  return true
}