Skip to content

Instantly share code, notes, and snippets.

@dsabanin

dsabanin/start.ts

Last active Feb 12, 2019
Embed
What would you like to do?
Electron auto-update startup fix
// The idea here is to make sure that ShipIt process that copies the updated app exits
// before we launch the new version. The code below checks if the update is still in progress,
// which means that we are running the old app still, it keeps checking until ShipIt exits and restarts
// the app hoping that a new version will start up at that time.
//
// "find-process": "~1.2.1",
import findProcess from 'find-process';
const BUNDLE_NAME = 'com.myorganization.bundleName';
const SHIP_IT_BINARY = 'ShipIt';
async function makeSureAutoUpdateFinished(): Promise<void> {
const shipItProcesses = await findProcess('name', SHIP_IT_BINARY);
if (shipItProcesses.some(f => f.cmd.includes(BUNDLE_NAME))) {
// if we don't restart, the old app from memory will keep running
shouldRestartBeforeLaunch = true;
console.debug('Waiting for auto update to finish');
setTimeout(makeSureAutoUpdateFinished, 1500);
} else {
if (shouldRestartBeforeLaunch) {
try {
const Electron = require('electron');
Electron.app.relaunch();
Electron.app.exit(0);
} catch (error) {
console.error('Failed to restart the app through electron', error);
process.exit(1);
}
} else {
execute();
}
}
}
function execute(): void {
// here goes your electron init
require('./electronMain/start');
}
@avner-hoffmann

This comment has been minimized.

Copy link

@avner-hoffmann avner-hoffmann commented Feb 12, 2019

@dsabanin - thanks for the response
By "added this code before the electron startup procedure" you mean to set "start.ts" as the main file (the app startup file) and execute all the other app modules only later, when ShipIt finished running and "execute" was called?

@avner-hoffmann

This comment has been minimized.

Copy link

@avner-hoffmann avner-hoffmann commented Feb 12, 2019

btw, where's the call to makeSureAutoUpdateFinished?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment