import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; // 获取项目根目录和 src-tauri 目录路径 const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const projectRoot = path.join(__dirname, '..', '..'); const tauriConfigPath = path.join(projectRoot, 'src-tauri', 'tauri.conf.json'); const packageJsonPath = path.join(projectRoot, 'package.json'); try { // 读取 tauri.conf.json const tauriConfigRaw: string = fs.readFileSync(tauriConfigPath, 'utf8'); const tauriConfig: { version: string } = JSON.parse(tauriConfigRaw); const tauriVersion: string = tauriConfig.version; console.log(`从 tauri.conf.json 读取的版本号: ${tauriVersion}`); // 计算新的版本号(tauri版本号尾号加1) const newPackageVersion: string = incrementVersion(tauriVersion); // 读取 package.json const packageJsonRaw: string = fs.readFileSync(packageJsonPath, 'utf8'); const packageJson: { version: string } = JSON.parse(packageJsonRaw); // 更新 package.json 中的版本号 packageJson.version = newPackageVersion; tauriConfig.version = newPackageVersion; // 写入更新后的 package.json fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8'); fs.writeFileSync(tauriConfigPath, JSON.stringify(tauriConfig, null, 2), 'utf8'); console.log(`已更新 package.json 和 tauri.conf.json 的版本号为: ${newPackageVersion}`); } catch (error: any) { console.error('更新版本号时发生错误:', error); process.exit(1); } /** * 将版本号的最后一个数字部分加1 * @param version - 原始版本号,如 "1.0.10" * @returns 更新后的版本号,如 "1.0.11" */ function incrementVersion(version: string): string { const parts: string[] = version.split('.'); if (parts.length > 0) { const lastPartIndex = parts.length - 1; const lastPart = Number(parts[lastPartIndex]); if (!isNaN(lastPart)) { parts[lastPartIndex] = (lastPart + 1).toString(); return parts.join('.'); } } return version; // 如果无法解析版本号,则返回原版本号 }