From 2d0c665ad56468ef8e75d654c1a0ed37ce34b2e5 Mon Sep 17 00:00:00 2001 From: yuantao Date: Mon, 19 Jan 2026 13:10:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E4=B8=8A=E4=BC=A0=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E7=89=88=E6=9C=AC=E5=8F=B7=E8=87=AA=E5=A2=9E=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=E6=A0=B9=E6=8D=AE=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E7=B1=BB=E5=9E=8B=E5=92=8C=E6=95=B0=E9=87=8F=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=B0=83=E6=95=B4=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/upload-weapp.js | 135 ++++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 52 deletions(-) diff --git a/scripts/upload-weapp.js b/scripts/upload-weapp.js index 7a7d25f..31f0587 100644 --- a/scripts/upload-weapp.js +++ b/scripts/upload-weapp.js @@ -50,13 +50,72 @@ function writeManifest(manifest) { } /** - * 自增版本号 + * 分析git提交类型 + * @param {string} gitLog - git日志字符串 + * @returns {Object} - 包含各类型提交数量的对象 + */ +function analyzeCommitTypes(gitLog) { + const commits = gitLog.split('\n').filter(line => line.trim()) + const types = { + major: 0, // 破坏性变更 + minor: 0, // 新增功能 + patch: 0, // 修复bug + other: 0 // 其他类型 + } + + commits.forEach(commit => { + const lowerCommit = commit.toLowerCase() + if (lowerCommit.includes('破坏') || lowerCommit.includes('breaking') || lowerCommit.includes('重构')) { + types.major++ + } else if (lowerCommit.startsWith('新增') || lowerCommit.startsWith('add') || lowerCommit.startsWith('feature')) { + types.minor++ + } else if (lowerCommit.startsWith('修复') || lowerCommit.startsWith('fix') || lowerCommit.startsWith('bug')) { + types.patch++ + } else if (lowerCommit.startsWith('优化') || lowerCommit.startsWith('optimize') || lowerCommit.startsWith('perf')) { + types.patch++ + } else { + types.other++ + } + }) + + return types +} + +/** + * 根据提交类型和数量动态自增版本号 * @param {string} version - 当前版本号 (例如: 1.0.0) + * @param {Object} commitTypes - 提交类型统计对象 * @returns {string} - 自增后的版本号 */ -function incrementVersion(version) { +function incrementVersion(version, commitTypes) { const parts = version.split('.').map(Number) - parts[2]++ // 自增最后一位 + const totalCommits = commitTypes.minor + commitTypes.patch + commitTypes.other + + // 破坏性变更:自增主版本号 + if (commitTypes.major > 0) { + parts[0]++ + parts[1] = 0 + parts[2] = 0 + console.log(' 检测到破坏性变更,升级主版本号') + } + // 大量新增功能(>=3个)或大量提交(>5个):自增次版本号 + else if (commitTypes.minor >= 3 || totalCommits > 5) { + parts[1]++ + parts[2] = 0 + console.log(' 检测到大量提交,升级次版本号') + } + // 有新增功能:自增次版本号 + else if (commitTypes.minor > 0) { + parts[1]++ + parts[2] = 0 + console.log(' 检测到新增功能,升级次版本号') + } + // 修复或优化:自增修订号 + else { + parts[2]++ + console.log(' 检测到修复/优化,升级修订号') + } + return parts.join('.') } @@ -77,7 +136,7 @@ function incrementVersionCode(code) { function getGitLog(count = 2) { try { // 获取最近N条提交的简短日志,使用相对时间 - const log = execSync(`git log -${count} --pretty=format:"%s (%an, %ar)"`, { + const log = execSync(`git log -${count} --pretty=format:"%s"`, { encoding: 'utf-8', cwd: CONFIG.projectPath, }) @@ -88,41 +147,6 @@ function getGitLog(count = 2) { } } -/** - * 将英文相对时间转换为中文 - * @param {string} timeStr - 英文时间字符串 - * @returns {string} - 中文时间字符串 - */ -function translateTimeToChinese(timeStr) { - const translations = [ - ['seconds', '秒'], - ['minutes', '分钟'], - ['hours', '小时'], - ['days', '天'], - ['weeks', '周'], - ['months', '月'], - ['years', '年'], - ['second', '秒'], - ['minute', '分钟'], - ['hour', '小时'], - ['day', '天'], - ['week', '周'], - ['month', '月'], - ['year', '年'], - ['ago', '前'], - ] - - let result = timeStr - for (const [en, zh] of translations) { - result = result.replace(new RegExp(en, 'g'), zh) - } - - // 去除时间单位和"前"之间的多余空格 - result = result.replace(/\s+前/g, '前') - - return result -} - /** * 获取当前git分支名 * @returns {string} - 分支名 @@ -196,24 +220,31 @@ function main() { console.log(' 当前版本:', manifest.versionName) console.log(' 当前版本号:', manifest.versionCode) - // 自增版本号 - console.log('\n2. 自增版本号...') - const newVersion = incrementVersion(manifest.versionName) - const newVersionCode = incrementVersionCode(manifest.versionCode) - console.log(' 新版本:', newVersion) - console.log(' 新版本号:', newVersionCode) - // 获取git信息 - console.log('\n3. 获取git信息...') + console.log('\n2. 获取git信息...') const branch = getGitBranch() const commitHash = getGitCommitHash() const gitLog = getGitLog() console.log(' 分支:', branch) console.log(' 提交:', commitHash) + // 分析提交类型 + console.log('\n3. 分析提交类型...') + const commitTypes = analyzeCommitTypes(gitLog) + console.log(' 新增功能:', commitTypes.minor) + console.log(' 修复优化:', commitTypes.patch) + console.log(' 破坏变更:', commitTypes.major) + console.log(' 其他提交:', commitTypes.other) + + // 自增版本号 + console.log('\n4. 自增版本号...') + const newVersion = incrementVersion(manifest.versionName, commitTypes) + const newVersionCode = incrementVersionCode(manifest.versionCode) + console.log(' 新版本:', newVersion) + console.log(' 新版本号:', newVersionCode) + // 构建上传描述 - const translatedLog = translateTimeToChinese(gitLog) - const logItems = translatedLog.split('\n') + const logItems = gitLog.split('\n') const numberedLogs = logItems.map((item, index) => `(${index + 1}) ${item}`) const uploadDesc = `[${branch}] ${numberedLogs.join(';')}` console.log('\n上传描述:') @@ -222,14 +253,14 @@ function main() { console.log('---') // 更新manifest.json - console.log('\n4. 更新manifest.json...') + console.log('\n5. 更新manifest.json...') manifest.versionName = newVersion manifest.versionCode = String(newVersionCode) writeManifest(manifest) console.log(' 已更新版本号') // 检查编译目录是否存在 - console.log('\n5. 检查编译目录...') + console.log('\n6. 检查编译目录...') if (!fs.existsSync(CONFIG.projectPath)) { console.error('错误: 编译目录不存在!') console.error('请先使用 HBuilderX 编译项目') @@ -239,7 +270,7 @@ function main() { console.log(' 编译目录存在') // 上传到微信 - console.log('\n6. 上传到微信...') + console.log('\n7. 上传到微信...') uploadToWechat(newVersion, uploadDesc) console.log('\n=== 上传完成 ===')