-
Notifications
You must be signed in to change notification settings - Fork 3
/
incrementVersion.js
53 lines (45 loc) · 1.49 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
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 filePath = "package.json";
const data = JSON.parse(readFileSync(filePath, "utf-8"));
console.log(`\nCurrent version is ${data.version}`);
const versionParts = data.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");
}
data.version = versionParts.join(".");
writeFileSync(filePath, JSON.stringify(data, null, 2));
execSync("git add -A");
execSync(`git commit -m "v${data.version}"`);
execSync("git push");
console.log(`Version incremented to ${data.version}`);
// Tag the current version
const tagName = `v${data.version}`;
execSync(`git tag ${tagName}`);
execSync(`git push origin tag ${tagName}`);
console.log(`Tagged with ${tagName}`);