Skip to content

Commit

Permalink
wip: one click update
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 11, 2025
1 parent 5bb9721 commit bb694a5
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 15 deletions.
1 change: 1 addition & 0 deletions satellite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"debounce-fn": "^6.0.0",
"electron-about-window": "^1.15.2",
"electron-store": "^10.0.0",
"electron-updater": "^6.3.9",
"eventemitter3": "^5.0.1",
"exit-hook": "^4.0.0",
"infinitton-idisplay": "^1.2.0",
Expand Down
7 changes: 7 additions & 0 deletions satellite/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import { SatelliteConfig, ensureFieldsPopulated } from './config.js'
import { ApiConfigData, ApiStatusResponse, compileConfig, compileStatus, updateConfig } from './apiTypes.js'
import { fileURLToPath } from 'url'
import { MdnsAnnouncer } from './mdnsAnnouncer.js'
import { ElectronUpdater } from './electronUpdater.js'

const appConfig = new electronStore<SatelliteConfig>({
// schema: satelliteConfigSchema,
// migrations: satelliteConfigMigrations,
})
ensureFieldsPopulated(appConfig)

const electronUpdater = new ElectronUpdater()

let tray: Tray | undefined
let configWindow: BrowserWindow | undefined

Expand Down Expand Up @@ -104,6 +107,8 @@ trayMenu.append(
},
}),
)
trayMenu.append(electronUpdater.menuItem)
trayMenu.append(electronUpdater.installMenuItem)
trayMenu.append(
new MenuItem({
label: 'About',
Expand All @@ -121,6 +126,8 @@ app.whenReady()
.then(async () => {
console.log('App ready')

electronUpdater.check()

tryConnect()
restartRestApi()
mdnsAnnouncer.start()
Expand Down
103 changes: 103 additions & 0 deletions satellite/src/electronUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { MenuItem, dialog } from 'electron'
import electronUpdater from 'electron-updater'
import { createRequire } from 'module'

const { autoUpdater } = electronUpdater

// For development testing
// autoUpdater.forceDevUpdateConfig = true

const require = createRequire(import.meta.url)
const pkgJson = require('../package.json')
const updateChannel: string | undefined = pkgJson.updateChannel

// Configure the updater
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = true
autoUpdater.autoRunAppAfterInstall = true
autoUpdater.requestHeaders = { 'User-Agent': `Companion Satellite v${autoUpdater.currentVersion}` }
autoUpdater.channel = updateChannel ?? ''

export function isUpdateSupported(): boolean {
return (
!!updateChannel &&
(process.platform === 'win32' || process.platform === 'darwin') &&
autoUpdater.isUpdaterActive()
)
}

export class ElectronUpdater {
readonly menuItem: MenuItem
readonly installMenuItem: MenuItem

constructor() {
this.menuItem = new MenuItem({
label: 'Check for updates',
visible: isUpdateSupported(),
click: () => this.check(true),
})
this.installMenuItem = new MenuItem({
label: 'Install pending update',
visible: false,
click: () => this.installPending(),
})
}

installPending(): void {
autoUpdater
.downloadUpdate()
.then(() => {
autoUpdater.quitAndInstall()
})
.catch((e) => {
dialog.showErrorBox(
'Install update failed',
'Failed to download update.\nTry again later, or try installing the update manually.',
)
console.log('failed to download', e)
})
}

check(notify = false): void {
if (!isUpdateSupported()) return

autoUpdater
.checkForUpdates()
.then((info) => {
this.menuItem.visible = !info
this.installMenuItem.visible = !!info

if (!notify) return

if (info) {
dialog
.showMessageBox({
title: 'Companion Satellite',
message: 'An update is available',
buttons: ['Install', 'Cancel'],
})
.then((v) => {
if (v.response === 0) {
this.installPending()
}
})
.catch((e) => {
console.error('dialog error', e)
})
} else {
dialog
.showMessageBox({
title: 'Companion Satellite',
message: 'No update is available',
buttons: ['Close'],
})
.catch((e) => {
console.error('dialog error', e)
})
}
})
.catch((e) => {
console.error('Failed to check for updates', e)
})
}
}
6 changes: 5 additions & 1 deletion tools/build_electron.mts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ if (platform === 'mac-x64' || platform === 'darwin-x64') {
await fs.remove('./electron-output')

const options: electronBuilder.Configuration = {
publish: [],
publish: {
provider: 'generic',
publishAutoUpdate: false,
url: 'https://builds.julusian.dev/companion-satellite',
},
productName: 'Companion Satellite',
appId: 'remote.companion.bitfocus.no',
afterSign: 'tools/notarize.cjs',
Expand Down
103 changes: 89 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ __metadata:
languageName: node
linkType: hard

"@eslint-community/eslint-utils@npm:^4.1.2, @eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0":
"@eslint-community/eslint-utils@npm:^4.1.2":
version: 4.4.1
resolution: "@eslint-community/eslint-utils@npm:4.4.1"
dependencies:
Expand All @@ -700,7 +700,25 @@ __metadata:
languageName: node
linkType: hard

"@eslint-community/regexpp@npm:^4.10.0, @eslint-community/regexpp@npm:^4.11.0":
"@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0":
version: 4.4.0
resolution: "@eslint-community/eslint-utils@npm:4.4.0"
dependencies:
eslint-visitor-keys: "npm:^3.3.0"
peerDependencies:
eslint: ^6.0.0 || ^7.0.0 || >=8.0.0
checksum: 10c0/7e559c4ce59cd3a06b1b5a517b593912e680a7f981ae7affab0d01d709e99cd5647019be8fafa38c350305bc32f1f7d42c7073edde2ab536c745e365f37b607e
languageName: node
linkType: hard

"@eslint-community/regexpp@npm:^4.10.0":
version: 4.11.0
resolution: "@eslint-community/regexpp@npm:4.11.0"
checksum: 10c0/0f6328869b2741e2794da4ad80beac55cba7de2d3b44f796a60955b0586212ec75e6b0253291fd4aad2100ad471d1480d8895f2b54f1605439ba4c875e05e523
languageName: node
linkType: hard

"@eslint-community/regexpp@npm:^4.11.0":
version: 4.12.0
resolution: "@eslint-community/regexpp@npm:4.12.0"
checksum: 10c0/168b4062544b220cd547f14a4d4d131f25a19098e0716832fa2fea1d1aa3894ec3014ed6f42a9638b0693490dd0d583c974064e0e621cbe146e61e20bab55f75
Expand Down Expand Up @@ -1906,16 +1924,7 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:*, @types/node@npm:>=20":
version: 22.8.1
resolution: "@types/node@npm:22.8.1"
dependencies:
undici-types: "npm:~6.19.8"
checksum: 10c0/83550fdf72a7db5b55eceac3f4fb038844eaee20202bdd2297a8248370cfa08317bda1605b781a8043eda4f173b75e73632e652fc85509eb14dfef78fa17337f
languageName: node
linkType: hard

"@types/node@npm:^20.17.1, @types/node@npm:^20.9.0":
"@types/node@npm:*, @types/node@npm:^20.17.1, @types/node@npm:^20.9.0":
version: 20.17.1
resolution: "@types/node@npm:20.17.1"
dependencies:
Expand All @@ -1924,6 +1933,15 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:>=20":
version: 22.8.1
resolution: "@types/node@npm:22.8.1"
dependencies:
undici-types: "npm:~6.19.8"
checksum: 10c0/83550fdf72a7db5b55eceac3f4fb038844eaee20202bdd2297a8248370cfa08317bda1605b781a8043eda4f173b75e73632e652fc85509eb14dfef78fa17337f
languageName: node
linkType: hard

"@types/plist@npm:^3.0.1":
version: 3.0.2
resolution: "@types/plist@npm:3.0.2"
Expand Down Expand Up @@ -2717,6 +2735,16 @@ __metadata:
languageName: node
linkType: hard

"builder-util-runtime@npm:9.2.10":
version: 9.2.10
resolution: "builder-util-runtime@npm:9.2.10"
dependencies:
debug: "npm:^4.3.4"
sax: "npm:^1.2.4"
checksum: 10c0/28681b8037ad0fb6a33c79532656f7eeddcf7c1d3c922253630d8794929c20a78adc6e4028111708643a1d10e25812c65ac1241886570ff12d6aa6308abe9015
languageName: node
linkType: hard

"builder-util-runtime@npm:9.2.4":
version: 9.2.4
resolution: "builder-util-runtime@npm:9.2.4"
Expand Down Expand Up @@ -3579,6 +3607,22 @@ __metadata:
languageName: node
linkType: hard

"electron-updater@npm:^6.3.9":
version: 6.3.9
resolution: "electron-updater@npm:6.3.9"
dependencies:
builder-util-runtime: "npm:9.2.10"
fs-extra: "npm:^10.1.0"
js-yaml: "npm:^4.1.0"
lazy-val: "npm:^1.0.5"
lodash.escaperegexp: "npm:^4.1.2"
lodash.isequal: "npm:^4.5.0"
semver: "npm:^7.6.3"
tiny-typed-emitter: "npm:^2.1.0"
checksum: 10c0/e692e8d744ba311caf17bfdf59d469b3f331b8dcbb174786ed69bba52b630093e8cd7d48f04c10e28cd25ead9c0896d42c92a25525275daaf47681da0dfd2094
languageName: node
linkType: hard

"electron@npm:31.7.2":
version: 31.7.2
resolution: "electron@npm:31.7.2"
Expand Down Expand Up @@ -4128,7 +4172,7 @@ __metadata:
languageName: node
linkType: hard

"eslint-visitor-keys@npm:^3.4.3":
"eslint-visitor-keys@npm:^3.3.0, eslint-visitor-keys@npm:^3.4.3":
version: 3.4.3
resolution: "eslint-visitor-keys@npm:3.4.3"
checksum: 10c0/92708e882c0a5ffd88c23c0b404ac1628cf20104a108c745f240a13c332a11aac54f49a22d5762efbffc18ecbc9a580d1b7ad034bf5f3cc3307e5cbff2ec9820
Expand Down Expand Up @@ -4668,7 +4712,7 @@ __metadata:
languageName: node
linkType: hard

"get-tsconfig@npm:^4.7.0, get-tsconfig@npm:^4.7.5":
"get-tsconfig@npm:^4.7.0":
version: 4.8.1
resolution: "get-tsconfig@npm:4.8.1"
dependencies:
Expand All @@ -4677,6 +4721,15 @@ __metadata:
languageName: node
linkType: hard

"get-tsconfig@npm:^4.7.5":
version: 4.7.5
resolution: "get-tsconfig@npm:4.7.5"
dependencies:
resolve-pkg-maps: "npm:^1.0.0"
checksum: 10c0/a917dff2ba9ee187c41945736bf9bbab65de31ce5bc1effd76267be483a7340915cff232199406379f26517d2d0a4edcdbcda8cca599c2480a0f2cf1e1de3efa
languageName: node
linkType: hard

"glob-parent@npm:^5.1.2":
version: 5.1.2
resolution: "glob-parent@npm:5.1.2"
Expand Down Expand Up @@ -5838,6 +5891,20 @@ __metadata:
languageName: node
linkType: hard

"lodash.escaperegexp@npm:^4.1.2":
version: 4.1.2
resolution: "lodash.escaperegexp@npm:4.1.2"
checksum: 10c0/484ad4067fa9119bb0f7c19a36ab143d0173a081314993fe977bd00cf2a3c6a487ce417a10f6bac598d968364f992153315f0dbe25c9e38e3eb7581dd333e087
languageName: node
linkType: hard

"lodash.isequal@npm:^4.5.0":
version: 4.5.0
resolution: "lodash.isequal@npm:4.5.0"
checksum: 10c0/dfdb2356db19631a4b445d5f37868a095e2402292d59539a987f134a8778c62a2810c2452d11ae9e6dcac71fc9de40a6fedcb20e2952a15b431ad8b29e50e28f
languageName: node
linkType: hard

"lodash.isplainobject@npm:^4.0.6":
version: 4.0.6
resolution: "lodash.isplainobject@npm:4.0.6"
Expand Down Expand Up @@ -7340,6 +7407,7 @@ __metadata:
electron-about-window: "npm:^1.15.2"
electron-builder: "npm:^24.13.3"
electron-store: "npm:^10.0.0"
electron-updater: "npm:^6.3.9"
eventemitter3: "npm:^5.0.1"
exit-hook: "npm:^4.0.0"
infinitton-idisplay: "npm:^1.2.0"
Expand Down Expand Up @@ -7886,6 +7954,13 @@ __metadata:
languageName: node
linkType: hard

"tiny-typed-emitter@npm:^2.1.0":
version: 2.1.0
resolution: "tiny-typed-emitter@npm:2.1.0"
checksum: 10c0/522bed4c579ee7ee16548540cb693a3d098b137496110f5a74bff970b54187e6b7343a359b703e33f77c5b4b90ec6cebc0d0ec3dbdf1bd418723c5c3ce36d8a2
languageName: node
linkType: hard

"tmp-promise@npm:^3.0.2":
version: 3.0.3
resolution: "tmp-promise@npm:3.0.3"
Expand Down

0 comments on commit bb694a5

Please sign in to comment.