mihomo-party/scripts/version.js
2025-08-21 19:01:33 +08:00

48 lines
1.4 KiB
JavaScript
Raw 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 { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')
function getGitCommitHash() {
try {
return execSync('git rev-parse --short=7 HEAD').toString().trim()
} catch (error) {
console.warn('无法获取 Git commit hash使用默认值')
return 'unknown'
}
}
function processVersion() {
const packagePath = path.join(__dirname, '..', 'package.json')
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
// 备份原始版本号
const originalVersion = packageJson.version
fs.writeFileSync(
path.join(__dirname, '..', 'package.json.bak'),
JSON.stringify({ version: originalVersion }, null, 2)
)
// 检查版本号是否以 -dev 结尾
if (originalVersion.endsWith('-dev')) {
const commitHash = getGitCommitHash()
const newVersion = originalVersion.replace('-dev', `-${commitHash}-dev`)
// 更新 package.json
packageJson.version = newVersion
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2))
console.log(`版本号已更新: ${originalVersion} -> ${newVersion}`)
return newVersion
}
console.log(`版本号未修改: ${originalVersion}`)
return originalVersion
}
// 如果是直接运行此脚本,则执行版本处理
if (require.main === module) {
processVersion()
}
module.exports = { processVersion, getGitCommitHash }