From 9fddc7ff34f8865cb7fce5eac0777246bc9fc849 Mon Sep 17 00:00:00 2001 From: Florian Metz Date: Sun, 26 Jan 2020 10:50:29 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20cmd.exe=20(updater)=20>=20PreMiD-Up?= =?UTF-8?q?dater.exe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 6 +- installer_assets/updater.xml | 5 +- package.json | 5 +- src/index.ts | 20 ++--- src/managers/discordManager.ts | 153 ++++++++++++++++---------------- src/managers/settingsManager.ts | 30 +++---- src/managers/trayManager.ts | 35 +++----- src/util/updateChecker.ts | 25 ++---- tsconfig.json | 5 +- yarn.lock | 5 -- 10 files changed, 135 insertions(+), 154 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index dd45eaa..30fc384 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -83,9 +83,9 @@ jobs: mv upgrader.app.tgz ../ rm -rf upgrader.app upgrader.app.zip unzip windows-latest.zip - cp updater.exe windows-latest/PreMiD-win32-x64/updater.exe + cp PreMiD-Updater.exe windows-latest/PreMiD-win32-x64/PreMiD-Updater.exe cp update.ini windows-latest/PreMiD-win32-x64/update.ini - cp updater.exe windows-latest/PreMiD-win32-ia32/updater.exe + cp PreMiD-Updater.exe windows-latest/PreMiD-win32-ia32/PreMiD-Updater.exe cp update.ini windows-latest/PreMiD-win32-ia32/update.ini cd windows-latest/PreMiD-win32-x64/ zip -r ../../PreMiD-win32-x64.zip . @@ -96,7 +96,7 @@ jobs: cd ../.. rm -rf windows-latest windows-latest.zip unzip macOS-latest.zip - cp -r updater.app macOS-latest/PreMiD-darwin-x64/updater.app + cp -r PreMiD-Updater.app macOS-latest/PreMiD-darwin-x64/PreMiD-Updater.app cp update.ini macOS-latest/PreMiD-darwin-x64/update.ini cd macOS-latest/PreMiD-darwin-x64/ zip -r ../../PreMiD-darwin-x64.zip . diff --git a/installer_assets/updater.xml b/installer_assets/updater.xml index ddb1fbd..072c1a3 100644 --- a/installer_assets/updater.xml +++ b/installer_assets/updater.xml @@ -4,10 +4,11 @@ Timeraa 2.0 1 - asInvoker 1 ../dist/app/ - updater.${platform_exec_suffix} + PreMiD-Updater.${platform_exec_suffix} osx-intel osx-x86_64 1 + appIcon.ico + appIcon.icns \ No newline at end of file diff --git a/package.json b/package.json index 0f35937..bbae043 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "init": "tsc --skipLibCheck && tsc pkg util/prepare util/zip && devScript --copyOnly", "start": "electron dist/app/.", - "dev": "devScript", + "dev": "nodemon --exec yarn start -S --watch dist/app", "pkg": "rimraf dist && tsc --skipLibCheck && devScript --copyOnly && cd dist/app/ && yarn && cd ../../ && node pkg", "deploy": "tsc --skipLibCheck .github/deploy && cd .github && node deploy.js" }, @@ -49,7 +49,6 @@ "axios": "0.19.2", "discord-rpc": "github:discordjs/RPC", "electron-store": "5.1.0", - "socket.io": "2.3.0", - "sudo-prompt": "9.1.1" + "socket.io": "2.3.0" } } diff --git a/src/index.ts b/src/index.ts index 93fb287..568c634 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,13 @@ -import { app, dialog } from "electron"; -//* Source .map support if devEnv -if (!app.isPackaged) require("source-map-support").install(); +import "source-map-support/register"; +import { app, dialog } from "electron"; import { init as initSocket, socket } from "./managers/socketManager"; -import { init as initTray } from "./managers/trayManager"; import { update as initAutoLaunch } from "./managers/launchManager"; import { platform } from "os"; import { checkForUpdate } from "./util/updateChecker"; +import { TrayManager } from "./managers/trayManager"; + +export let trayManager: TrayManager; //* Define and set it to null //* Set AppUserModelId for task manager etc @@ -14,13 +15,10 @@ import { checkForUpdate } from "./util/updateChecker"; export let updateCheckerInterval = null; app.setAppUserModelId("Timeraa.PreMiD"); app.whenReady().then(async () => { - //* Init auto launch - //* Check for updates > Update and relaunch - //* Init socket - //* init application tray icon - //* If app is packaged, run an update check every 15 mins - initAutoLaunch(); - await Promise.all([checkForUpdate(true), initSocket(), initTray()]); + trayManager = new TrayManager(); + + await Promise.all([checkForUpdate(true), initAutoLaunch(), initSocket()]); + app.isPackaged ? (updateCheckerInterval = setInterval(checkForUpdate, 15 * 1000 * 60)) : undefined; diff --git a/src/managers/discordManager.ts b/src/managers/discordManager.ts index 566e8b0..1166afb 100644 --- a/src/managers/discordManager.ts +++ b/src/managers/discordManager.ts @@ -1,12 +1,12 @@ import { Client } from "discord-rpc"; import { app } from "electron"; import { platform } from "os"; -import { tray } from "./trayManager"; import { info } from "../util/debug"; //* Import custom types import Presence from "../../@types/PreMiD/Presence"; import PresenceData from "../../@types/PreMiD/PresenceData"; +import { trayManager } from ".."; //* Define Presence array let loggedInPresences: Array = []; @@ -16,20 +16,20 @@ let loggedInPresences: Array = []; * @param presence PresenceData to set activity */ export function setActivity(presence: PresenceData) { - //* If platform is darwin (Mac OS) set trayTitle if theres one - //* Check if theres an active RPC connection that we can use - //* If we have one, use it - //* Else create one and use it - //* Show debug - if (platform() === "darwin" && presence.trayTitle) - tray.setTitle(presence.trayTitle); - let rpc = loggedInPresences.find(p => p.clientId === presence.clientId); - if (rpc) rpc.rpc.setActivity(presence.presenceData).catch(destroy); - else - loginPresence(presence.clientId).then(p => - p.rpc.setActivity(presence.presenceData).catch(destroy) - ); - info("setActivity"); + //* If platform is darwin (Mac OS) set trayTitle if theres one + //* Check if theres an active RPC connection that we can use + //* If we have one, use it + //* Else create one and use it + //* Show debug + if (platform() === "darwin" && presence.trayTitle) + trayManager.tray.setTitle(presence.trayTitle); + let rpc = loggedInPresences.find(p => p.clientId === presence.clientId); + if (rpc) rpc.rpc.setActivity(presence.presenceData).catch(destroy); + else + loginPresence(presence.clientId).then(p => + p.rpc.setActivity(presence.presenceData).catch(destroy) + ); + info("setActivity"); } /** @@ -37,21 +37,21 @@ export function setActivity(presence: PresenceData) { * @param clientId clientId of presence to clear */ export function clearActivity(clientId: string = undefined) { - //* Clear tray title - //* If clientId set - //* Else map through presences and clear them - //* Show Debug - if (platform() === "darwin") tray.setTitle(""); - if (clientId) { - //* Check if this presence is logged in - //* If it is clear its activity - //* Return to prevent further actions - let pTC = loggedInPresences.find(p => p.clientId === clientId); - if (pTC) pTC.rpc.clearActivity().catch(destroy); - return; - } - loggedInPresences.map(p => p.rpc.clearActivity().catch(destroy)); - info("clearActivity"); + //* Clear tray title + //* If clientId set + //* Else map through presences and clear them + //* Show Debug + if (platform() === "darwin") trayManager.tray.setTitle(""); + if (clientId) { + //* Check if this presence is logged in + //* If it is clear its activity + //* Return to prevent further actions + let pTC = loggedInPresences.find(p => p.clientId === clientId); + if (pTC) pTC.rpc.clearActivity().catch(destroy); + return; + } + loggedInPresences.map(p => p.rpc.clearActivity().catch(destroy)); + info("clearActivity"); } /** @@ -59,60 +59,61 @@ export function clearActivity(clientId: string = undefined) { * @param clientId client ID of presence */ function loginPresence(clientId: string) { - //* Return promise that will resolve to either Presence or null - return new Promise((resolve, reject) => { - //* Create presence object - //* Add presence to object - //* Try login with client id - //* Once RPC connection is ready - //* Destroy all clients when Discord closed (My issue #42) - let presence: Presence = { - clientId: clientId, - rpc: new Client({ transport: "ipc" }), - ready: false - }; - loggedInPresences.push(presence); - presence.rpc.login({ clientId: clientId }).catch(() => { - //* If couldn't log in remove it from loggedInPresences - //* Reject promise - loggedInPresences = loggedInPresences.filter( - p => p.clientId !== presence.clientId - ); - reject(); - }); - presence.rpc.once("ready", () => { - //* Update status - //* Resolve with presence - presence.ready = true; - resolve(presence); - }); - // @ts-ignore - presence.rpc.once("disconnected", destroy); - }); + //* Return promise that will resolve to either Presence or null + return new Promise((resolve, reject) => { + //* Create presence object + //* Add presence to object + //* Try login with client id + //* Once RPC connection is ready + //* Destroy all clients when Discord closed (My issue #42) + let presence: Presence = { + clientId: clientId, + rpc: new Client({ transport: "ipc" }), + ready: false + }; + loggedInPresences.push(presence); + presence.rpc.login({ clientId: clientId }).catch(() => { + //* If couldn't log in remove it from loggedInPresences + //* Reject promise + loggedInPresences = loggedInPresences.filter( + p => p.clientId !== presence.clientId + ); + reject(); + }); + presence.rpc.once("ready", () => { + //* Update status + //* Resolve with presence + presence.ready = true; + resolve(presence); + }); + // @ts-ignore + presence.rpc.once("disconnected", destroy); + }); } /** * Destroys all rpc connections */ export function destroy() { - //* Clear tray title - //* Map through loggedInPresences and destroy their rpcs - //* Set loggedInPresences to new Array - //* Return the promise - if (platform() === "darwin" && typeof tray !== "undefined") tray.setTitle(""); - let res = Promise.all( - loggedInPresences.map((presence: Presence) => - presence.rpc.destroy().catch(() => {}) - ) - ); - loggedInPresences = []; - return res; + //* Clear tray title + //* Map through loggedInPresences and destroy their rpcs + //* Set loggedInPresences to new Array + //* Return the promise + if (platform() === "darwin" && typeof trayManager.tray !== "undefined") + trayManager.tray.setTitle(""); + let res = Promise.all( + loggedInPresences.map((presence: Presence) => + presence.rpc.destroy().catch(() => {}) + ) + ); + loggedInPresences = []; + return res; } //* if app will quit app.once("will-quit", () => { - //* Show debug - //* Destroy all connections - info("Closing rpc connections"); - destroy(); + //* Show debug + //* Destroy all connections + info("Closing rpc connections"); + destroy(); }); diff --git a/src/managers/settingsManager.ts b/src/managers/settingsManager.ts index dd56db0..943ff09 100644 --- a/src/managers/settingsManager.ts +++ b/src/managers/settingsManager.ts @@ -1,17 +1,17 @@ import ElectronStore from "electron-store"; -import { tray } from "./trayManager"; import { update as updateAutoLaunch } from "./launchManager"; import { platform } from "os"; import { info } from "../util/debug"; //* Import custom types import ExtensionSettings from "../../@types/PreMiD/ExtensionSettings"; +import { trayManager } from ".."; //* Export and set default settings export let settings = new ElectronStore({ - defaults: { - autoLaunch: true - } + defaults: { + autoLaunch: true + } }); /** @@ -19,15 +19,15 @@ export let settings = new ElectronStore({ * @param extensionSettings Settings from extension */ export function update(extensionSettings: ExtensionSettings) { - //* Show debug - //* remove title if disabled - //* Update autolaunch if updated - //* Save Settings - info("Updated settings"); - if (!extensionSettings.titleMenubar && platform() === "darwin") - tray.setTitle(""); - if (settings.get("autoLaunch") != extensionSettings.autoLaunch) { - settings.set("autoLaunch", extensionSettings.autoLaunch); - updateAutoLaunch(); - } + //* Show debug + //* remove title if disabled + //* Update autolaunch if updated + //* Save Settings + info("Updated settings"); + if (!extensionSettings.titleMenubar && platform() === "darwin") + trayManager.tray.setTitle(""); + if (settings.get("autoLaunch") != extensionSettings.autoLaunch) { + settings.set("autoLaunch", extensionSettings.autoLaunch); + updateAutoLaunch(); + } } diff --git a/src/managers/trayManager.ts b/src/managers/trayManager.ts index b853a4e..e435540 100644 --- a/src/managers/trayManager.ts +++ b/src/managers/trayManager.ts @@ -1,26 +1,19 @@ import { Menu, Tray } from "electron"; import { join } from "path"; +import { platform } from "os"; -//* Export tray -//* Export trayContextMenu -export let tray: Tray; -export let trayContextMenu = Menu.buildFromTemplate([ - { - role: "quit" - } -]); +export class TrayManager { + tray: Tray; + trayContextMenu: Menu; -/** - * Create tray - */ -export function init() { - //* Return promise resolves to Tray - return new Promise(function(resolve) { - //* Create Tray - //* Set its context menu - //* Resolve promise - tray = new Tray(join(__dirname, "../assets/tray/IconTemplate.png")); - tray.setContextMenu(trayContextMenu); - resolve(tray); - }); + constructor() { + this.trayContextMenu = Menu.buildFromTemplate([ + { + role: "quit" + } + ]); + + this.tray = new Tray(join(__dirname, "../assets/tray/IconTemplate.png")); + this.tray.setContextMenu(this.trayContextMenu); + } } diff --git a/src/util/updateChecker.ts b/src/util/updateChecker.ts index c0069e9..29ae2c1 100644 --- a/src/util/updateChecker.ts +++ b/src/util/updateChecker.ts @@ -1,24 +1,22 @@ import { exec } from "child_process"; import { resolve } from "path"; import { error, info } from "./debug"; -import { trayContextMenu } from "../managers/trayManager"; import { MenuItem, app, dialog } from "electron"; -import { tray } from "../managers/trayManager"; import { platform } from "os"; -import sudoPrompt from "sudo-prompt"; import axios from "axios"; import { Notification } from "electron"; +import { trayManager } from ".."; let updaterPath: string; //* Resolve paths for each OS switch (platform()) { case "darwin": updaterPath = resolve( - `${app.getAppPath()}../../../../../updater.app/Contents/MacOS/installbuilder.sh` + `${app.getAppPath()}../../../../../PreMiD-Updater.app/Contents/MacOS/installbuilder.sh` ); break; case "win32": - updaterPath = resolve(`${app.getAppPath()}../../../updater.exe`); + updaterPath = resolve(`${app.getAppPath()}../../../PreMiD-Updater.exe`); break; } @@ -70,22 +68,15 @@ export function update() { return; } - sudoPrompt.exec( + exec( `\"${updaterPath}\" --mode unattended --unattendedmodebehavior download`, - { - name: app.name, - icns: "assets/appIcon.icns" - }, err => { - // @ts-ignore if (err.message === "User did not grant permission.") { - // @ts-ignore error(err.message); addTray(); } else dialog.showErrorBox( "Error while updating", - // @ts-ignore `${app.name} was unable to update itself. Please try again later.\n\nError: ${err.message}` ); } @@ -93,8 +84,8 @@ export function update() { } function addTray() { - if (trayContextMenu.items.length < 3) { - trayContextMenu.insert( + if (trayManager.trayContextMenu.items.length < 3) { + trayManager.trayContextMenu.insert( 0, new MenuItem({ label: "Update available!", @@ -104,12 +95,12 @@ function addTray() { }) ); - trayContextMenu.insert( + trayManager.trayContextMenu.insert( 1, new MenuItem({ type: "separator" }) ); - tray.setContextMenu(trayContextMenu); + trayManager.tray.setContextMenu(trayManager.trayContextMenu); } } diff --git a/tsconfig.json b/tsconfig.json index aef7a57..fb2f22f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,13 +2,16 @@ "compilerOptions": { "module": "commonjs", "target": "es2018", + "moduleResolution": "node", "inlineSourceMap": true, "outDir": "dist/app", "removeComments": true, - "esModuleInterop": true + "esModuleInterop": true, + "skipLibCheck": true }, "include": ["src/**/*"], "exclude": [ + "**/node_modules/", "pkg.ts", "util/**/*", "installer_assets/builder.ts", diff --git a/yarn.lock b/yarn.lock index 3ad71bc..480a6cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2530,11 +2530,6 @@ strip-json-comments@~2.0.1: resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo= -sudo-prompt@9.1.1: - version "9.1.1" - resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-9.1.1.tgz#73853d729770392caec029e2470db9c221754db0" - integrity sha512-es33J1g2HjMpyAhz8lOR+ICmXXAqTuKbuXuUWLhOLew20oN9oUCgCJx615U/v7aioZg7IX5lIh9x34vwneu4pA== - sumchecker@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/sumchecker/-/sumchecker-3.0.1.tgz#6377e996795abb0b6d348e9b3e1dfb24345a8e42"