#!/usr/bin/env node /** * 验证前端环境设置 * * 检查: * - Node.js版本 * - 依赖包安装 * - TypeScript配置 * - 构建配置 */ import { execSync } from 'child_process'; import { existsSync, readFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); function checkNodeVersion() { console.log('\n检查Node.js版本:'); try { const version = process.version; const majorVersion = parseInt(version.slice(1).split('.')[0]); console.log(` ✓ Node.js版本: ${version}`); if (majorVersion < 18) { console.log(' ❌ 需要Node.js 18或更高版本'); return false; } console.log(' ✓ Node.js版本符合要求'); return true; } catch (error) { console.log(` ❌ Node.js检查失败: ${error.message}`); return false; } } function checkPackageManager() { console.log('\n检查包管理器:'); try { const npmVersion = execSync('npm --version', { encoding: 'utf8' }).trim(); console.log(` ✓ npm版本: ${npmVersion}`); try { const yarnVersion = execSync('yarn --version', { encoding: 'utf8' }).trim(); console.log(` ✓ yarn版本: ${yarnVersion}`); } catch { console.log(' ⚠️ yarn未安装 (可选)'); } return true; } catch (error) { console.log(` ❌ 包管理器检查失败: ${error.message}`); return false; } } function checkDependencies() { console.log('\n检查依赖包:'); const nodeModulesPath = join(__dirname, 'node_modules'); if (!existsSync(nodeModulesPath)) { console.log(' ❌ node_modules目录不存在'); console.log(' 请运行: yarn install 或 npm install'); return false; } console.log(' ✓ node_modules目录存在'); // 检查关键依赖 const criticalDeps = [ 'react', 'react-dom', 'vite', 'typescript', 'antd', '@ant-design/icons', 'react-router-dom', 'axios' ]; let allDepsOk = true; for (const dep of criticalDeps) { const depPath = join(nodeModulesPath, dep); if (existsSync(depPath)) { try { const packageJson = JSON.parse( readFileSync(join(depPath, 'package.json'), 'utf8') ); console.log(` ✓ ${dep}: ${packageJson.version}`); } catch { console.log(` ✓ ${dep}: 已安装`); } } else { console.log(` ❌ ${dep}: 未安装`); allDepsOk = false; } } return allDepsOk; } function checkConfiguration() { console.log('\n检查配置文件:'); const configFiles = [ 'package.json', 'vite.config.ts', 'tsconfig.json', 'index.html' ]; let allConfigsOk = true; for (const file of configFiles) { const filePath = join(__dirname, file); if (existsSync(filePath)) { console.log(` ✓ ${file}`); } else { console.log(` ❌ ${file} 不存在`); allConfigsOk = false; } } return allConfigsOk; } function checkTypeScript() { console.log('\n检查TypeScript:'); try { execSync('npx tsc --noEmit', { encoding: 'utf8', stdio: 'pipe' }); console.log(' ✓ TypeScript编译检查通过'); return true; } catch (error) { console.log(' ❌ TypeScript编译检查失败'); console.log(` 错误: ${error.message}`); return false; } } function checkBuild() { console.log('\n检查构建配置:'); try { // 只检查配置,不实际构建 execSync('npx vite build --dry-run', { encoding: 'utf8', stdio: 'pipe' }); console.log(' ✓ Vite构建配置正确'); return true; } catch (error) { // dry-run可能不被支持,尝试其他方式 try { execSync('npx vite --version', { encoding: 'utf8', stdio: 'pipe' }); console.log(' ✓ Vite配置可用'); return true; } catch { console.log(' ❌ Vite构建配置检查失败'); return false; } } } function main() { console.log('='.repeat(50)); console.log('Frontend 环境验证'); console.log('='.repeat(50)); const checks = [ { name: 'Node.js版本', fn: checkNodeVersion }, { name: '包管理器', fn: checkPackageManager }, { name: '依赖包', fn: checkDependencies }, { name: '配置文件', fn: checkConfiguration }, { name: 'TypeScript', fn: checkTypeScript }, { name: '构建配置', fn: checkBuild } ]; const results = []; for (const { name, fn } of checks) { try { const result = fn(); results.push({ name, result }); } catch (error) { console.log(` ❌ ${name}检查失败: ${error.message}`); results.push({ name, result: false }); } } console.log('\n' + '='.repeat(50)); console.log('验证结果:'); console.log('='.repeat(50)); let allPassed = true; for (const { name, result } of results) { const status = result ? '✓ 通过' : '❌ 失败'; console.log(` ${name}: ${status}`); if (!result) allPassed = false; } if (allPassed) { console.log('\n🎉 所有检查通过! 前端环境设置正确。'); console.log('\n可以开始开发:'); console.log(' yarn dev - 启动开发服务器'); console.log(' yarn build - 构建生产版本'); console.log(' yarn test - 运行测试'); console.log(' yarn lint - 代码检查'); console.log('\n访问: http://localhost:3000'); } else { console.log('\n⚠️ 部分检查失败,请检查上述问题。'); console.log('\n常见解决方案:'); console.log(' yarn install - 重新安装依赖'); console.log(' yarn cache clean - 清理缓存'); console.log(' rm -rf node_modules && yarn install - 完全重装'); } console.log('='.repeat(50)); } main();