/** * 构建前将版本号 patch 段 +1,并同步写入 tauri.conf.json / Cargo.toml / aiclient_ui / npmrelease。 */ const fs = require("node:fs"); const path = require("node:path"); const tauriDir = path.join(__dirname, ".."); const tauriConfPath = path.join(tauriDir, "tauri.conf.json"); const cargoTomlPath = path.join(tauriDir, "Cargo.toml"); const uiPackageJsonPath = path.join(tauriDir, "..", "aiclient_ui", "package.json"); const npmReleasePackageJsonPath = path.join( tauriDir, "..", "..", "npmrelease", "package.json", ); function bumpPatch(version) { const parts = String(version) .trim() .split(".") .map((p) => Number.parseInt(p, 10)); if (parts.length === 0 || parts.some(Number.isNaN)) { throw new Error(`无法解析版本号: ${version}`); } parts[parts.length - 1] += 1; return parts.join("."); } function updateJsonVersion(filePath, newVersion) { const data = JSON.parse(fs.readFileSync(filePath, "utf8")); const oldVersion = data.version; data.version = newVersion; fs.writeFileSync(filePath, `${JSON.stringify(data, null, 2)}\n`, "utf8"); return oldVersion; } function updateCargoVersion(filePath, newVersion) { const content = fs.readFileSync(filePath, "utf8"); const match = content.match(/^version\s*=\s*"([^"]+)"/m); if (!match) { throw new Error(`Cargo.toml 中未找到 version 字段: ${filePath}`); } const oldVersion = match[1]; const next = content.replace( /^version\s*=\s*"[^"]+"/m, `version = "${newVersion}"`, ); fs.writeFileSync(filePath, next, "utf8"); return oldVersion; } const conf = JSON.parse(fs.readFileSync(tauriConfPath, "utf8")); const newVersion = bumpPatch(conf.version); updateJsonVersion(tauriConfPath, newVersion); if (fs.existsSync(uiPackageJsonPath)) { updateJsonVersion(uiPackageJsonPath, newVersion); } if (fs.existsSync(npmReleasePackageJsonPath)) { updateJsonVersion(npmReleasePackageJsonPath, newVersion); } else { console.warn(`未找到 npmrelease/package.json: ${npmReleasePackageJsonPath}`); } updateCargoVersion(cargoTomlPath, newVersion); console.log(`版本号已更新: ${conf.version} -> ${newVersion}`);