-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
41 lines (32 loc) · 822 Bytes
/
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
import isGit from "is-git-repository";
import process from "node:process";
import { execSync } from "node:child_process";
const cwd = process.cwd();
const defaultOptions = {
cwd,
branchOptions: null,
};
const sanitize = (input) => {
if (Array.isArray(input)) return input.map(sanitize).join(" ").trim();
if (typeof input !== "string") {
return "";
}
return input?.replace(/[^a-zA-Z0-9-_]/g, "");
};
const branchName = (options = defaultOptions) => {
if (!isGit(options.cwd)) {
return false;
}
const branchOptions = sanitize(options.branchOptions);
try {
const cmd = `git branch --show-current ${branchOptions}`;
return execSync(cmd.trim(), {
cwd: options.cwd ?? cwd,
})
.toString()
.trim();
} catch {
return false;
}
};
export default branchName;