// scripts/generate-version.js import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs' import { resolve, dirname } from 'path' import { fileURLToPath } from 'url' // 获取当前目录 const __filename = fileURLToPath(import.meta.url) const __dirname = dirname(__filename) // 读取 .env 文件获取版本号 const envPath = resolve(__dirname, '../.env') let version = '1.0.0' let versionType = 'patch' try { if (existsSync(envPath)) { const envContent = readFileSync(envPath, 'utf-8') const versionMatch = envContent.match(/VITE_APP_VERSION=([^\r\n]+)/) const typeMatch = envContent.match(/VITE_VERSION_TYPE=([^\r\n]+)/) if (versionMatch && versionMatch[1]) { version = versionMatch[1].replace(/['"]/g, '') } if (typeMatch && typeMatch[1]) { versionType = typeMatch[1].replace(/['"]/g, '') } } } catch (error) { console.warn('无法读取 .env 文件,使用默认版本号:', error.message) } // 生成版本信息 const versionInfo = { version: version, type: versionType, // 新增版本类型 buildTime: new Date().toISOString(), timestamp: Date.now(), } // 确保 dist 目录存在 const distDir = resolve(__dirname, '../dist') if (!existsSync(distDir)) { mkdirSync(distDir, { recursive: true }) } // 写入 version.json const outputPath = resolve(distDir, 'version.json') writeFileSync(outputPath, JSON.stringify(versionInfo, null, 2)) console.log( `✅ 版本文件已生成: ${versionInfo.version}-${versionInfo.type} (${versionInfo.buildTime})` )