From 94849542476f60fbe3b315b3a9f88c27f701eab0 Mon Sep 17 00:00:00 2001 From: Oj Date: Mon, 13 Dec 2021 08:11:10 +0000 Subject: [PATCH] [FirstRun] Complete rewrite --- src/firstRun/darwin.js | 2 ++ src/firstRun/index.js | 3 +- src/firstRun/linux.js | 2 ++ src/firstRun/win32.js | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/firstRun/darwin.js create mode 100644 src/firstRun/linux.js create mode 100644 src/firstRun/win32.js diff --git a/src/firstRun/darwin.js b/src/firstRun/darwin.js new file mode 100644 index 0000000..fb6b9ea --- /dev/null +++ b/src/firstRun/darwin.js @@ -0,0 +1,2 @@ +// Stub in normal Discord +exports.performFirstRunTasks = () => {}; \ No newline at end of file diff --git a/src/firstRun/index.js b/src/firstRun/index.js index 6291eaa..00cdcbc 100644 --- a/src/firstRun/index.js +++ b/src/firstRun/index.js @@ -1,4 +1,3 @@ // Stub for now at least -exports.update = (callback) => { callback(); }; -exports.performFirstRunTasks = (updater) => { }; \ No newline at end of file +module.exports = require('./' + process.platform); \ No newline at end of file diff --git a/src/firstRun/linux.js b/src/firstRun/linux.js new file mode 100644 index 0000000..fb6b9ea --- /dev/null +++ b/src/firstRun/linux.js @@ -0,0 +1,2 @@ +// Stub in normal Discord +exports.performFirstRunTasks = () => {}; \ No newline at end of file diff --git a/src/firstRun/win32.js b/src/firstRun/win32.js new file mode 100644 index 0000000..ca5d53f --- /dev/null +++ b/src/firstRun/win32.js @@ -0,0 +1,80 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.performFirstRunTasks = performFirstRunTasks; + +const fs = require('fs'); +const path = require('path'); +const paths = require('../paths'); +const squirrel = require('../updater/squirrelUpdate'); +const Constants = require('../Constants'); + +const appPath = path.resolve(process.execPath, '..'); +const rootPath = path.resolve(appFolder, '..'); +const exeFilename = path.basename(process.execPath); +const updateExe = path.join(rootFolder, 'Update.exe'); + +const iconFile = 'app.ico'; +const copyIconToRoot = () => { + const currentPath = path.join(appPath, iconFile); + const newPath = path.join(rootPath, iconFile); + + try { + fs.copyFileSync(currentPath, newPath); + return newPath; + } catch (e) { + log('FirstRun', 'Failed to copy icon to root', e); + return currentPath; + } +}; + +const updateShortcuts = (updater) => { + const filename = Constants.APP_NAME_FOR_HUMANS + '.lnk'; + const paths = [ + path.join(updater.getKnownFolder('desktop'), filename), + path.join(updater.getKnownFolder('programs'), Constants.APP_COMPANY, filename) + ]; + + const icon = copyIconToRoot(); + + for (const path of paths) { + if (!fs.existsSync(path)) continue; // Don't update already deleted paths + + updater.createShortcut({ + target_path: updateExe, + shortcut_path: shortcutPath, + arguments: `--processStart ${exeFilename}`, + icon_path: icon, + icon_index: 0, + description: Constants.APP_DESCRIPTION, + app_user_model_id: Constants.APP_ID, + working_directory: appPath + }); + } +}; + +exports.performFirstRunTasks = (updater) => { + const flagPath = path.join(paths.getUserDataVersioned(), '.first-run'); + + if (fs.existsSync(flagPath)) return; // Already ran first path, skip + + let shortcutSuccess = false; + try { + updateShortcuts(updater); + shortcutSuccess = true; + } catch (e) { + log('FirstRun', 'Error updating shortcuts', e); + } + + squirrel.installProtocol(Constants.APP_PROTOCOL, () => { + if (shortcutSuccess) { + try { + fs.writeFileSync(firstRunCompletePath, 'true'); + } catch (e) { + log('FirstRun', 'Error writing .first-run', e); + } + } + }); +}; \ No newline at end of file