Auto updating the application

Auto updating the application is important when you want to push the new changes to the user. If you have worked with the Visual studio code or similar applications, you can note that each month the application getting updated automatically without breaking your existing application installation. You need to configure the CI for automatic deployment. Follow the preceding section to automate the publishing your artifacts.

To configure the auto update, install the electron-updater package first to the project using the npm package manager:

npm install --save-dev electron-updater

Next, configure the publish as we discussed before. Once you configure the publish option, you can import the autoUpdater from electron-updater, which will do the work for you. Let's look at the code sample with the electron-updater. The Electron already provides a built-in class called autoUpdater.. Actually, the electron-updater package is built on top of the Electrons' autoUpdater class, which provides more functionalities to the developer. You can use the Electron's built-in class, but you need to set up the release server to work with the auto update.

With a built-in autoUpdator class, you need to set the release server URL using the following code:

autoUpdater.setFeedURL(url)

Checking for an update programmatically can be done using the autoUpdater.checkForUpdates() method. Whenever a new update is available, the autoUpdater will emit update-available method and the Electron will download the update automatically. As the native Electron autoUpdater is of low level, let's use the electron-updater package. The API itself almost same as the native autoUpdator class including the class names and function names. Here is a sample implementation done using electron-update. The coding should be done for the main process:

const {app, BrowserWindow, Menu, protocol, ipcMain} = require('electron');
const log = require('electron-log');
const {autoUpdater} = require("electron-updater");

autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');

let template = []
if (process.platform === 'darwin') {
// OS X
const name = app.getName();
template.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click() { app.quit(); }
},
]
})
}

let win;

function sendStatusToWindow(text) {
log.info(text);
win.webContents.send('message', text);
}
function createDefaultWindow() {
win = new BrowserWindow();
win.webContents.openDevTools();
win.on('closed', () => {
win = null;
});
win.loadURL(`file://${__dirname}/version.html#v${app.getVersion()}`);
return win;
}
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...');
})
autoUpdater.on('update-available', (ev, info) => {
sendStatusToWindow('Update available.');
})
autoUpdater.on('update-not-available', (ev, info) => {
sendStatusToWindow('Update not available.');
})
autoUpdater.on('error', (ev, err) => {
sendStatusToWindow('Error in auto-updater.');
})
autoUpdater.on('download-progress', (ev, progressObj) => {
sendStatusToWindow('Download progress...');
})
autoUpdater.on('update-downloaded', (ev, info) => {
sendStatusToWindow('Update downloaded; will install in 5 seconds');
});
app.on('ready', function() {
// Create the Menu
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

createDefaultWindow();
});
app.on('window-all-closed', () => {
app.quit();
});

//-------------------------------------------------------------------
// Auto updates
//
// For details about these events, see the Wiki:
// https://github.com/electron-userland/electron-builder/wiki/Auto-Update#events
//
// The app doesn't need to listen to any events except `update-downloaded`
//
// Uncomment any of the below events to listen for them. Also,
// look in the previous section to see them being used.
//-------------------------------------------------------------------
// autoUpdater.on('checking-for-update', () => {
// })
// autoUpdater.on('update-available', (ev, info) => {
// })
// autoUpdater.on('update-not-available', (ev, info) => {
// })
// autoUpdater.on('error', (ev, err) => {
// })
autoUpdater.on('download-progress', (ev, progressObj) => {
})
autoUpdater.on('update-downloaded', (ev, info) => {
// Wait 5 seconds, then quit and install
// In your application, you don't need to wait 5 seconds.
// You could call autoUpdater.quitAndInstall(); immediately
setTimeout(function() {
autoUpdater.quitAndInstall();
}, 5000)
})

app.on('ready', function() {
autoUpdater.checkForUpdates();
});

You don't need to use all these events for configuring the update. Only the update-downloaded event is needed to do our work. The autoUpdater.quitAndInall method will quit the application and install the new updates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.111.134