feat: add commit hash in dev version

This commit is contained in:
Memory 2025-08-21 19:01:33 +08:00 committed by GitHub
parent 2ebb5db055
commit c01c985c91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 86 additions and 1 deletions

View File

@ -18,6 +18,12 @@
"artifact": "node scripts/artifact.mjs",
"dev": "electron-vite dev",
"postinstall": "electron-builder install-app-deps",
"prebuild:win": "node scripts/version.js",
"prebuild:mac": "node scripts/version.js",
"prebuild:linux": "node scripts/version.js",
"postbuild:win": "node scripts/restore-version.js",
"postbuild:mac": "node scripts/restore-version.js",
"postbuild:linux": "node scripts/restore-version.js",
"build:win": "electron-vite build && electron-builder --publish never --win",
"build:mac": "electron-vite build && electron-builder --publish never --mac",
"build:linux": "electron-vite build && electron-builder --publish never --linux"
@ -97,4 +103,4 @@
"vite-plugin-monaco-editor": "^1.1.0"
},
"packageManager": "pnpm@9.15.0+sha512.76e2379760a4328ec4415815bcd6628dee727af3779aaa4c914e3944156c4299921a89f976381ee107d41f12cfa4b66681ca9c718f0668fa0831ed4c6d8ba56c"
}
}

View File

@ -0,0 +1,32 @@
const fs = require('fs')
const path = require('path')
function restoreVersion() {
const backupPath = path.join(__dirname, '..', 'package.json.bak')
const packagePath = path.join(__dirname, '..', 'package.json')
if (fs.existsSync(backupPath)) {
try {
const backup = JSON.parse(fs.readFileSync(backupPath, 'utf8'))
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
// 恢复版本号
packageJson.version = backup.version
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2))
// 删除备份文件
fs.unlinkSync(backupPath)
console.log(`版本号已恢复: ${backup.version}`)
} catch (error) {
console.error('恢复版本号时出错:', error)
}
}
}
// 如果是直接运行此脚本,则执行版本恢复
if (require.main === module) {
restoreVersion()
}
module.exports = { restoreVersion }

47
scripts/version.js Normal file
View File

@ -0,0 +1,47 @@
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 }