diff --git a/package.json b/package.json index 17c0306..ad34316 100644 --- a/package.json +++ b/package.json @@ -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" -} +} \ No newline at end of file diff --git a/scripts/restore-version.js b/scripts/restore-version.js new file mode 100644 index 0000000..40b3b8d --- /dev/null +++ b/scripts/restore-version.js @@ -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 } diff --git a/scripts/version.js b/scripts/version.js new file mode 100644 index 0000000..792090f --- /dev/null +++ b/scripts/version.js @@ -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 }