Skip to content

Commit

Permalink
feat: Update check and notification for new electron versions (#589)
Browse files Browse the repository at this point in the history
Co-authored-by: valia fetisov <[email protected]>
  • Loading branch information
aomafarag and valiafetisov authored Mar 7, 2023
1 parent a67fa6c commit 7d8aa83
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 96 deletions.
7 changes: 7 additions & 0 deletions electron/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"arrowParens": "avoid",
"printWidth": 119
}
48 changes: 46 additions & 2 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
const path = require('path');
const url = require('url');
const { app, BrowserWindow, shell } = require('electron');
const semverGt = require('semver/functions/gt');
const { app, BrowserWindow, shell, ipcMain } = require('electron');
const { autoUpdater } = require('electron-updater');
const currentPackage = require('./package.json');

autoUpdater.autoDownload = false;

function createWindow() {
const mainWindow = new BrowserWindow({ show: false });
const mainWindow = new BrowserWindow({
show: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
mainWindow.once('ready-to-show', () => {
mainWindow.maximize();
});

// open dist/index.html using file protocol
mainWindow.loadURL(
url.format({
protocol: 'file',
slashes: true,
pathname: path.join(__dirname, 'dist/index.html'),
})
);

// open external links in the default browser instead of a new window
mainWindow.webContents.setWindowOpenHandler(details => {
if (!details.url.startsWith('/')) {
Expand All @@ -27,6 +40,37 @@ function createWindow() {

app.whenReady().then(() => {
createWindow();

// provide method to check for updates
ipcMain.handle('checkForUpdates', async () => {
const releasesUrl = `${currentPackage.repository.url}/releases`;
try {
const updateCheckResult = await autoUpdater.checkForUpdates();
const isNewerVersionAvailable = semverGt(updateCheckResult.updateInfo.version, currentPackage.version);
if (isNewerVersionAvailable) {
return {
version: currentPackage.version,
status: 'available',
url: `${releasesUrl}/latest`,
};
} else {
return {
version: currentPackage.version,
status: 'unavailable',
url: releasesUrl,
};
}
} catch (error) {
console.error('Error while retrieving updates', error.message);
return {
version: currentPackage.version,
status: 'error',
url: releasesUrl,
errorMessage: error.message,
};
}
});

// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', () => {
Expand Down
Loading

0 comments on commit 7d8aa83

Please sign in to comment.