generated from zacanger/ts-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·85 lines (68 loc) · 2.08 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
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
#!/usr/bin/env node
const { execSync } = require('child_process')
const remoteName = process.argv[2] || 'origin'
const useCol = process.stdout.isTTY
const reset = useCol ? '\u001b[0m' : ''
const green = useCol ? '\u001b[0;32m' : ''
const yellow = useCol ? '\u001b[0;33m' : ''
const red = useCol ? '\u001b[0;31m' : ''
const isInGitRepo = () => {
try {
return (
execSync('git rev-parse --is-inside-work-tree').toString().trim() ===
'true'
)
} catch {
return false
}
}
if (!isInGitRepo()) {
process.exit(1)
} else {
const cleanUpLists = (s = '') =>
s
.split('\n')
.map((a) => a.trim())
.map((a) => a.replace(`${remoteName}/`, ''))
.filter((a) => !a.includes('->'))
try {
execSync('git fetch --all')
} catch (e) {
console.error('Failed fetching, continuing with current local data', e)
}
const currentBranch = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim()
const remoteBranches = cleanUpLists(
execSync('git branch -r').toString().trim()
)
const localBranches = cleanUpLists(
execSync('git branch').toString().trim().replace('* ', '')
)
const uniq = (xs) => xs.filter((v, i, s) => s.indexOf(v) === i)
const allBranches = uniq(
[...remoteBranches, ...localBranches].sort((a, b) => a.localeCompare(b))
)
const onlyRemote = allBranches.filter((a) => !localBranches.includes(a))
const onlyLocal = allBranches.filter((a) => !remoteBranches.includes(a))
const onBoth = allBranches.filter(
(a) => remoteBranches.includes(a) && localBranches.includes(a)
)
const annotatedBranches = allBranches.map((a) => {
let branchString = ` ${a}`
if (a === currentBranch) {
branchString = `* ${a}`
}
if (onlyRemote.includes(a)) {
branchString = `${yellow}${branchString} (remote)${reset}`
}
if (onlyLocal.includes(a)) {
branchString = `${red}${branchString} (local)${reset}`
}
if (onBoth.includes(a)) {
branchString = `${green}${branchString} (both)${reset}`
}
return branchString
})
console.log(annotatedBranches.join('\n'))
}