-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (51 loc) · 1.99 KB
/
index.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
#!/usr/bin/env node
const { exec } = require("child_process");
const co = require("co");
var prompt = require("prompt");
function execute(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout) => {
if (err) {
return reject(err);
}
resolve(stdout);
});
});
}
function branchNameParser(branch) {
return branch.replace(/^\s+/, "").replace(/^remotes\//, "").replace(/^origin\//, "");
}
co(function* run() {
yield execute("git fetch -p");
const remoteBranches = (yield execute("git branch -r")).split("\n").map(branchNameParser);
const localBranches = (yield execute("git branch")).split("\n").filter((localBranch) => !localBranch.match(/^\*/)).map(branchNameParser);
const branchesToBeRemoved = localBranches.filter((localBranch) => !remoteBranches.includes(localBranch));
if (branchesToBeRemoved.length) {
prompt.start();
prompt.message = '';
prompt.delimiter = '';
prompt.colors = false;
const { confirm } = yield prompt.get({
properties: {
// setup the dialog
confirm: {
// allow yes, no, y, n, YES, NO, Y, N as answer
pattern: /^(yes|no|y|n)$/gi,
description: `The following branches will be removed:\n\n${branchesToBeRemoved.join("\n")}\n\nConfirm (Y/N)?`,
message: 'Type yes/no',
required: true
}
}
});
if (!["y", "yes"].includes(confirm.toLowerCase())) {
return;
}
yield execute(`git branch -D ${branchesToBeRemoved.join(" ")}`);
console.log("Done!");
} else {
console.log("No branches to be removed");
}
})
.catch((e) => {
process.exit(1);
});