minishouyin/test/diagnose.js
2025-11-12 11:35:57 +08:00

173 lines
5.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 诊断脚本 - 检查并解决启动问题和乱码
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
console.log('迷你收银台系统诊断工具');
console.log('====================\n');
// 1. 检查Node.js版本
console.log('1. 检查Node.js版本...');
try {
const nodeVersion = process.version;
const majorVersion = parseInt(nodeVersion.substring(1).split('.')[0]);
console.log(`当前Node.js版本: ${nodeVersion}`);
if (majorVersion < 16) {
console.log('❌ Node.js版本过低建议升级到16.x或更高版本');
console.log('建议从 https://nodejs.org 下载最新LTS版本');
} else {
console.log('✅ Node.js版本满足要求');
}
} catch (error) {
console.log('❌ 无法检测Node.js版本');
}
// 2. 检查项目文件
console.log('\n2. 检查项目文件...');
const requiredFiles = [
'package.json',
'src/main/main.js',
'src/server/app.js',
'src/renderer/App.js',
'src/renderer/pages/Cashier.js'
];
let filesOk = true;
requiredFiles.forEach(file => {
const filePath = path.join(__dirname, file);
if (fs.existsSync(filePath)) {
console.log(`${file}`);
} else {
console.log(`${file} 缺失`);
filesOk = false;
}
});
// 3. 检查依赖
console.log('\n3. 检查依赖...');
const nodeModulesPath = path.join(__dirname, 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
console.log('✅ node_modules存在');
// 检查关键依赖
const keyDependencies = ['electron', 'react', 'antd', 'express'];
keyDependencies.forEach(dep => {
const depPath = path.join(__dirname, 'node_modules', dep);
if (fs.existsSync(depPath)) {
console.log(`${dep} 已安装`);
} else {
console.log(`${dep} 缺失`);
filesOk = false;
}
});
} else {
console.log('❌ node_modules不存在');
filesOk = false;
}
// 4. 检查数据库目录
console.log('\n4. 检查数据库目录...');
const dataPath = path.join(__dirname, 'data');
if (fs.existsSync(dataPath)) {
console.log('✅ 数据目录存在');
try {
fs.accessSync(dataPath, fs.constants.W_OK);
console.log('✅ 数据目录可写');
} catch (error) {
console.log('❌ 数据目录不可写');
console.log('请检查目录权限或以管理员身份运行');
}
} else {
console.log('📁 数据目录不存在,将自动创建');
try {
fs.mkdirSync(dataPath, { recursive: true });
console.log('✅ 数据目录已创建');
} catch (error) {
console.log('❌ 无法创建数据目录');
console.log('请检查目录权限或以管理员身份运行');
}
}
// 5. 检查字符编码
console.log('\n5. 检查字符编码...');
try {
// 读取package.json检查编码
const packageJsonPath = path.join(__dirname, 'package.json');
const packageContent = fs.readFileSync(packageJsonPath, 'utf8');
if (packageContent.includes('<27>') || packageContent.includes('?')) {
console.log('❌ 检测到编码问题package.json可能包含乱码');
} else {
console.log('✅ package.json编码正常');
}
} catch (error) {
console.log('❌ 无法检查文件编码');
}
// 6. 提供解决方案
console.log('\n6. 解决方案...');
if (!filesOk) {
console.log('发现文件或依赖问题,请按以下步骤操作:');
console.log('1. 删除node_modules文件夹');
console.log('2. 运行: npm cache clean --force');
console.log('3. 运行: npm install');
console.log('4. 尝试重新启动应用');
} else {
console.log('文件和依赖检查正常,尝试以下启动方式:');
console.log('1. 运行: node test-start.js (推荐)');
console.log('2. 运行: npm start');
console.log('3. 运行: node smart-start.js');
}
console.log('\n如果仍然遇到乱码问题请尝试');
console.log('1. 设置命令行编码: chcp 65001');
console.log('2. 或者使用PowerShell运行: node test-start.js');
console.log('3. 确保编辑器使用UTF-8编码保存文件');
// 7. 生成修复启动脚本
console.log('\n7. 生成修复启动脚本...');
const fixedStartupScript = `@echo off
chcp 65001 > nul
title 迷你收银台系统
echo 正在启动迷你收银台系统...
echo.
cd /d "%~dp0"
REM 检查node_modules是否存在
if not exist "node_modules" (
echo 首次运行,正在安装依赖...
npm install
if errorlevel 1 (
echo 依赖安装失败请检查网络连接或Node.js是否正确安装
pause
exit /b 1
)
echo 依赖安装完成!
echo.
)
REM 启动应用
echo 正在启动应用...
node test-start.js
if errorlevel 1 (
echo 应用启动失败,请检查错误信息
pause
exit /b 1
)
echo 应用已关闭
pause
`;
const fixedScriptPath = path.join(__dirname, '启动收银台-修复版.bat');
fs.writeFileSync(fixedScriptPath, fixedStartupScript, 'utf8');
console.log(`✅ 已生成修复版启动脚本: ${fixedScriptPath}`);
console.log('\n诊断完成');
console.log('如果问题仍未解决,请查看控制台输出的具体错误信息。');