zen-gitsync 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -0
- package/package.json +27 -0
- package/src/gitCommit.js +121 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 xz333221
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
# zen-gitsync
|
|
3
|
+
|
|
4
|
+
`zen-gitsync` 是一个简单的命令行工具,用于自动化 Git 提交和推送操作。只需在控制台输入 `g`,即可自动执行 `git add`、`git commit` 和 `git push` 操作,极大提升 Git 工作流程的效率。
|
|
5
|
+
|
|
6
|
+
## 特性
|
|
7
|
+
|
|
8
|
+
- 一键执行 `git add`、`git commit`、`git push`
|
|
9
|
+
- 使用交互式命令行输入提交信息
|
|
10
|
+
- 提交信息颜色随机显示
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
通过 npm 安装 `zen-gitsync`,并进行全局安装:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install -g zen-gitsync
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 使用方法
|
|
21
|
+
|
|
22
|
+
1. 在终端中,输入 `g` 并按回车。
|
|
23
|
+
2. 输入提交信息,按回车确认提交。
|
|
24
|
+
3. 工具将自动执行以下操作:
|
|
25
|
+
- `git add .`
|
|
26
|
+
- `git commit -m "你的提交信息"`
|
|
27
|
+
- `git push`
|
|
28
|
+
|
|
29
|
+
### 示例:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
$ g
|
|
33
|
+
请输入你的提交信息: 修复了登录页样式问题
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
完成提交后,你会看到一个随机颜色的提交信息确认
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zen-gitsync",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "控制台输入g,回车,输入提交内容,自动执行git add+commit+push",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"g": "node ./src/gitCommit.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src/gitCommit.js",
|
|
11
|
+
"LICENSE",
|
|
12
|
+
"package.json",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"bin": { "g": "./src/gitCommit.js" },
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/xz333221/zen-gitsync.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [],
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/xz333221/zen-gitsync/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/xz333221/zen-gitsync#readme"
|
|
27
|
+
}
|
package/src/gitCommit.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { exec, execSync } = require('child_process')
|
|
4
|
+
|
|
5
|
+
// 有时候有乱码呢123神奇
|
|
6
|
+
const readline = require('readline')
|
|
7
|
+
const rl = readline.createInterface({
|
|
8
|
+
input: process.stdin,
|
|
9
|
+
output: process.stdout
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
function rlPromisify (fn) {
|
|
13
|
+
return async (...args) => {
|
|
14
|
+
return new Promise((resolve,reject) => fn(...args, resolve, reject))
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const question = rlPromisify(rl.question.bind(rl))
|
|
19
|
+
|
|
20
|
+
const colors = [
|
|
21
|
+
'\x1b[31m', // 红色
|
|
22
|
+
'\x1b[32m', // 绿色
|
|
23
|
+
'\x1b[33m', // 黄色
|
|
24
|
+
'\x1b[34m', // 蓝色
|
|
25
|
+
'\x1b[35m', // 紫色
|
|
26
|
+
'\x1b[36m', // 青色
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
function getRandomColor() {
|
|
30
|
+
const randomIndex = Math.floor(Math.random() * colors.length);
|
|
31
|
+
return colors[randomIndex];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function resetColor() {
|
|
35
|
+
return '\x1b[0m';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function coloredLog(...args) {
|
|
39
|
+
const color = getRandomColor();
|
|
40
|
+
console.log(color, ...args.map(arg => `\n${arg}`), resetColor());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class GitCommit {
|
|
45
|
+
constructor () {
|
|
46
|
+
this.statusOutput = null
|
|
47
|
+
this.init()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async init () {
|
|
51
|
+
try {
|
|
52
|
+
// 设置终端字符编码为UTF-8
|
|
53
|
+
execSync('chcp 65001')
|
|
54
|
+
|
|
55
|
+
this.statusOutput = this.execSyncGitCommand('git status')
|
|
56
|
+
if (this.statusOutput.includes('nothing to commit, working tree clean')) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
this.execSyncGitCommand('git diff')
|
|
60
|
+
|
|
61
|
+
// 等待用户输入提交信息
|
|
62
|
+
const commitMessage = await question('请输入提交信息:')
|
|
63
|
+
|
|
64
|
+
// 执行 git add .
|
|
65
|
+
this.statusOutput.includes('(use "git add <file>') && this.execSyncGitCommand('git add .')
|
|
66
|
+
|
|
67
|
+
// 执行 git commit
|
|
68
|
+
if (this.statusOutput.includes('Untracked files:') || this.statusOutput.includes('Changes not staged for commit') || this.statusOutput.includes('Changes to be committed')) {
|
|
69
|
+
this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
// 检查是否需要拉取更新
|
|
74
|
+
this.statusOutput.includes('use "git pull') && this.execSyncGitCommand('git pull')
|
|
75
|
+
|
|
76
|
+
// 执行 git push
|
|
77
|
+
// this.execSyncGitCommand(`git push`);
|
|
78
|
+
this.execGitCommand('git push', {}, (error, stdout, stderr) => {
|
|
79
|
+
console.log('提交完成。')
|
|
80
|
+
this.execSyncGitCommand(`git log -n 1 --pretty=format:"%H %d %ad%n%B" --date=iso`)
|
|
81
|
+
process.exit();
|
|
82
|
+
})
|
|
83
|
+
} catch (e) {
|
|
84
|
+
console.log(`e ==> `, e)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
execSyncGitCommand (command, options = {}) {
|
|
89
|
+
try {
|
|
90
|
+
let { encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd() } = options
|
|
91
|
+
cwd = process.argv[2] || cwd
|
|
92
|
+
const output = execSync(command, { encoding, maxBuffer, cwd })
|
|
93
|
+
let result = output.trim()
|
|
94
|
+
coloredLog(command, result)
|
|
95
|
+
return result
|
|
96
|
+
} catch (e) {
|
|
97
|
+
console.log(`执行命令出错 ==> `, command, e)
|
|
98
|
+
throw new Error(e)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
execGitCommand (command, options = {}, callback) {
|
|
103
|
+
let { encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd() } = options
|
|
104
|
+
cwd = process.argv[2] || cwd
|
|
105
|
+
exec(command, { encoding, maxBuffer, cwd }, (error, stdout, stderr) => {
|
|
106
|
+
if (error) {
|
|
107
|
+
coloredLog(command, error)
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
if (stdout) {
|
|
111
|
+
coloredLog(command, stdout)
|
|
112
|
+
}
|
|
113
|
+
if (stderr) {
|
|
114
|
+
coloredLog(command, stderr)
|
|
115
|
+
}
|
|
116
|
+
callback && callback(error, stdout, stderr)
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
new GitCommit()
|