summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop/src/updater.ts
blob: 4753ee66390a2d74d1927e72d220f8a452937f11 (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
39
40
41
42
43
44
45
46
47
import { check } from "@tauri-apps/plugin-updater"
import { relaunch } from "@tauri-apps/plugin-process"
import { ask, message } from "@tauri-apps/plugin-dialog"
import { invoke } from "@tauri-apps/api/core"
import { type as ostype } from "@tauri-apps/plugin-os"

export const UPDATER_ENABLED = window.__OPENCODE__?.updaterEnabled ?? false

export async function runUpdater({ alertOnFail }: { alertOnFail: boolean }) {
  let update
  try {
    update = await check()
  } catch {
    if (alertOnFail) await message("Failed to check for updates", { title: "Update Check Failed" })
    return
  }

  if (!update) {
    if (alertOnFail)
      await message("You are already using the latest version of OpenCode", { title: "No Update Available" })
    return
  }

  try {
    await update.download()
  } catch {
    if (alertOnFail) await message("Failed to download update", { title: "Update Failed" })
    return
  }

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

  try {
    if (ostype() === "windows") await invoke("kill_sidecar")
    await update.install()
  } catch {
    await message("Failed to install update", { title: "Update Failed" })
    return
  }

  await invoke("kill_sidecar")
  await relaunch()
}