Skip to content

Commit ca02a1c

Browse files
committed
init
1 parent 8b7a247 commit ca02a1c

File tree

7 files changed

+136
-96
lines changed

7 files changed

+136
-96
lines changed

.gitignore

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,99 +6,5 @@ yarn-debug.log*
66
yarn-error.log*
77
lerna-debug.log*
88

9-
# Diagnostic reports (https://nodejs.org/api/report.html)
10-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11-
12-
# Runtime data
13-
pids
14-
*.pid
15-
*.seed
16-
*.pid.lock
17-
18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
21-
# Coverage directory used by tools like istanbul
22-
coverage
23-
*.lcov
24-
25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
34-
# node-waf configuration
35-
.lock-wscript
36-
37-
# Compiled binary addons (https://nodejs.org/api/addons.html)
38-
build/Release
39-
40-
# Dependency directories
41-
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
49-
50-
# Optional npm cache directory
51-
.npm
52-
53-
# Optional eslint cache
54-
.eslintcache
55-
56-
# Microbundle cache
57-
.rpt2_cache/
58-
.rts2_cache_cjs/
59-
.rts2_cache_es/
60-
.rts2_cache_umd/
61-
62-
# Optional REPL history
63-
.node_repl_history
64-
65-
# Output of 'npm pack'
66-
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
83-
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
9+
/node_modules
10+
/package-lock.json

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# bm
2+
3+
Git branch manager

bin/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require("../src/index");

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@lxfu/gm",
3+
"version": "0.0.1",
4+
"description": "Git branch manger",
5+
"bin": "./bin/index.js",
6+
"scripts": {},
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/lxfu1/bm.git"
10+
},
11+
"keywords": [
12+
"git",
13+
"branch"
14+
],
15+
"author": "lxfu1",
16+
"license": "MIT",
17+
"bugs": {
18+
"url": "https://github.com/lxfu1/bm/issues"
19+
},
20+
"homepage": "https://github.com/lxfu1/bm#readme",
21+
"dependencies": {
22+
"commander": "^9.2.0",
23+
"shelljs": "^0.8.5"
24+
}
25+
}

src/constants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const COLOR_MAP = {
2+
red: "\x1b[31m",
3+
yellow: "\x1b[33m",
4+
green: "\x1b[32m",
5+
white: "\x1b[37m",
6+
};
7+
module.exports = {
8+
COLOR_MAP,
9+
};

src/index.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const shell = require("shelljs");
2+
const commander = require("commander");
3+
const { exec, exit } = shell;
4+
5+
const { printer } = require("./utils");
6+
7+
const pkg = require("../package.json");
8+
9+
const program = new commander.Command("gm");
10+
11+
program.version(pkg.version);
12+
13+
const onList = (option) => {
14+
const { r, a } = option;
15+
if (a) {
16+
exec(`git branch -a`);
17+
} else if (r) {
18+
exec(`git branch -r`);
19+
} else {
20+
exec(`git branch`);
21+
}
22+
exit(1);
23+
};
24+
25+
const onDelete = (branchName) => {
26+
exec(`git branch -D ${branchName}`);
27+
exit(1);
28+
};
29+
30+
const onInvertDelete = () => {
31+
const stableBranchs = process.argv.splice(2);
32+
const { stdout } = exec("git symbolic-ref --short -q HEAD");
33+
stableBranchs.push(stdout);
34+
const { stdout: localBranchs } = exec("git branch");
35+
const deleteBranchs = localBranchs
36+
.split("\n")
37+
.map((item) => item.replace(/\s+/g, ""))
38+
.filter(
39+
(item) => item && !stableBranchs.includes(item) && !item.startsWith("*")
40+
);
41+
if (deleteBranchs.length > 0) {
42+
printer(deleteBranchs, "green");
43+
pipeline.question(
44+
`Are you sure to delete the above branches(y/n)?`,
45+
(check) => {
46+
if (["y", "Y"].includes(check)) {
47+
deleteBranchs.forEach((branch) => {
48+
exec(`git branch -D ${branch}`);
49+
});
50+
console.log(`Deleted`);
51+
}
52+
pipeline.close();
53+
}
54+
);
55+
} else {
56+
printer(`There are no branches to delete`, "green");
57+
}
58+
exit(1);
59+
};
60+
61+
program
62+
.command("l")
63+
.description("List all the branch")
64+
.option("-a", "all branch")
65+
.option("-r", "remote branch")
66+
.action(onList);
67+
68+
program
69+
.command("d <branch-name>")
70+
.description("Delete one branch")
71+
.action(onDelete);
72+
73+
program
74+
.command("D [branch-name]")
75+
.description("Delete all branchs exclude input [branchs] and current branch")
76+
.action(onInvertDelete);
77+
78+
program.parse(process.argv);

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const readline = require("readline");
2+
const { COLOR_MAP } = require("constants");
3+
4+
const pipeline = readline.createInterface({
5+
input: process.stdin,
6+
output: process.stdout,
7+
});
8+
9+
const printer = (val, color) => {
10+
console.log(`${COLOR_MAP[color]}%s\x1b[0m`, val);
11+
};
12+
13+
module.exports = {
14+
printer,
15+
pipeline,
16+
};

0 commit comments

Comments
 (0)