Files
visitor/update.version.js
T
lzyyj 783364b695 微信端添加版本号;
修补信息泄露漏洞;
2025-12-16 12:54:05 +08:00

131 lines
4.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
function changeVersion() {
// 直接读取.env文件中的版本号
const envPath = path.join(__dirname, '.env');
let currentVersion = '1.0.0'; // 默认版本号
if (fs.existsSync(envPath)) {
const fileContent = fs.readFileSync(envPath, 'utf-8');
const versionMatch = fileContent.match(/VITE_APP_VERSION\s*=\s*([^\r\n]+)/);
if (versionMatch && versionMatch[1]) {
currentVersion = versionMatch[1].trim();
}
}
// 解析版本号
const versionParts = currentVersion.split('.');
if (versionParts.length < 3) {
throw new Error('Invalid version format');
}
// 获取当前环境
const env = process.env.NODE_ENV || 'development';
let newVersion;
const lastPart = versionParts[versionParts.length - 1];
const today = new Date().toISOString().slice(0, 10).replace(/-/g, ''); // YYYYMMDD 格式 (8位)
// 检查最后一组是否以8位日期开头 (支持各种分隔符,如 20250906-01 或 20250906_01)
const dateMatch = lastPart.match(/^(\d{8})[^\d]?(\d*)$/);
if (dateMatch) {
// 最后一组以8位日期开头
const currentDatePart = dateMatch[1];
const sequencePart = dateMatch[2];
if (currentDatePart === today) {
// 日期相同,根据环境递增序号
const currentSequence = sequencePart ? parseInt(sequencePart) : 0;
let newSequence;
if (env === 'production') {
// 生产环境:递增到下一个十位整数
newSequence = Math.ceil((currentSequence + 1) / 10) * 10;
} else if (env === 'test') {
// 测试环境:递增到下一个十位整数,但跳过二十及其倍数
newSequence = getNextTestSequence(currentSequence);
} else {
// 开发环境:递增但避开十位整数
newSequence = getNextDevSequence(currentSequence);
}
// 保持三位数格式,不足三位前面补0
const formattedSequence = newSequence.toString().padStart(2, '0');
versionParts[versionParts.length - 1] = today + '-' + formattedSequence;
} else {
// 日期不同,重置序号
let resetSequence;
if (env === 'production' || env === 'prod') {
resetSequence = '20';
} else if (env === 'test') {
resetSequence = '10';
} else {
resetSequence = '01';
}
versionParts[versionParts.length - 1] = today + '-' + resetSequence;
}
} else {
// 最后一组不是日期格式,检查是否为纯数字
if (/^\d+$/.test(lastPart)) {
// 是纯数字,按照原来的方式递增
versionParts[versionParts.length - 1] = parseInt(lastPart) + 1;
} else {
// 不是纯数字,添加新的日期序号
let initialSequence;
if (env === 'production' || env === 'prod') {
initialSequence = '20';
} else if (env === 'test') {
initialSequence = '10';
} else {
initialSequence = '01';
}
versionParts.push(today + '-' + initialSequence);
}
}
newVersion = versionParts.join('.');
// 修改版本号
if (fs.existsSync(envPath)) {
const file = fs.readFileSync(envPath, 'utf-8');
// 使用正则表达式更可靠地替换版本号
const versionRegex = /VITE_APP_VERSION\s*=\s*.*/;
const newVersionLine = `VITE_APP_VERSION=${newVersion}`;
const result = file.replace(versionRegex, newVersionLine);
// 同步写文件
fs.writeFileSync(envPath, result, 'utf-8');
}
return newVersion;
}
// 获取下一个测试环境序号(十位整数但跳过百位整数)
function getNextTestSequence(current) {
let next = Math.ceil((current + 1) / 10) * 10;
// 跳过百位整数
while (next % 20 === 0) {
next += 10;
}
return next;
}
// 获取下一个开发环境序号(避开十位整数)
function getNextDevSequence(current) {
let next = current + 1;
// 跳过十位整数和百位整数
while (next % 10 === 0) {
next++;
}
return next;
}
module.exports = changeVersion;