-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
77 lines (63 loc) · 2.15 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
import { execSync } from "child_process";
import ora from "ora";
import pc from "picocolors";
import prompts from "prompts";
const printText = console.log;
(async () => {
const spinner = ora("Start cleaning up local branches").start();
try {
// Fetch latest changes from remote
execSync("git fetch");
// Get the name of the current branch
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD")
.toString()
.trim();
// Get the names of all local branches
const localBranches = execSync("git branch")
.toString()
.split("\n")
.map((branch) => branch.replace("*", "").trim())
.filter(
(branch) => !["", "master", "main", currentBranch].includes(branch)
);
// Get the names of all remote branches
const remoteBranches = execSync("git branch -r")
.toString()
.split("\n")
.map((branch) => branch.trim())
.filter((branch) => branch !== "");
// Get the names of all local branches that do not have a corresponding remote branch
const localOnlyBranches = localBranches.filter(
(branch) => !remoteBranches.includes(`origin/${branch}`)
);
if (localOnlyBranches.length > 0) {
spinner.info(
"Local branches to be deleted: \n" +
localOnlyBranches.map((branch) => `- ${branch}`).join("\n") +
"\n"
);
const response = await prompts({
type: "confirm",
name: "confirmValue",
message: "Are you sure want to delete the local branches?",
initial: true,
});
printText();
if (response.confirmValue) {
// Delete each local-only branch
localOnlyBranches.forEach((branch) => {
execSync(`git branch -D ${branch}`);
});
spinner.succeed(pc.green("Successfully deleted the local branches"));
} else {
spinner.fail(pc.red("Cancel deleting the local branches"));
}
return;
}
spinner.succeed(pc.green("No local branches to be deleted"));
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e);
spinner.fail(pc.red("Error: " + errorMessage));
}
})();