Skip to content

Instantly share code, notes, and snippets.

@dsabanin
Last active April 18, 2023 09:08
Show Gist options
  • Save dsabanin/aadfbe508a627a3e95e3f024d2e5207d to your computer and use it in GitHub Desktop.
Save dsabanin/aadfbe508a627a3e95e3f024d2e5207d to your computer and use it in GitHub Desktop.
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
Copy link

@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
Copy link

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