84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 智能启动脚本 - 自动适配项目结构
|
||
|
|
*/
|
||
|
|
|
||
|
|
const { spawn } = require('child_process');
|
||
|
|
const path = require('path');
|
||
|
|
const fs = require('fs');
|
||
|
|
|
||
|
|
// 检查当前目录是否有index.html
|
||
|
|
const currentDir = process.cwd();
|
||
|
|
const indexPath = path.join(currentDir, 'index.html');
|
||
|
|
const publicIndexPath = path.join(currentDir, 'public', 'index.html');
|
||
|
|
|
||
|
|
let serverRoot = '.';
|
||
|
|
let openFile = '';
|
||
|
|
|
||
|
|
// 智能检测项目根目录
|
||
|
|
if (fs.existsSync(indexPath)) {
|
||
|
|
console.log('✅ 检测到 index.html 在当前目录');
|
||
|
|
serverRoot = '.';
|
||
|
|
openFile = '/';
|
||
|
|
} else if (fs.existsSync(publicIndexPath)) {
|
||
|
|
console.log('⚠️ index.html 在 public 目录,调整服务器根目录...');
|
||
|
|
serverRoot = 'public';
|
||
|
|
openFile = '/';
|
||
|
|
} else {
|
||
|
|
console.error('❌ 未找到 index.html 文件');
|
||
|
|
console.error('请确保在正确的项目目录中运行此脚本');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 构建命令参数
|
||
|
|
const args = [
|
||
|
|
'http-server',
|
||
|
|
serverRoot,
|
||
|
|
'-p', '5175',
|
||
|
|
'-a', '127.0.0.1'
|
||
|
|
];
|
||
|
|
|
||
|
|
// 如果需要自动打开浏览器
|
||
|
|
if (process.argv.includes('--open') || process.argv.includes('-o')) {
|
||
|
|
args.push('-o');
|
||
|
|
if (openFile) {
|
||
|
|
args.push(openFile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('');
|
||
|
|
console.log('======================================');
|
||
|
|
console.log('🚀 AI Agent 工作流 - 启动服务器');
|
||
|
|
console.log('======================================');
|
||
|
|
console.log('');
|
||
|
|
console.log(`📁 服务器根目录: ${serverRoot === '.' ? '当前目录' : serverRoot}`);
|
||
|
|
console.log(`🌐 访问地址: http://127.0.0.1:5175${openFile}`);
|
||
|
|
console.log('');
|
||
|
|
console.log('按 Ctrl+C 停止服务器');
|
||
|
|
console.log('');
|
||
|
|
|
||
|
|
// 启动服务器
|
||
|
|
const server = spawn('npx', args, {
|
||
|
|
stdio: 'inherit',
|
||
|
|
shell: true
|
||
|
|
});
|
||
|
|
|
||
|
|
// 处理进程退出
|
||
|
|
server.on('error', (err) => {
|
||
|
|
console.error('❌ 启动失败:', err.message);
|
||
|
|
process.exit(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
server.on('exit', (code) => {
|
||
|
|
if (code !== 0 && code !== null) {
|
||
|
|
console.error(`❌ 服务器退出,代码: ${code}`);
|
||
|
|
}
|
||
|
|
process.exit(code || 0);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 处理 Ctrl+C
|
||
|
|
process.on('SIGINT', () => {
|
||
|
|
console.log('\n正在停止服务器...');
|
||
|
|
server.kill('SIGINT');
|
||
|
|
});
|