start-diagnostic.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * 诊断启动脚本 - 运行诊断并安全启动服务器
  3. */
  4. const { spawn } = require('child_process');
  5. const path = require('path');
  6. const fs = require('fs');
  7. const { runDiagnostics } = require('./scripts/diagnostics');
  8. // 确保必要的模块存在
  9. try {
  10. require('./logger');
  11. } catch (error) {
  12. console.error('无法加载logger模块,请确保该模块存在:', error.message);
  13. process.exit(1);
  14. }
  15. const logger = require('./logger');
  16. async function startWithDiagnostics() {
  17. logger.info('正在运行系统诊断...');
  18. try {
  19. // 运行诊断
  20. const { criticalErrors } = await runDiagnostics();
  21. if (criticalErrors.length > 0) {
  22. logger.error('发现严重问题,无法启动系统。请修复问题后重试。');
  23. process.exit(1);
  24. }
  25. logger.success('诊断通过,正在启动系统...');
  26. // 启动服务器
  27. const serverProcess = spawn('node', ['server.js'], {
  28. stdio: 'inherit',
  29. cwd: __dirname
  30. });
  31. serverProcess.on('close', (code) => {
  32. if (code !== 0) {
  33. logger.error(`服务器进程异常退出,退出码: ${code}`);
  34. process.exit(code);
  35. }
  36. });
  37. serverProcess.on('error', (err) => {
  38. logger.error('启动服务器进程时出错:', err);
  39. process.exit(1);
  40. });
  41. } catch (error) {
  42. logger.fatal('诊断过程中发生错误:', error);
  43. process.exit(1);
  44. }
  45. }
  46. // 启动服务
  47. startWithDiagnostics();