Skip to content

Commit

Permalink
auto-update fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Apr 26, 2024
1 parent 1b90c45 commit a4bd61d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ increment-build-number:

publish:
$(eval RELEASES := $(shell find out -name '*$(VERSION).zip'))
gh release create v$(VERSION) --repo https://github.com/nbonamy/witsy --title $(VERSION) $(RELEASES) --notes "Release $(VERSION)"
gh release create v$(VERSION) --repo https://github.com/nbonamy/witsy --title $(VERSION) $(RELEASES) --generate-notes
2 changes: 1 addition & 1 deletion build/build_number.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164
165
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "witsy",
"productName": "Witsy",
"version": "1.4.1",
"version": "1.4.2",
"description": "Witsy: desktop AI assistant",
"repository": {
"type": "git",
Expand Down
31 changes: 6 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import { Chat, Command, Prompt } from './types/index.d';
import { Configuration } from './types/config.d';
import process from 'node:process';
import { app, Menu, Tray, BrowserWindow, ipcMain, nativeImage, clipboard, autoUpdater, dialog } from 'electron';
import { app, Menu, Tray, BrowserWindow, ipcMain, nativeImage, clipboard } from 'electron';
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
import { PythonShell } from 'python-shell';
import Store from 'electron-store';
import log from 'electron-log/main';
import { wait } from './main/utils';

import AutoUpdater from './main/autoupdate';
import * as config from './main/config';
import * as history from './main/history';
import * as commands from './main/commands';
Expand Down Expand Up @@ -39,30 +40,10 @@ if (require('electron-squirrel-startup')) {
}

// auto-update
// eslint-disable-next-line @typescript-eslint/no-var-requires
const server = 'https://update.electronjs.org'
const feed = `${server}/nbonamy/witsy/${process.platform}-${process.arch}/${app.getVersion()}`
console.log('Checking for updates at', feed)
autoUpdater.setFeedURL({ url: feed })
autoUpdater.on('error', (error) => console.error('Error while checking for updates', error) )
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {

const dialogOpts: Electron.MessageBoxOptions = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.'
}
const autoUpdater = new AutoUpdater({
preUpdate: () => quitAnyway = true
});

dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
quitAnyway = true;
autoUpdater.quitAndInstall()
}
})
})
autoUpdater.checkForUpdates()

// // look for menus as soon as possible
// import MacosAutomator from './automations/macos2';
Expand All @@ -82,7 +63,6 @@ const registerShortcuts = () => {
import trayIconMacos from '../assets/bulbTemplate.png?asset';
// eslint-disable-next-line import/no-unresolved
import trayIconWindows from '../assets/[email protected]?asset';
import { UpdateSourceType } from 'update-electron-app';
const trayIcon = process.platform === 'darwin' ? trayIconMacos : trayIconWindows;

let tray: Tray = null;
Expand Down Expand Up @@ -122,6 +102,7 @@ app.whenReady().then(() => {
// install the menu
menu.installMenu(app, {
quit: app.quit,
checkForUpdates: autoUpdater.check,
newChat: window.openMainWindow,
settings: window.openSettingsWindow,
});
Expand Down
90 changes: 90 additions & 0 deletions src/main/autoupdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

import { app, autoUpdater, dialog } from 'electron';

export default class {

manualUpdate = false

constructor(public hooks: {
preUpdate?: () => void;
}) {
this.install();
}

install = () => {

// basic setup
const server = 'https://update.electronjs.org'
const feed = `${server}/nbonamy/witsy/${process.platform}-${process.arch}/${app.getVersion()}`
console.log('Checking for updates at', feed)
autoUpdater.setFeedURL({ url: feed })

// error
autoUpdater.on('error', (error) => {
console.error('Error while checking for updates', error)
if (this.manualUpdate) {
dialog.showErrorBox('Witsy', 'Error while checking for updates. Please try again later.')
}
})

// available
autoUpdater.on('update-available', () => {
console.log('Update available. Downloading…')
if (this.manualUpdate) {
dialog.showMessageBox({
type: 'info',
message: 'Witsy',
detail: 'A new version is available. Downloading now…'
})
}
})

// not available
autoUpdater.on('update-not-available', () => {
if (this.manualUpdate) {
dialog.showMessageBox({
title: 'Witsy',
message: 'You are already using the latest version of Witsy.',
})
}
})

// downloaded
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {

const dialogOpts: Electron.MessageBoxOptions = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.'
}

dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
autoUpdater.quitAndInstall()
}
})
})

// before quit for update
autoUpdater.on('before-quit-for-update', () => {
this.hooks.preUpdate?.()
})

// check now and schedule
autoUpdater.checkForUpdates()
setInterval(() => {
console.log('Scheduled update-check')
this.manualUpdate = false;
autoUpdater.checkForUpdates()
}, 60*60*1000)

}

check = () => {
this.manualUpdate = true;
autoUpdater.checkForUpdates()
}

}
4 changes: 4 additions & 0 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const template = (app: App, callbacks: anyDict) => [
label: app.name,
submenu: [
{ role: 'about' },
{
label: 'Check for Updates…',
click: async () => callbacks.checkForUpdates()
},
{ type: 'separator' },
{
label: 'Settings…',
Expand Down

0 comments on commit a4bd61d

Please sign in to comment.