-
Notifications
You must be signed in to change notification settings - Fork 0
/
incrementVersion.js
57 lines (48 loc) · 1.71 KB
/
incrementVersion.js
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
53
54
55
56
57
import { readFileSync, writeFileSync } from "node:fs";
import process from "node:process";
import { execSync } from "node:child_process";
// Check if we are on the main branch
let branch = execSync("git rev-parse --abbrev-ref HEAD", {
encoding: "utf-8",
}).trim();
// Remove 'heads/' prefix if present
if (branch.startsWith("heads/")) {
branch = branch.slice(6);
}
if (branch !== "main") {
throw new Error(
`You can only increment the version on the main branch. Current branch is ${branch}`,
);
}
const args = process.argv.slice(2);
const [incrementType] = args;
const denoJsonPath = "deno.json";
const denoData = JSON.parse(readFileSync(denoJsonPath, "utf-8"));
const versionParts = denoData.version.split(".").map((d) => parseInt(d));
if (incrementType === "major") {
versionParts[0] += 1;
versionParts[1] = 0;
versionParts[2] = 0;
} else if (incrementType === "minor") {
versionParts[1] += 1;
versionParts[2] = 0;
} else if (incrementType === "patch") {
versionParts[2] += 1;
} else {
throw new Error("Invalid increment type");
}
denoData.version = versionParts.join(".");
writeFileSync(denoJsonPath, JSON.stringify(denoData, null, 2));
const packageJsonPath = "package.json";
const packageJsonData = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
packageJsonData.version = denoData.version;
writeFileSync(packageJsonPath, JSON.stringify(packageJsonData, null, 2));
execSync("git add -A");
execSync(`git commit -m "v${denoData.version}"`);
execSync("git push");
console.log(`Version incremented to ${denoData.version}`);
// Tag the current version
const tagName = `v${denoData.version}`;
execSync(`git tag ${tagName}`);
execSync(`git push origin tag ${tagName}`);
console.log(`Tagged with ${tagName}`);