/** * 构建前将版本号 patch 段 +1,并同步写入 tauri.conf.json / Cargo.toml / package.json。 */ const fs = require("node:fs"); const path = require("node:path"); const tauriDir = path.join(__dirname, ".."); const rootDir = path.join(tauriDir, ".."); const tauriConfPath = path.join(tauriDir, "tauri.conf.json"); const cargoTomlPath = path.join(tauriDir, "Cargo.toml"); const packageJsonPath = path.join(rootDir, "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); updateJsonVersion(packageJsonPath, newVersion); updateCargoVersion(cargoTomlPath, newVersion); console.log(`版本号已更新: ${conf.version} -> ${newVersion}`);