fixed an error where app wouldnt launch

This commit is contained in:
2025-12-17 21:32:34 +01:00
parent 7c85d91b85
commit c7f919fe18
6 changed files with 151 additions and 43 deletions

View File

@@ -1,12 +1,64 @@
const { app, BrowserWindow, ipcMain } = require('electron'); const { app, BrowserWindow, ipcMain } = require('electron');
const { fork } = require('child_process'); const { fork } = require('child_process');
const path = require('path'); const path = require('path');
const log = require('electron-log');
log.transports.file.level = 'info';
log.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}';
let win; let win;
let backend; let backend;
const net = require('net');
function waitForServer(port, host = '127.0.0.1', timeout = 10000) {
return new Promise((resolve, reject) => {
const start = Date.now();
const check = () => {
const socket = new net.Socket();
socket
.once('connect', () => {
socket.destroy();
resolve();
})
.once('error', () => {
socket.destroy();
if (Date.now() - start > timeout) {
reject(new Error('Backend timeout'));
} else {
setTimeout(check, 200);
}
})
.connect(port, host);
};
check();
});
}
function startBackend() { function startBackend() {
backend = fork(path.join(__dirname, 'server.js')); backend = fork(path.join(__dirname, 'server.js'), [], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
env: {
...process.env,
IS_PACKAGED: app.isPackaged ? 'true' : 'false'
}
});
log.info('Starting backend process...');
backend.stdout.on('data', (data) => {
log.info(`[Backend]: ${data.toString().trim()}`);
});
backend.stderr.on('data', (data) => {
log.error(`[Backend ERROR]: ${data.toString().trim()}`);
});
backend.on('exit', (code) => {
log.warn(`Backend process exited with code: ${code}`);
});
} }
function createWindow() { function createWindow() {
@@ -23,19 +75,47 @@ function createWindow() {
}); });
win.setMenu(null); win.setMenu(null);
win.loadURL('http://localhost:54322'); win.loadURL('http://localhost:54322');
win.on('closed', () => {
win = null;
});
} }
ipcMain.on("win:minimize", () => win.minimize()); ipcMain.on("win:minimize", () => win.minimize());
ipcMain.on("win:maximize", () => win.maximize()); ipcMain.on("win:maximize", () => {
if (win.isMaximized()) {
win.unmaximize();
} else {
win.maximize();
}
});
ipcMain.on("win:close", () => win.close()); ipcMain.on("win:close", () => win.close());
app.whenReady().then(() => { process.on('uncaughtException', (err) => {
log.error('Critical unhandled error in Main:', err);
});
app.whenReady().then(async () => {
log.info('--- Application Started ---');
console.log("Logs location:", log.transports.file.getFile().path);
startBackend(); startBackend();
await waitForServer(54322);
createWindow(); createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
}); });
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
if (backend) backend.kill(); log.info('Closing all windows...');
app.quit(); if (backend) {
backend.kill();
log.info('Backend process terminated.');
}
if (process.platform !== 'darwin') {
app.quit();
}
}); });

View File

@@ -11,10 +11,11 @@
"dependencies": { "dependencies": {
"@fastify/static": "^8.3.0", "@fastify/static": "^8.3.0",
"@ryuziii/discord-rpc": "^1.0.1-rc.1", "@ryuziii/discord-rpc": "^1.0.1-rc.1",
"bcrypt": "^6.0.0", "bcryptjs": "^3.0.3",
"bindings": "^1.5.0", "bindings": "^1.5.0",
"cheerio": "^1.1.2", "cheerio": "^1.1.2",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"electron-log": "^5.4.3",
"fastify": "^5.6.2", "fastify": "^5.6.2",
"jsonwebtoken": "^9.0.3", "jsonwebtoken": "^9.0.3",
"node-addon-api": "^8.5.0", "node-addon-api": "^8.5.0",
@@ -1972,18 +1973,13 @@
], ],
"license": "MIT" "license": "MIT"
}, },
"node_modules/bcrypt": { "node_modules/bcryptjs": {
"version": "6.0.0", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-6.0.0.tgz", "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-3.0.3.tgz",
"integrity": "sha512-cU8v/EGSrnH+HnxV2z0J7/blxH8gq7Xh2JFT6Aroax7UohdmiJJlxApMxtKfuI7z68NvvVcmR78k2LbT6efhRg==", "integrity": "sha512-GlF5wPWnSa/X5LKM1o0wz0suXIINz1iHRLvTS+sLyi7XPbe5ycmYI3DlZqVGZZtDgl4DmasFg7gOB3JYbphV5g==",
"hasInstallScript": true, "license": "BSD-3-Clause",
"license": "MIT", "bin": {
"dependencies": { "bcrypt": "bin/bcrypt"
"node-addon-api": "^8.3.0",
"node-gyp-build": "^4.8.4"
},
"engines": {
"node": ">= 18"
} }
}, },
"node_modules/bindings": { "node_modules/bindings": {
@@ -3402,6 +3398,15 @@
"node": ">= 10.0.0" "node": ">= 10.0.0"
} }
}, },
"node_modules/electron-log": {
"version": "5.4.3",
"resolved": "https://registry.npmjs.org/electron-log/-/electron-log-5.4.3.tgz",
"integrity": "sha512-sOUsM3LjZdugatazSQ/XTyNcw8dfvH1SYhXWiJyfYodAAKOZdHs0txPiLDXFzOZbhXgAgshQkshH2ccq0feyLQ==",
"license": "MIT",
"engines": {
"node": ">= 14"
}
},
"node_modules/electron-publish": { "node_modules/electron-publish": {
"version": "26.0.11", "version": "26.0.11",
"resolved": "https://registry.npmjs.org/electron-publish/-/electron-publish-26.0.11.tgz", "resolved": "https://registry.npmjs.org/electron-publish/-/electron-publish-26.0.11.tgz",
@@ -5365,17 +5370,6 @@
"node": "^20.17.0 || >=22.9.0" "node": "^20.17.0 || >=22.9.0"
} }
}, },
"node_modules/node-gyp-build": {
"version": "4.8.4",
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
"integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==",
"license": "MIT",
"bin": {
"node-gyp-build": "bin.js",
"node-gyp-build-optional": "optional.js",
"node-gyp-build-test": "build-test.js"
}
},
"node_modules/node-gyp/node_modules/chownr": { "node_modules/node-gyp/node_modules/chownr": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-3.0.0.tgz", "resolved": "https://registry.npmjs.org/chownr/-/chownr-3.0.0.tgz",

View File

@@ -4,10 +4,8 @@
"description": "", "description": "",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"build": "tsc", "start": "tsc && electron .",
"start": "tsc && node server.js", "dist": "tsc && electron-builder"
"electron": "tsc && electron .",
"dist": "npm run build && electron-builder"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@@ -16,10 +14,11 @@
"dependencies": { "dependencies": {
"@fastify/static": "^8.3.0", "@fastify/static": "^8.3.0",
"@ryuziii/discord-rpc": "^1.0.1-rc.1", "@ryuziii/discord-rpc": "^1.0.1-rc.1",
"bcrypt": "^6.0.0", "bcryptjs": "^3.0.3",
"bindings": "^1.5.0", "bindings": "^1.5.0",
"cheerio": "^1.1.2", "cheerio": "^1.1.2",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"electron-log": "^5.4.3",
"fastify": "^5.6.2", "fastify": "^5.6.2",
"jsonwebtoken": "^9.0.3", "jsonwebtoken": "^9.0.3",
"node-addon-api": "^8.5.0", "node-addon-api": "^8.5.0",
@@ -50,7 +49,11 @@
"public/assets/*" "public/assets/*"
], ],
"extraResources": [ "extraResources": [
"./.env" {
"from": "C:\\Users\\user\\AppData\\Local\\ms-playwright\\chromium_headless_shell-1200",
"to": "playwright/chromium"
},
".env"
], ],
"win": { "win": {
"target": "portable", "target": "portable",

View File

@@ -9,13 +9,14 @@ const { initDatabase } = require("./electron/shared/database");
const { loadExtensions } = require("./electron/shared/extensions"); const { loadExtensions } = require("./electron/shared/extensions");
const { init } = require("./electron/api/rpc/rpc.controller"); const { init } = require("./electron/api/rpc/rpc.controller");
const dotenv = require("dotenv"); const dotenv = require("dotenv");
const envPath = process.resourcesPath
? path.join(process.resourcesPath, ".env")
: path.join(__dirname, ".env");
// Attempt to load it and log the result to be sure const isPackaged = process.env.IS_PACKAGED === "true";
dotenv.config({ path: envPath });
const envPath = isPackaged
? path.join(process.resourcesPath, ".env")
: path.join(__dirname, ".env");
dotenv.config({ path: envPath, override: false });
const viewsRoutes = require("./electron/views/views.routes"); const viewsRoutes = require("./electron/views/views.routes");
const animeRoutes = require("./electron/api/anime/anime.routes"); const animeRoutes = require("./electron/api/anime/anime.routes");
const booksRoutes = require("./electron/api/books/books.routes"); const booksRoutes = require("./electron/api/books/books.routes");

View File

@@ -1,5 +1,5 @@
import {queryAll, queryOne, run} from '../../shared/database'; import {queryAll, queryOne, run} from '../../shared/database';
import bcrypt from 'bcrypt'; import bcrypt from 'bcryptjs';
const USER_DB_NAME = 'userdata'; const USER_DB_NAME = 'userdata';
const SALT_ROUNDS = 10; const SALT_ROUNDS = 10;

View File

@@ -1,14 +1,44 @@
const { chromium } = require("playwright-chromium"); const path = require("path");
const fs = require("fs");
const { chromium } = require("playwright-core");
let browser; let browser;
let context; let context;
const BLOCK_LIST = [ const BLOCK_LIST = [
"google-analytics", "doubleclick", "facebook", "twitter", "google-analytics", "doubleclick", "facebook", "twitter",
"adsystem", "analytics", "tracker", "pixel", "quantserve", "newrelic" "adsystem", "analytics", "tracker", "pixel", "quantserve", "newrelic"
]; ];
function isPackaged() {
return process.env.IS_PACKAGED === "true";
}
function getChromiumPath() {
if (isPackaged()) {
return path.join(
process.resourcesPath,
"playwright",
"chromium",
"chrome-headless-shell-win64",
"chrome-headless-shell.exe"
);
}
return chromium.executablePath();
}
async function initHeadless() { async function initHeadless() {
if (browser) return; if (browser) return;
const exePath = getChromiumPath();
if (!fs.existsSync(exePath)) {
throw new Error("Chromium not found: " + exePath);
}
browser = await chromium.launch({ browser = await chromium.launch({
headless: true, headless: true,
executablePath: exePath,
args: [ args: [
"--no-sandbox", "--no-sandbox",
"--disable-setuid-sandbox", "--disable-setuid-sandbox",