codeburn/app/electron/auto-update.ts
Resham Joshi fce75d5420
feat(app,windows): one-click updates for the desktop app and the Windows tray (#1685)
* feat(desktop): one-click updates through electron-updater on the update-feeds release

* feat(tray): signed one-click updates through tauri-plugin-updater; feed publishing and release docs

* ci(tray): build without the updater key until it is configured

* chore(windows): set the tray updater public key

* ci(windows): skip updater signing in the release build smoke

* ci(windows): build without updater artifacts when no signing key is available

* ci(windows): turn off updater artifacts through TAURI_CONFIG, not CLI flags pwsh mangles

* ci(windows): run tauri build under bash so --no-sign reaches the CLI (pwsh drops the -- separator)
2026-10-08 05:06:55 +02:00

108 lines
3.7 KiB
TypeScript

// One-click updates through electron-updater, reading the fixed update-feeds release rather
// than GitHub's /releases/latest, which four release lines share. Nothing downloads until the
// reader clicks; the next click restarts into the new version.
import { compareSemver, type UpdateChecker, type UpdateStatus } from './updates'
// Off until the maintainer picks Authenticode signing or hash-only trust for the NSIS
// installer (RELEASING.md, "Windows NSIS auto-update"). Windows keeps the link banner.
export const WINDOWS_AUTO_UPDATE = false
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000
export function autoUpdateSupported(env: {
platform: string
isPackaged: boolean
mas: boolean
windowsStore: boolean
appImage?: string
}): boolean {
if (!env.isPackaged) return false
if (env.platform === 'darwin') return !env.mas
// Only the AppImage replaces itself; deb, rpm, snap and Flathub update through their package manager.
if (env.platform === 'linux') return Boolean(env.appImage)
if (env.platform === 'win32') return WINDOWS_AUTO_UPDATE && !env.windowsStore
return false
}
export type Updater = {
autoDownload: boolean
checkForUpdates(): Promise<{ updateInfo: { version: string } } | null>
downloadUpdate(): Promise<unknown>
quitAndInstall(): void
on(event: 'update-downloaded', listener: () => void): unknown
}
export type AutoUpdateChecker = UpdateChecker & {
download(): Promise<UpdateStatus>
install(): void
}
export function createAutoUpdateChecker(opts: {
updater: Updater
currentVersion: string
onChange: (status: UpdateStatus) => void
now?: () => number
}): AutoUpdateChecker {
const { updater, currentVersion, onChange } = opts
const now = opts.now ?? (() => Date.now())
updater.autoDownload = false
let status: UpdateStatus = { currentVersion, latestVersion: null, updateAvailable: false, tag: null }
let lastCheckedAt = 0
let inflight: Promise<UpdateStatus> | null = null
const set = (next: UpdateStatus) => { status = next; onChange(status) }
updater.on('update-downloaded', () => {
if (status.install === 'downloading') set({ ...status, install: 'ready' })
})
const check = (): Promise<UpdateStatus> => {
if (inflight) return inflight
inflight = (async () => {
try {
const result = await updater.checkForUpdates()
lastCheckedAt = now()
const latest = result?.updateInfo.version ?? null
if (!latest) return status
// A download already under way or finished is not reset by a later check.
if (status.install === 'downloading' || status.install === 'ready') return status
const updateAvailable = compareSemver(latest, currentVersion) > 0
status = {
currentVersion,
latestVersion: latest,
updateAvailable,
tag: updateAvailable ? `desktop-v${latest}` : null,
...(updateAvailable ? { install: 'available' as const } : {}),
}
} catch {
// Offline or no feed yet: keep what was known and retry next cycle.
} finally {
inflight = null
}
return status
})()
return inflight
}
return {
check,
getStatus: () => (lastCheckedAt !== 0 && now() - lastCheckedAt < CHECK_INTERVAL_MS ? Promise.resolve(status) : check()),
async download() {
if (status.install !== 'available') return status
set({ ...status, install: 'downloading' })
try {
await updater.downloadUpdate()
} catch {
// Fall back to the manual download link rather than a button that keeps failing.
const { install: _dropped, ...rest } = status
set(rest)
}
return status
},
install() {
if (status.install === 'ready') updater.quitAndInstall()
},
}
}