Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: James Long <longster@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com> Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: Simon Klee <hello@simonklee.dk> Co-authored-by: Jay <air@live.ca> Co-authored-by: David Hill <1879069+iamdavidhill@users.noreply.github.com>
85 lines
2.9 KiB
TypeScript
85 lines
2.9 KiB
TypeScript
import { app, dialog } from "electron"
|
|
import pkg from "electron-updater"
|
|
import { UPDATER_ENABLED } from "./constants"
|
|
import { createUpdaterController, type UpdaterReadyRecord } from "./updater-controller"
|
|
import { getLogger } from "./logging"
|
|
import { getStore } from "./store"
|
|
import { setAppQuitting } from "./windows"
|
|
|
|
const { autoUpdater } = pkg
|
|
const key = "ready"
|
|
|
|
export function setupAutoUpdater(stop: () => Promise<void>) {
|
|
const logger = getLogger()
|
|
autoUpdater.logger = logger
|
|
autoUpdater.channel = "latest"
|
|
autoUpdater.allowPrerelease = false
|
|
autoUpdater.allowDowngrade = true
|
|
autoUpdater.autoDownload = false
|
|
autoUpdater.autoInstallOnAppQuit = false
|
|
logger.log("auto updater configured", {
|
|
channel: autoUpdater.channel,
|
|
allowPrerelease: autoUpdater.allowPrerelease,
|
|
allowDowngrade: autoUpdater.allowDowngrade,
|
|
currentVersion: app.getVersion(),
|
|
})
|
|
|
|
const store = getStore("opencode.updater")
|
|
return createUpdaterController({
|
|
enabled: UPDATER_ENABLED,
|
|
currentVersion: app.getVersion(),
|
|
backend: {
|
|
checkForUpdates: () => autoUpdater.checkForUpdates(),
|
|
downloadUpdate: () => autoUpdater.downloadUpdate(),
|
|
quitAndInstall: () => {
|
|
// quitAndInstall closes all windows before emitting before-quit, so
|
|
// flag the quit first to keep window ids persisted for restore.
|
|
setAppQuitting()
|
|
try {
|
|
autoUpdater.quitAndInstall()
|
|
} catch (error) {
|
|
// The install failed and the app keeps running; clear the flag so
|
|
// deliberate window closes prune ids again.
|
|
setAppQuitting(false)
|
|
throw error
|
|
}
|
|
},
|
|
},
|
|
persistence: {
|
|
get() {
|
|
const value = store.get(key)
|
|
if (!value || typeof value !== "object" || !("version" in value) || typeof value.version !== "string") return
|
|
return { version: value.version } satisfies UpdaterReadyRecord
|
|
},
|
|
set: (value) => store.set(key, value),
|
|
clear: () => store.delete(key),
|
|
},
|
|
stop,
|
|
log: (message, data) => logger.log(message, data),
|
|
})
|
|
}
|
|
|
|
export async function showUpdaterDialog(controller: ReturnType<typeof setupAutoUpdater>, alertOnFail: boolean) {
|
|
const state = await controller.check()
|
|
if (state.status === "error") {
|
|
if (!alertOnFail) return
|
|
await dialog.showMessageBox({ type: "error", message: "Update check failed.", title: "Update Error" })
|
|
return
|
|
}
|
|
if (state.status === "up-to-date") {
|
|
if (!alertOnFail) return
|
|
await dialog.showMessageBox({ type: "info", message: "You're up to date.", title: "No Updates" })
|
|
return
|
|
}
|
|
if (state.status !== "ready") return
|
|
|
|
const response = await dialog.showMessageBox({
|
|
type: "info",
|
|
message: `Update ${state.version} downloaded. Restart now?`,
|
|
title: "Update Ready",
|
|
buttons: ["Restart", "Later"],
|
|
defaultId: 0,
|
|
cancelId: 1,
|
|
})
|
|
if (response.response === 0) await controller.install()
|
|
}
|