aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/scripts/bump-tauri.ts
blob: 0cf6aea5ad0358befec2fd9196d7c8aceb216ee5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import fs from "node:fs";
import path from "node:path";
import consola from "consola";
import toml from "toml";

const tauriJsonPath = path.join(
  __dirname,
  "..",
  "src-tauri",
  "tauri.conf.json",
);
consola.debug("tauriJsonPath:", tauriJsonPath);
const tauriTomlPath = path.join(__dirname, "..", "src-tauri", "Cargo.toml");
consola.debug("tauriTomlPath:", tauriTomlPath);

const getCurrentVersion = () => {
  const tauriJsonData = fs.readFileSync(tauriJsonPath, "utf8");
  const tauriJson = JSON.parse(tauriJsonData);
  const version = tauriJson.version;
  if (!version) throw new Error("Version field not found in tauri.conf.json");
  return version;
};

const getBumpVersion = () => {
  const tauriTomlData = fs.readFileSync(tauriTomlPath, "utf8");
  const tauriToml = toml.parse(tauriTomlData);
  const version = tauriToml.package.version;
  if (!version) throw new Error("Version field not found in Cargo.toml");
  return version;
};

const replaceVersion = (content: string, version: string) => {
  const newJson = content.replace(
    /"version": "[^"]+"/,
    `"version": "${version}"`,
  );
  return newJson;
};

const tauriJsonData = fs.readFileSync(tauriJsonPath, "utf8");
const currentVersion = getCurrentVersion();
const bumpVersion = getBumpVersion();
consola.debug("currentVersion:", currentVersion);
consola.debug("bumpVersion:", bumpVersion);

if (currentVersion !== bumpVersion) {
  const replacedData = replaceVersion(tauriJsonData, bumpVersion);
  consola.info(`Bumped version from ${currentVersion} to ${bumpVersion}`);
  fs.writeFileSync(tauriJsonPath, replacedData);
} else {
  consola.info(`Version ${currentVersion} is already up-to-date`);
}