54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
const { app } = require('electron');
|
|
const ChildProcess = require('child_process');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
function handleSquirrelEvent() {
|
|
if (process.argv.length === 1) {
|
|
return false;
|
|
}
|
|
|
|
const appFolder = path.resolve(process.execPath, '..');
|
|
const rootAtomFolder = path.resolve(appFolder, '..');
|
|
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
|
|
const exeName = path.basename(process.execPath);
|
|
|
|
const spawn = function (command, args) {
|
|
let spawnedProcess;
|
|
try {
|
|
spawnedProcess = ChildProcess.spawn(command, args, { detached: true });
|
|
} catch (error) {
|
|
// Log error or handle appropriately
|
|
}
|
|
return spawnedProcess;
|
|
};
|
|
|
|
const spawnUpdate = function (args) {
|
|
return spawn(updateDotExe, args);
|
|
};
|
|
|
|
const squirrelEvent = process.argv[1];
|
|
switch (squirrelEvent) {
|
|
case '--squirrel-install':
|
|
case '--squirrel-updated':
|
|
spawnUpdate(['--createShortcut', exeName]);
|
|
const desktopShortcutPath = path.join(app.getPath('desktop'), `${exeName}.lnk`);
|
|
if (fs.existsSync(desktopShortcutPath)) {
|
|
fs.unlinkSync(desktopShortcutPath);
|
|
}
|
|
setTimeout(app.quit, 1000);
|
|
return true;
|
|
|
|
case '--squirrel-uninstall':
|
|
spawnUpdate(['--removeShortcut', exeName]);
|
|
setTimeout(app.quit, 1000);
|
|
return true;
|
|
|
|
case '--squirrel-obsolete':
|
|
app.quit();
|
|
return true;
|
|
}
|
|
return false; // Ensure a boolean is always returned
|
|
}
|
|
|
|
module.exports = { handleSquirrelEvent };
|