48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
|
|
const fs = require('fs');
|
|||
|
|
const path = require('path');
|
|||
|
|
|
|||
|
|
// 生成版本号(格式:YYYYMMDD-HHmmss)
|
|||
|
|
function generateVersion() {
|
|||
|
|
const now = new Date();
|
|||
|
|
const year = now.getFullYear();
|
|||
|
|
const month = String(now.getMonth() + 1).padStart(2, '0');
|
|||
|
|
const day = String(now.getDate()).padStart(2, '0');
|
|||
|
|
const hours = String(now.getHours()).padStart(2, '0');
|
|||
|
|
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|||
|
|
const seconds = String(now.getSeconds()).padStart(2, '0');
|
|||
|
|
return `${year}${month}${day}-${hours}${minutes}${seconds}`;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 更新 index.html 中的版本号
|
|||
|
|
function updateVersion() {
|
|||
|
|
const htmlPath = path.join(__dirname, '../index.html');
|
|||
|
|
|
|||
|
|
if (!fs.existsSync(htmlPath)) {
|
|||
|
|
console.error('❌ index.html 文件不存在:', htmlPath);
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
let html = fs.readFileSync(htmlPath, 'utf8');
|
|||
|
|
const version = generateVersion();
|
|||
|
|
|
|||
|
|
// 替换版本号
|
|||
|
|
if (html.includes('<meta name="version"')) {
|
|||
|
|
html = html.replace(
|
|||
|
|
/<meta name="version" content="[^"]*">/,
|
|||
|
|
`<meta name="version" content="${version}">`
|
|||
|
|
);
|
|||
|
|
} else {
|
|||
|
|
// 如果不存在版本号 meta 标签,在 charset 后面添加
|
|||
|
|
html = html.replace(
|
|||
|
|
/<meta charset="UTF-8"\/>/,
|
|||
|
|
`<meta charset="UTF-8"/>\n <!-- 版本号:每次发布时自动更新 -->\n <meta name="version" content="${version}">`
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fs.writeFileSync(htmlPath, html, 'utf8');
|
|||
|
|
console.log(`✅ 版本号已更新为: ${version}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
updateVersion();
|
|||
|
|
|