-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathversion-push.ts
59 lines (52 loc) · 2.39 KB
/
version-push.ts
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
58
59
import { execSync } from "child_process";
// Get the version passed as argument
const version = process.argv[2];
if (!version) throw new Error("Please provide a version");
console.log(`Pushing version ${version} ...`);
// When running this script, we are checked-out to a tag instead of a branch, now we need to find the related branch and checkout to it
const currentHeadHashStdout = String(execSync(`git rev-parse HEAD`));
console.log("\n" + currentHeadHashStdout);
const currentHeadHash = currentHeadHashStdout.replace("\n", "");
const showRefsStdout = String(execSync(`git show-ref`));
const branches = showRefsStdout
.split("\n")
.filter((headAndBranch) => headAndBranch.startsWith(`${currentHeadHash} refs/remotes/origin`))
.map((headAndBranch) => headAndBranch.replace(/\S+\srefs\/remotes\/origin\//, ""));
if (branches.length > 0) {
const firstBranch = branches[0];
// stash the modified files
console.log(String(execSync(`git stash`)));
// checkout to the branch for this tag
console.log(String(execSync(`git checkout ${firstBranch}`)));
// unstash
console.log(String(execSync(`git stash pop`)));
// stage
console.log(String(execSync(`git add .`)));
// commit
console.log(String(execSync(`git config user.name "DzCode i/o"`)));
console.log(String(execSync(`git config user.email [email protected]`)));
console.log(String(execSync(`git commit -m "version: ${version}\n[skip ci]" --no-verify`)));
// preserve the new head commit for later use
const headStdOut = String(execSync(`git rev-parse HEAD`));
console.log(headStdOut);
const firstBranchHeadHash = headStdOut.replace("\n", "");
// push the new head commit
console.log(String(execSync(`git push`)));
// overwrite the current tag
console.log(String(execSync(`git tag ${version} -f`)));
// push the new tag
console.log(String(execSync(`git push --tags -f`)));
// in case we have other branches with the same tag, we should update them as well
if (branches.length > 1) {
console.log(
`found ${
branches.length - 1
} more branches with same HEAD, cherry-picking ${firstBranchHeadHash}...`,
);
// cherry-pick the new head commit`firstBranchHeadHash`
console.log(`@TODO-ZM: cherry-pick ${firstBranchHeadHash} to all branches with same HEAD`);
}
console.log(`Done applying version ${version}`);
} else {
console.log(`Skipping because no branches are found with the current HEAD`);
}