Skip to content

Commit

Permalink
Fix update from creating desktop shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
elibroftw committed Mar 29, 2022
1 parent 31b0bc1 commit 23ae769
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions src/squirrelhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import path from "path";
import { spawn } from "child_process";
import { app } from "electron";
import { promises as fsProm } from "fs";
import fs, { promises as fsProm } from "fs";

function runUpdateExe(args: string[]): Promise<void> {
// Invokes Squirrel's Update.exe which will do things for us like create shortcuts
Expand All @@ -33,43 +33,57 @@ function runUpdateExe(args: string[]): Promise<void> {
});
}


async function removeOldShortcuts(): Promise<boolean> {
// latest name is 'Element' so if old shortcuts related to 'Riot' and 'Element (Riot)' exist,
// remove them and return True
// --removeShortcut relies on an excutable name so isn't used here
const appDataDir = process.env.APPDATA;
if (!appDataDir) return;
const newVectorLTDStartMenu = path.join(
appDataDir, 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'New Vector Ltd',
);
const riotDesktopShortcut = path.join(app.getPath('desktop'), 'Riot.lnk');
const eRiotStartMenu = path.join(
appDataDir, 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Element', 'Element (Riot).lnk',
);
const eRiotDeskShortcut = path.join(app.getPath('desktop'), 'Element (Riot).lnk');
[newVectorLTDStartMenu, riotDesktopShortcut, eRiotStartMenu, eRiotDeskShortcut].forEach(item => {
fs.stat(item, async (err, stat) => {
if(err.code === 'ENOENT') {
// remove all shortcuts and return true
await Promise.all([
fsProm.rmdir(newVectorLTDStartMenu, { recursive: true }),
fsProm.unlink(riotDesktopShortcut).catch(() => { }),
fsProm.unlink(eRiotStartMenu).catch(() => { }),
fsProm.unlink(eRiotDeskShortcut).catch(() => { })
]);
return true;
}
});
})
return false;
}

function createShortcuts() {
const target = path.basename(process.execPath);
return runUpdateExe(['--createShortcut=' + target]);
}

function checkSquirrelHooks(): boolean {
if (process.platform !== 'win32') return false;

const cmd = process.argv[1];
const target = path.basename(process.execPath);
if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') {
runUpdateExe(['--createShortcut=' + target]).then(() => {
// remove the old 'Riot' shortcuts, if they exist (update.exe --removeShortcut doesn't work
// because it always uses the name of the product as the name of the shortcut: the only variable
// is what executable you're linking to)
const appDataDir = process.env.APPDATA;
if (!appDataDir) return;
const startMenuDir = path.join(
appDataDir, 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'New Vector Ltd',
);
return fsProm.rmdir(startMenuDir, { recursive: true });
}).then(() => {
// same for 'Element (Riot) which is old now too (we have to try to delete both because
// we don't know what version we're updating from, but of course we do know this version
// is 'Element' so the two old ones are all safe to delete).
const appDataDir = process.env.APPDATA;
if (!appDataDir) return;
const oldStartMenuLink = path.join(
appDataDir, 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Element', 'Element (Riot).lnk',
);
return fsProm.unlink(oldStartMenuLink).catch(() => {});
}).then(() => {
const oldDesktopShortcut = path.join(app.getPath('desktop'), 'Element (Riot).lnk');
return fsProm.unlink(oldDesktopShortcut).catch(() => {});
}).then(() => {
const oldDesktopShortcut = path.join(app.getPath('desktop'), 'Riot.lnk');
return fsProm.unlink(oldDesktopShortcut).catch(() => {});
}).then(() => {
app.quit();
});
if (cmd === '--squirrel-install') {
removeOldShortcuts().then(() => createShortcuts()).then(() => app.quit());
return true;
}
else if (cmd === '--squirrel-updated') {
removeOldShortcuts().then(shortcutsRemoved =>
shortcutsRemoved ? createShortcuts().then(() => app.quit()) : app.quit()
);
return true;
} else if (cmd === '--squirrel-uninstall') {
const target = path.basename(process.execPath);
runUpdateExe(['--removeShortcut=' + target]).then(() => {
app.quit();
});
Expand Down

0 comments on commit 23ae769

Please sign in to comment.