auto-setup.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { execSync } = require('child_process');
  5. const { isDatabaseReady, getDatabaseStats } = require('../utils/database-checker');
  6. // 检查是否需要安装依赖
  7. function needsInstall() {
  8. const nodeModulesPath = path.join(process.cwd(), 'node_modules');
  9. const packageLockPath = path.join(process.cwd(), 'package-lock.json');
  10. if (!fs.existsSync(nodeModulesPath)) {
  11. return true;
  12. }
  13. // 检查package.json是否比package-lock.json新
  14. const packageJsonPath = path.join(process.cwd(), 'package.json');
  15. if (fs.existsSync(packageJsonPath) && fs.existsSync(packageLockPath)) {
  16. const packageStat = fs.statSync(packageJsonPath);
  17. const lockStat = fs.statSync(packageLockPath);
  18. if (packageStat.mtime > lockStat.mtime) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24. // 检查是否需要初始化数据库
  25. async function needsInit() {
  26. const dataDir = path.join(process.cwd(), 'data');
  27. // 如果data目录不存在,需要初始化
  28. if (!fs.existsSync(dataDir)) {
  29. return true;
  30. }
  31. // 使用专门的数据库检查器
  32. const isReady = await isDatabaseReady();
  33. return !isReady;
  34. }
  35. // 执行命令并显示输出
  36. function runCommand(command, description) {
  37. console.log(`\n🔄 ${description}...`);
  38. try {
  39. execSync(command, { stdio: 'inherit', cwd: process.cwd() });
  40. console.log(`✅ ${description}完成`);
  41. return true;
  42. } catch (error) {
  43. console.error(`❌ ${description}失败:`, error.message);
  44. return false;
  45. }
  46. }
  47. async function autoSetup() {
  48. console.log('🚀 HubCmdUI 自动设置检查...\n');
  49. let needsSetup = false;
  50. // 检查是否需要安装依赖
  51. if (needsInstall()) {
  52. console.log('📦 检测到需要安装依赖包');
  53. needsSetup = true;
  54. if (!runCommand('npm install', '安装依赖包')) {
  55. process.exit(1);
  56. }
  57. } else {
  58. console.log('✅ 依赖包已安装');
  59. }
  60. // 检查是否需要初始化
  61. const needsInitialization = await needsInit();
  62. if (needsInitialization) {
  63. console.log('🗄️ 检测到需要初始化数据库');
  64. needsSetup = true;
  65. if (!runCommand('node scripts/init-complete.js', '初始化SQLite数据库')) {
  66. process.exit(1);
  67. }
  68. } else {
  69. console.log('✅ 数据库已初始化');
  70. }
  71. if (needsSetup) {
  72. console.log('\n🎉 系统设置完成!正在启动服务...\n');
  73. } else {
  74. console.log('\n🎯 系统已就绪,正在启动服务...\n');
  75. }
  76. // 启动服务器
  77. console.log('🌐 启动 HubCmdUI 服务器...');
  78. console.log('📍 访问地址: http://localhost:3000');
  79. console.log('🔧 管理面板: http://localhost:3000/admin');
  80. console.log('👤 默认账户: root / admin@123\n');
  81. // 启动主服务器
  82. try {
  83. require('../server.js');
  84. } catch (error) {
  85. console.error('❌ 服务器启动失败:', error.message);
  86. console.error('💡 尝试运行: npm run init 重新初始化');
  87. process.exit(1);
  88. }
  89. }
  90. // 如果直接运行此脚本
  91. if (require.main === module) {
  92. autoSetup().catch(error => {
  93. console.error('❌ 自动设置失败:', error.message);
  94. process.exit(1);
  95. });
  96. }
  97. module.exports = { autoSetup, needsInstall, needsInit };