fix(electron): 更新开发模式支持和控制台编码设置
- 在主进程中添加对Windows控制台UTF-8编码的支持 - 优化开发模式检测逻辑,确保在开发环境下正确打开开发者工具 - 改进后端进程的启动逻辑,确保在启动失败时提供详细错误信息
This commit is contained in:
94
app/main.js
94
app/main.js
@@ -4,6 +4,24 @@ const { spawn } = require('child_process');
|
||||
const dotenv = require('dotenv');
|
||||
const fs = require('fs');
|
||||
|
||||
// 设置控制台输出编码为 UTF-8(Windows 兼容)
|
||||
if (process.platform === 'win32') {
|
||||
// 设置 stdout 和 stderr 的编码
|
||||
if (process.stdout.setDefaultEncoding) {
|
||||
process.stdout.setDefaultEncoding('utf8');
|
||||
}
|
||||
if (process.stderr.setDefaultEncoding) {
|
||||
process.stderr.setDefaultEncoding('utf8');
|
||||
}
|
||||
|
||||
// 尝试设置控制台代码页为 UTF-8
|
||||
try {
|
||||
require('child_process').exec('chcp 65001', { encoding: 'utf8' });
|
||||
} catch (e) {
|
||||
// 忽略错误
|
||||
}
|
||||
}
|
||||
|
||||
// 加载环境变量
|
||||
const envPath = path.join(__dirname, '..', '.env');
|
||||
if (fs.existsSync(envPath)) {
|
||||
@@ -13,6 +31,17 @@ if (fs.existsSync(envPath)) {
|
||||
let mainWindow;
|
||||
let backendProcess;
|
||||
|
||||
// 判断是否为开发模式
|
||||
// 主要依赖 app.isPackaged,如果为 false 则是开发环境
|
||||
// 或者检查路径是否包含 app.asar(打包后的应用)
|
||||
const isDevelopment = !app.isPackaged || process.env.NODE_ENV === 'development';
|
||||
|
||||
console.log('运行模式检测:');
|
||||
console.log(' - app.isPackaged:', app.isPackaged);
|
||||
console.log(' - process.resourcesPath:', process.resourcesPath);
|
||||
console.log(' - process.env.NODE_ENV:', process.env.NODE_ENV);
|
||||
console.log(' - isDevelopment:', isDevelopment);
|
||||
|
||||
/**
|
||||
* 创建Electron主窗口
|
||||
*/
|
||||
@@ -34,7 +63,8 @@ function createWindow() {
|
||||
mainWindow.loadFile(indexPath);
|
||||
|
||||
// 开发环境下打开开发者工具
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
if (isDevelopment || process.env.NODE_ENV === 'development') {
|
||||
console.log('开发模式:打开开发者工具');
|
||||
mainWindow.webContents.openDevTools();
|
||||
|
||||
// 过滤掉 DevTools 的 Autofill 相关错误
|
||||
@@ -101,11 +131,11 @@ function waitForBackend(port = 3000, maxRetries = 30, interval = 1000) {
|
||||
async function startBackend() {
|
||||
let backendPath;
|
||||
|
||||
if (process.resourcesPath) {
|
||||
if (app.isPackaged) {
|
||||
// 生产环境:使用 app.asar.unpacked 中的文件
|
||||
backendPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'dist', 'main.js');
|
||||
} else {
|
||||
// 开发环境
|
||||
// 开发环境:使用项目根目录下的 dist 文件夹
|
||||
backendPath = path.join(__dirname, '..', 'dist', 'main.js');
|
||||
}
|
||||
|
||||
@@ -122,25 +152,61 @@ async function startBackend() {
|
||||
backendProcess = spawn('node', [backendPath], {
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: process.env.NODE_ENV || 'production',
|
||||
NODE_ENV: isDevelopment ? 'development' : (process.env.NODE_ENV || 'production'),
|
||||
// 设置编码环境变量(Windows)
|
||||
...(process.platform === 'win32' && {
|
||||
PYTHONIOENCODING: 'utf-8',
|
||||
LANG: 'en_US.UTF-8'
|
||||
}),
|
||||
},
|
||||
stdio: 'pipe',
|
||||
// Windows 上设置 shell 选项以确保编码正确
|
||||
...(process.platform === 'win32' && { shell: false }),
|
||||
});
|
||||
|
||||
// 捕获并显示后端进程的输出
|
||||
backendProcess.stdout.on('data', (data) => {
|
||||
// 确保正确解码 UTF-8 编码的数据
|
||||
const output = Buffer.isBuffer(data) ? data.toString('utf8') : data.toString();
|
||||
console.log(`[后端输出] ${output}`);
|
||||
});
|
||||
|
||||
backendProcess.stderr.on('data', (data) => {
|
||||
// 确保正确解码 UTF-8 编码的数据
|
||||
const output = Buffer.isBuffer(data) ? data.toString('utf8') : data.toString();
|
||||
console.error(`[后端错误] ${output}`);
|
||||
});
|
||||
|
||||
backendProcess.on('error', (error) => {
|
||||
console.error('后端服务启动失败:', error);
|
||||
});
|
||||
|
||||
backendProcess.on('exit', (code) => {
|
||||
console.log(`后端服务退出,退出码: ${code}`);
|
||||
let backendExited = false;
|
||||
backendProcess.on('exit', (code, signal) => {
|
||||
backendExited = true;
|
||||
if (code !== 0 && code !== null) {
|
||||
console.error(`后端服务异常退出,退出码: ${code}, 信号: ${signal}`);
|
||||
} else {
|
||||
console.log(`后端服务退出,退出码: ${code}`);
|
||||
}
|
||||
backendProcess = null;
|
||||
});
|
||||
|
||||
// 等待后端服务启动完成
|
||||
try {
|
||||
await waitForBackend();
|
||||
// 检查后端是否在等待期间就退出了
|
||||
if (backendExited) {
|
||||
throw new Error('后端服务在启动过程中退出');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('等待后端服务启动失败:', error.message);
|
||||
if (backendProcess) {
|
||||
console.error('正在停止后端进程...');
|
||||
backendProcess.kill();
|
||||
backendProcess = null;
|
||||
}
|
||||
throw error; // 重新抛出错误,让调用者知道启动失败
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,9 +223,19 @@ function stopBackend() {
|
||||
|
||||
// 应用就绪时启动后端服务,然后创建窗口
|
||||
app.on('ready', async () => {
|
||||
await startBackend();
|
||||
createWindow();
|
||||
Menu.setApplicationMenu(null);
|
||||
try {
|
||||
await startBackend();
|
||||
createWindow();
|
||||
Menu.setApplicationMenu(null);
|
||||
} catch (error) {
|
||||
console.error('应用启动失败:', error);
|
||||
// 显示错误对话框
|
||||
const { dialog } = require('electron');
|
||||
dialog.showErrorBox(
|
||||
'启动失败',
|
||||
`后端服务启动失败: ${error.message}\n\n请检查控制台输出以获取更多信息。`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// 所有窗口关闭时退出应用
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
"ai-recommendations": "ts-node -r tsconfig-paths/register src/scripts/ai-recommendations.ts",
|
||||
"sync": "ts-node -r tsconfig-paths/register src/scripts/sync.ts",
|
||||
"deploy": "powershell -ExecutionPolicy Bypass -File src/scripts/deploy.ps1",
|
||||
"electron:dev": "npm run -prefix frontend build && npm run build && set NODE_ENV=development&& electron ./app",
|
||||
"electron:dev": "chcp 65001 >nul 2>&1 & npm run -prefix frontend build && npm run build && set NODE_ENV=development && electron ./app",
|
||||
"electron:build": "npm run -prefix frontend build && npm run build && electron-builder --config ./app/electron-builder.json"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
Reference in New Issue
Block a user