-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (78 loc) · 2.03 KB
/
index.js
File metadata and controls
87 lines (78 loc) · 2.03 KB
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
78
79
80
81
82
83
84
85
86
87
import inquirer from "inquirer";
import path from "path";
import fs from "fs";
import { execa } from "execa";
async function main() {
const { branches, dir } = await identifyCurrentDirectoryAndBranches()
if (!branches.length || branches.length === 0) {
console.log("No branches to delete");
return;
}
inquirer
.prompt([
{
type: "checkbox",
name: "answer",
message: "Which branches would you like to delete?",
question: "What is your name?",
choices: branches.map((branch) => ({
name: branch,
message: branch,
})),
},
])
.then(async (answers) => {
const toDelete = answers.answer;
if (!toDelete) return;
await Promise.all(
toDelete.map(
async (branch) =>
await execa("git", ["branch", "-D", branch], {
cwd: dir
})
)
);
});
}
async function identifyCurrentDirectoryAndBranches(){
const { directory } = await inquirer.prompt([
{
type: "input",
name: "directory",
message: "What is the directory you want to delete branches in?",
default: "CWD"
}
])
const dir = directory === 'CWD' || directory === '.' ? process.cwd() : directory
const isGitRepo = await isGitRepository(dir);
if (!isGitRepo) {
throw new Error("Not a git repository");
}
const branches = await getBranches(dir);
return { branches, dir }
}
async function isGitRepository(dirPath) {
try {
const gitDirPath = path.join(dirPath, ".git");
const gitDirStat = fs.statSync(gitDirPath);
if (!gitDirStat.isDirectory()) {
return false;
}
await execa("git", ["status"], { cwd: dirPath });
return true;
} catch (error) {
return false;
}
}
async function getBranches(cwd) {
const { stdout: branches } = await execa("git", ["branch"], {
cwd
});
return branches
.split("\n")
.map((s) => s.trim())
.filter(
(branch) => !["main", "master", "* master", "* main"].includes(branch)
);
}
await main();