|
| 1 | +/** |
| 2 | + * sync-version.mjs — 将仓库根目录的 VERSION 同步到各配置文件(单一版本源)。 |
| 3 | + * CI 打包前与本地发版前执行,保证安装包版本号一致。 |
| 4 | + * 幂等:值一致时不改写文件。 |
| 5 | + */ |
| 6 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 7 | +import { dirname, join } from "node:path"; |
| 8 | +import { fileURLToPath } from "node:url"; |
| 9 | + |
| 10 | +const desktopDir = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 11 | +const version = readFileSync(join(desktopDir, "..", "VERSION"), "utf-8").trim(); |
| 12 | + |
| 13 | +if (!/^\d+\.\d+\.\d+(-[\w.]+)?$/.test(version)) { |
| 14 | + console.error(`[sync-version] VERSION 内容非法:${version}`); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +const targets = [ |
| 19 | + { |
| 20 | + file: join(desktopDir, "src-tauri", "tauri.conf.json"), |
| 21 | + pattern: /("version"\s*:\s*")[^"]+(")/, |
| 22 | + replace: (v) => `$1${v}$2`, |
| 23 | + }, |
| 24 | + { |
| 25 | + file: join(desktopDir, "package.json"), |
| 26 | + pattern: /("version"\s*:\s*")[^"]+(")/, |
| 27 | + replace: (v) => `$1${v}$2`, |
| 28 | + }, |
| 29 | + { |
| 30 | + file: join(desktopDir, "src-tauri", "Cargo.toml"), |
| 31 | + pattern: /^(version\s*=\s*")[^"]+(")/m, |
| 32 | + replace: (v) => `$1${v}$2`, |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +for (const { file, pattern, replace } of targets) { |
| 37 | + const name = file.split(/[\\/]/).slice(-2).join("/"); |
| 38 | + const text = readFileSync(file, "utf-8").replace(/\r\n/g, "\n"); |
| 39 | + if (!pattern.test(text)) { |
| 40 | + console.error(`[sync-version] ${name} 未找到 version 字段`); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + const updated = text.replace(pattern, replace(version)); |
| 44 | + if (updated !== text) { |
| 45 | + writeFileSync(file, updated, "utf-8"); |
| 46 | + console.log(`[sync-version] ${name}: -> ${version}`); |
| 47 | + } else { |
| 48 | + console.log(`[sync-version] ${name}: 已是 ${version}`); |
| 49 | + } |
| 50 | +} |
| 51 | +console.log(`[sync-version] 完成,当前版本 ${version}`); |
0 commit comments