You've already forked template-MP
优化 微信小程序上传脚本的版本号自增逻辑,支持根据提交类型和数量动态调整版本号
This commit is contained in:
@@ -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 {string} version - 当前版本号 (例如: 1.0.0)
|
||||||
|
* @param {Object} commitTypes - 提交类型统计对象
|
||||||
* @returns {string} - 自增后的版本号
|
* @returns {string} - 自增后的版本号
|
||||||
*/
|
*/
|
||||||
function incrementVersion(version) {
|
function incrementVersion(version, commitTypes) {
|
||||||
const parts = version.split('.').map(Number)
|
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('.')
|
return parts.join('.')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +136,7 @@ function incrementVersionCode(code) {
|
|||||||
function getGitLog(count = 2) {
|
function getGitLog(count = 2) {
|
||||||
try {
|
try {
|
||||||
// 获取最近N条提交的简短日志,使用相对时间
|
// 获取最近N条提交的简短日志,使用相对时间
|
||||||
const log = execSync(`git log -${count} --pretty=format:"%s (%an, %ar)"`, {
|
const log = execSync(`git log -${count} --pretty=format:"%s"`, {
|
||||||
encoding: 'utf-8',
|
encoding: 'utf-8',
|
||||||
cwd: CONFIG.projectPath,
|
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分支名
|
* 获取当前git分支名
|
||||||
* @returns {string} - 分支名
|
* @returns {string} - 分支名
|
||||||
@@ -196,24 +220,31 @@ function main() {
|
|||||||
console.log(' 当前版本:', manifest.versionName)
|
console.log(' 当前版本:', manifest.versionName)
|
||||||
console.log(' 当前版本号:', manifest.versionCode)
|
console.log(' 当前版本号:', manifest.versionCode)
|
||||||
|
|
||||||
// 自增版本号
|
|
||||||
console.log('\n2. 自增版本号...')
|
|
||||||
const newVersion = incrementVersion(manifest.versionName)
|
|
||||||
const newVersionCode = incrementVersionCode(manifest.versionCode)
|
|
||||||
console.log(' 新版本:', newVersion)
|
|
||||||
console.log(' 新版本号:', newVersionCode)
|
|
||||||
|
|
||||||
// 获取git信息
|
// 获取git信息
|
||||||
console.log('\n3. 获取git信息...')
|
console.log('\n2. 获取git信息...')
|
||||||
const branch = getGitBranch()
|
const branch = getGitBranch()
|
||||||
const commitHash = getGitCommitHash()
|
const commitHash = getGitCommitHash()
|
||||||
const gitLog = getGitLog()
|
const gitLog = getGitLog()
|
||||||
console.log(' 分支:', branch)
|
console.log(' 分支:', branch)
|
||||||
console.log(' 提交:', commitHash)
|
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 = gitLog.split('\n')
|
||||||
const logItems = translatedLog.split('\n')
|
|
||||||
const numberedLogs = logItems.map((item, index) => `(${index + 1}) ${item}`)
|
const numberedLogs = logItems.map((item, index) => `(${index + 1}) ${item}`)
|
||||||
const uploadDesc = `[${branch}] ${numberedLogs.join(';')}`
|
const uploadDesc = `[${branch}] ${numberedLogs.join(';')}`
|
||||||
console.log('\n上传描述:')
|
console.log('\n上传描述:')
|
||||||
@@ -222,14 +253,14 @@ function main() {
|
|||||||
console.log('---')
|
console.log('---')
|
||||||
|
|
||||||
// 更新manifest.json
|
// 更新manifest.json
|
||||||
console.log('\n4. 更新manifest.json...')
|
console.log('\n5. 更新manifest.json...')
|
||||||
manifest.versionName = newVersion
|
manifest.versionName = newVersion
|
||||||
manifest.versionCode = String(newVersionCode)
|
manifest.versionCode = String(newVersionCode)
|
||||||
writeManifest(manifest)
|
writeManifest(manifest)
|
||||||
console.log(' 已更新版本号')
|
console.log(' 已更新版本号')
|
||||||
|
|
||||||
// 检查编译目录是否存在
|
// 检查编译目录是否存在
|
||||||
console.log('\n5. 检查编译目录...')
|
console.log('\n6. 检查编译目录...')
|
||||||
if (!fs.existsSync(CONFIG.projectPath)) {
|
if (!fs.existsSync(CONFIG.projectPath)) {
|
||||||
console.error('错误: 编译目录不存在!')
|
console.error('错误: 编译目录不存在!')
|
||||||
console.error('请先使用 HBuilderX 编译项目')
|
console.error('请先使用 HBuilderX 编译项目')
|
||||||
@@ -239,7 +270,7 @@ function main() {
|
|||||||
console.log(' 编译目录存在')
|
console.log(' 编译目录存在')
|
||||||
|
|
||||||
// 上传到微信
|
// 上传到微信
|
||||||
console.log('\n6. 上传到微信...')
|
console.log('\n7. 上传到微信...')
|
||||||
uploadToWechat(newVersion, uploadDesc)
|
uploadToWechat(newVersion, uploadDesc)
|
||||||
|
|
||||||
console.log('\n=== 上传完成 ===')
|
console.log('\n=== 上传完成 ===')
|
||||||
|
|||||||
Reference in New Issue
Block a user