hydra/src/main/index.ts

124 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-05-03 21:51:13 +03:00
import { app, BrowserWindow, net, protocol } from "electron";
2024-05-14 19:12:19 +03:00
import updater from "electron-updater";
2024-04-21 08:26:29 +03:00
import i18n from "i18next";
import path from "node:path";
2024-06-19 11:48:11 +03:00
import url from "node:url";
2024-04-25 07:52:19 +03:00
import { electronApp, optimizer } from "@electron-toolkit/utils";
2024-06-03 04:12:05 +03:00
import { DownloadManager, logger, WindowManager } from "@main/services";
2024-04-21 08:26:29 +03:00
import { dataSource } from "@main/data-source";
import * as resources from "@locales";
import { userPreferencesRepository } from "@main/repository";
2024-06-16 19:58:24 +03:00
import { HydraApi } from "./services/hydra-api";
2024-06-03 04:12:05 +03:00
2024-05-14 19:12:19 +03:00
const { autoUpdater } = updater;
autoUpdater.setFeedURL({
provider: "github",
owner: "hydralauncher",
repo: "hydra",
});
2024-05-20 19:57:42 +03:00
autoUpdater.logger = logger;
2024-05-19 04:29:11 +03:00
2024-04-21 08:26:29 +03:00
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) app.quit();
app.commandLine.appendSwitch("--no-sandbox");
2024-05-16 00:42:44 +03:00
2024-04-21 08:26:29 +03:00
i18n.init({
resources,
lng: "en",
fallbackLng: "en",
interpolation: {
escapeValue: false,
},
});
const PROTOCOL = "hydralauncher";
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(PROTOCOL, process.execPath, [
path.resolve(process.argv[1]),
]);
}
} else {
app.setAsDefaultProtocolClient(PROTOCOL);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
2024-06-03 04:12:05 +03:00
app.whenReady().then(async () => {
2024-04-25 07:52:19 +03:00
electronApp.setAppUserModelId("site.hydralauncher.hydra");
2024-06-19 11:48:11 +03:00
protocol.handle("local", (request) => {
const filePath = request.url.slice("local://".length);
return net.fetch(url.pathToFileURL(filePath).toString());
});
2024-05-03 21:51:13 +03:00
2024-06-03 04:12:05 +03:00
await dataSource.initialize();
await dataSource.runMigrations();
2024-04-21 08:26:29 +03:00
2024-06-03 04:12:05 +03:00
await import("./main");
2024-04-21 08:26:29 +03:00
2024-06-03 04:12:05 +03:00
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
2024-04-21 08:26:29 +03:00
});
2024-06-03 04:12:05 +03:00
WindowManager.createMainWindow();
WindowManager.createSystemTray(userPreferences?.language || "en");
2024-04-21 08:26:29 +03:00
});
2024-04-25 07:52:19 +03:00
app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window);
});
2024-06-16 23:01:38 +03:00
app.on("second-instance", (_event, commandLine) => {
2024-04-21 08:26:29 +03:00
// Someone tried to run a second instance, we should focus our window.
if (WindowManager.mainWindow) {
if (WindowManager.mainWindow.isMinimized())
WindowManager.mainWindow.restore();
WindowManager.mainWindow.focus();
} else {
WindowManager.createMainWindow();
}
const [, path] = commandLine.pop()?.split("://") ?? [];
2024-06-16 19:58:24 +03:00
if (path) {
if (path.startsWith("auth")) {
2024-06-16 23:01:38 +03:00
HydraApi.handleExternalAuth(path);
2024-06-16 19:58:24 +03:00
} else {
WindowManager.redirect(path);
}
}
2024-04-21 08:26:29 +03:00
});
app.on("open-url", (_event, url) => {
const [, path] = url.split("://");
WindowManager.redirect(path);
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
WindowManager.mainWindow = null;
});
2024-05-30 00:14:07 +03:00
app.on("before-quit", () => {
DownloadManager.disconnect();
});
2024-04-21 08:26:29 +03:00
app.on("activate", () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
WindowManager.createMainWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.