Files
aitu/scripts/update-version.js
gongchengtu 51bb033edb feat: 版本日志菜单 + CHANGELOG.md 自动同步到 changelog.json
- 新增 scripts/sync-changelog.js:解析 CHANGELOG.md 生成 changelog.json,
  自动过滤噪音条目(bump version、docs、refactor 等内部描述)
- safe-version-bump.js:version:patch 时自动调用 sync-changelog 并提交 changelog.json
- versions.html:简化为纯 changelog 展示页,去掉版本跳转和 CDN 链接,
  默认显示 30 个版本 + 加载更多按钮,展开详情 max-height 调至 5000px
- app-menu-items.tsx:新增「版本日志」菜单项(打开 versions.html),
  「版本更多」恢复跳转到 release.opentu.ai
- update-version.js:构建时检查 changelog.json 是否包含当前版本
- upload-and-deploy.js:部署时复制 versions.html 和 changelog.json 到 releases 根目录
- i18n.tsx:新增 menu.changelog 翻译 key
2026-02-26 15:01:02 +08:00

111 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const fs = require('fs');
const path = require('path');
// 获取当前版本号
function getCurrentVersion() {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
return packageJson.version;
}
// 更新 Service Worker 中的版本号(已废弃,sw.js 现在是构建产物)
function updateServiceWorkerVersion(version) {
// sw.js 现在是构建产物,不再需要手动更新
// 版本信息会通过 version.json 和构建过程自动处理
console.log(`ℹ️ Service Worker 版本将通过构建过程自动更新`);
}
// 创建版本信息文件(保留现有的 changelog)
function createVersionFile(version) {
const versionPath = path.join(__dirname, '../apps/web/public/version.json');
// 读取现有的 version.json,保留 changelog
let existingChangelog = [];
if (fs.existsSync(versionPath)) {
try {
const existing = JSON.parse(fs.readFileSync(versionPath, 'utf8'));
// 只有当版本号相同时才保留 changelog
if (existing.version === version && Array.isArray(existing.changelog)) {
existingChangelog = existing.changelog;
}
} catch (e) {
// 忽略解析错误
}
}
const versionInfo = {
version: version,
buildTime: new Date().toISOString(),
gitCommit: process.env.GITHUB_SHA || 'unknown',
changelog: existingChangelog
};
fs.writeFileSync(versionPath, JSON.stringify(versionInfo, null, 2));
console.log(`✅ Version file created: ${version}${existingChangelog.length > 0 ? ` (保留 ${existingChangelog.length} 条更新日志)` : ''}`);
}
// 更新 HTML 文件,添加版本号到资源链接
function updateHtmlWithVersion(version) {
const htmlPath = path.join(__dirname, '../apps/web/index.html');
let htmlContent = fs.readFileSync(htmlPath, 'utf8');
// 在 manifest.json 后添加版本号查询参数
htmlContent = htmlContent.replace(
'href="/manifest.json"',
`href="/manifest.json?v=${version}"`
);
// 更新或添加版本信息到 meta 标签
if (htmlContent.includes('name="app-version"')) {
// 更新现有的版本标签
htmlContent = htmlContent.replace(
/<meta name="app-version" content="[^"]*" \/>/g,
`<meta name="app-version" content="${version}" />`
);
} else {
// 添加新的版本标签
const versionMeta = ` <meta name="app-version" content="${version}" />`;
htmlContent = htmlContent.replace(
' <meta name="viewport" content="width=device-width, initial-scale=1" />',
` <meta name="viewport" content="width=device-width, initial-scale=1" />\n${versionMeta}`
);
}
fs.writeFileSync(htmlPath, htmlContent);
console.log(`✅ HTML updated with version ${version}`);
}
// 检查 changelog.json 是否包含当前版本
function checkChangelog(version) {
const changelogPath = path.join(__dirname, '../apps/web/public/changelog.json');
if (!fs.existsSync(changelogPath)) {
console.log(`⚠️ changelog.json not found, skipping changelog check`);
return;
}
try {
const changelog = JSON.parse(fs.readFileSync(changelogPath, 'utf8'));
const hasEntry = (changelog.versions || []).some(v => v.version === version);
if (!hasEntry) {
console.log(`⚠️ Version ${version} has no entry in changelog.json — consider adding one`);
} else {
console.log(`✅ changelog.json has entry for ${version}`);
}
} catch (e) {
console.log(`⚠️ Failed to parse changelog.json: ${e.message}`);
}
}
// 主函数
function main() {
const version = getCurrentVersion();
console.log(`🚀 Updating app to version ${version}`);
// updateServiceWorkerVersion(version);
createVersionFile(version);
updateHtmlWithVersion(version);
checkChangelog(version);
console.log(`🎉 Version update completed: ${version}`);
}
main();