From 9c99e56b70438ae693dca13e47aba9b978c893eb Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:50:18 -0300 Subject: [PATCH] fix: add script to auto launch hydra on startup --- resources/hydra.vbs | 3 ++ src/main/constants.ts | 9 ++++++ .../events/user-preferences/auto-launch.ts | 29 +++++++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 resources/hydra.vbs diff --git a/resources/hydra.vbs b/resources/hydra.vbs new file mode 100644 index 00000000..191bddad --- /dev/null +++ b/resources/hydra.vbs @@ -0,0 +1,3 @@ +Set WshShell = CreateObject("WScript.Shell" ) +WshShell.Run """%localappdata%\Programs\Hydra\Hydra.exe""", 0 'Must quote command if it has spaces; must escape quotes +Set WshShell = Nothing \ No newline at end of file diff --git a/src/main/constants.ts b/src/main/constants.ts index 850c9ada..62713a2c 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -14,3 +14,12 @@ export const logsPath = path.join(app.getPath("appData"), "hydra", "logs"); export const seedsPath = app.isPackaged ? path.join(process.resourcesPath, "seeds") : path.join(__dirname, "..", "..", "seeds"); + +export const windowsStartupPath = path.join( + app.getPath("appData"), + "Microsoft", + "Windows", + "Start Menu", + "Programs", + "Startup" +); diff --git a/src/main/events/user-preferences/auto-launch.ts b/src/main/events/user-preferences/auto-launch.ts index 3292d2a6..20482b68 100644 --- a/src/main/events/user-preferences/auto-launch.ts +++ b/src/main/events/user-preferences/auto-launch.ts @@ -1,18 +1,35 @@ +import { windowsStartupPath } from "@main/constants"; import { registerEvent } from "../register-event"; import AutoLaunch from "auto-launch"; import { app } from "electron"; +import fs from "node:fs"; +import path from "node:path"; const autoLaunch = async ( _event: Electron.IpcMainInvokeEvent, enabled: boolean ) => { - const appLauncher = new AutoLaunch({ - name: app.getName(), - }); - if (enabled) { - appLauncher.enable().catch(); + if (!app.isPackaged) return; + + if (process.platform == "win32") { + const destination = path.join(windowsStartupPath, "hydra.vbs"); + + if (enabled) { + const scriptPath = path.join(process.resourcesPath, "hydra.vbs"); + + fs.copyFileSync(scriptPath, destination); + } else { + fs.rmSync(destination); + } } else { - appLauncher.disable().catch(); + const appLauncher = new AutoLaunch({ + name: app.getName(), + }); + if (enabled) { + appLauncher.enable().catch(); + } else { + appLauncher.disable().catch(); + } } };