This commit is contained in:
萌森 2024-06-28 16:13:57 +08:00
parent 16de4f118e
commit 44a602116d
3 changed files with 48 additions and 21 deletions

20
main.js
View File

@ -1,12 +1,16 @@
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow } = require('electron')
const path = require('node:path') const path = require('node:path')
const server = require('./server/server'); const server = require('./server/server');
const log = require('electron-log')
const createWindow = () => { const createWindow = () => {
const win = new BrowserWindow({ const win = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
webPreferences: { webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js') preload: path.join(__dirname, 'preload.js')
} }
}) })
@ -17,8 +21,13 @@ const createWindow = () => {
} }
app.whenReady().then(() => { app.whenReady().then(() => {
log.transports.file.encoding = 'utf8';
log.transports.file.file = path.join(app.getPath('userData'), 'logs/iyuu.log');
log.transports.file.format = '{y}-{m}-{d} {h}:{i}:{s} {text}';
log.info('App is starting...');
server.startServer() server.startServer()
console.log("[About] 新疆萌森软件开发工作室提供技术支持"); log.info("[About] 新疆萌森软件开发工作室提供技术支持");
setTimeout(() => { setTimeout(() => {
createWindow() createWindow()
@ -32,3 +41,12 @@ app.on('window-all-closed', () => {
server.stopServer() server.stopServer()
if (process.platform !== 'darwin') app.quit() if (process.platform !== 'darwin') app.quit()
}) })
process.on('uncaughtException', (error) => {
log.error('Uncaught Exception:', error);
});
process.on('unhandledRejection', (reason, promise) => {
log.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

View File

@ -4,7 +4,7 @@
"description": "基于特征码的索引工具", "description": "基于特征码的索引工具",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"start": "electron-forge start", "start": "chcp 65001 && electron-forge start",
"package": "electron-forge package", "package": "electron-forge package",
"make": "electron-forge make" "make": "electron-forge make"
}, },
@ -29,6 +29,7 @@
"electron": "^31.1.0" "electron": "^31.1.0"
}, },
"dependencies": { "dependencies": {
"electron-log": "^5.1.5",
"electron-squirrel-startup": "^1.0.1", "electron-squirrel-startup": "^1.0.1",
"tree-kill": "^1.2.2" "tree-kill": "^1.2.2"
} }

View File

@ -1,8 +1,7 @@
const { spawn } = require('child_process'); const { spawn, exec } = require('child_process');
const os = require('os'); const os = require('os');
const fs = require('fs');
const path = require('path'); const path = require('path');
const treeKill = require('tree-kill'); const log = require('electron-log')
function getCmdPath(resourcePath) { function getCmdPath(resourcePath) {
const arch = os.arch(); const arch = os.arch();
@ -11,7 +10,7 @@ function getCmdPath(resourcePath) {
} else if (arch === 'ia32') { } else if (arch === 'ia32') {
return path.join(resourcePath, 'run', 'php-8.3.8-x86', 'php.exe'); return path.join(resourcePath, 'run', 'php-8.3.8-x86', 'php.exe');
} else { } else {
console.error("Unsupported architecture:", arch); log.info("Unsupported architecture:", arch);
process.exit(1); process.exit(1);
} }
} }
@ -36,32 +35,41 @@ function startServer() {
cwd: workingDirectory, cwd: workingDirectory,
stdio: ['inherit', 'pipe', 'inherit'], stdio: ['inherit', 'pipe', 'inherit'],
env: env, env: env,
windowsHide: true detached: true,
}); });
serverProcess.stdout.setEncoding('utf8'); serverProcess.stdout.setEncoding('utf8');
serverProcess.stdout.on("data", function (data) { serverProcess.stdout.on("data", function (data) {
console.log("[IYUU] 服务启动成功"); log.info("[IYUU] 服务启动成功");
}); });
serverProcess.on("close", function (code) { serverProcess.on("close", function (code) {
console.log("[IYUU] 服务退出:" + code); log.info("[IYUU] 服务退出:" + code);
}); });
} }
function stopServer() { function stopServer() {
if (serverProcess) { if (serverProcess) {
console.log("Killing server process with PID:", serverProcess.pid); log.info("Killing server process with PID:", serverProcess.pid);
treeKill(serverProcess.pid, "SIGTERM", function (err) {
if (err) { try {
console.error('Error killing server process:', err); process.kill(-serverProcess.pid, 'SIGTERM');
} else { log.info("后台服务已关闭...");
console.log("后台服务已关闭..."); serverProcess = null;
serverProcess = null; } catch (err) {
} log.error('Error using process.kill:', err);
}); log.info('Falling back to taskkill');
exec(`taskkill /PID ${serverProcess.pid} /T /F`, (err, stdout, stderr) => {
if (err) {
log.error('Error using taskkill:', err);
} else {
log.info("后台服务已关闭...");
serverProcess = null;
}
});
}
} else { } else {
console.log("No server process to kill."); log.info("No server process to kill.");
} }
} }