zen-gitsync 1.2.1 → 1.2.3
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/README.md +9 -3
- package/package.json +4 -2
- package/src/gitCommit.js +63 -12
- package/src/utils/index.js +9 -5
package/README.md
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
- 一键执行 `git add`、`git commit`、`git push`
|
|
9
9
|
- 使用交互式命令行输入提交信息
|
|
10
|
-
- 提交信息颜色随机显示
|
|
11
10
|
|
|
12
11
|
## 安装
|
|
13
12
|
|
|
@@ -32,5 +31,12 @@ npm install -g zen-gitsync
|
|
|
32
31
|
$ g
|
|
33
32
|
请输入你的提交信息: 修复了登录页样式问题
|
|
34
33
|
```
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
直接提交
|
|
35
|
+
```bash
|
|
36
|
+
$ g -y
|
|
37
|
+
```
|
|
38
|
+
定时执行自动提交,默认间隔1小时
|
|
39
|
+
```bash
|
|
40
|
+
$ g -y --interval
|
|
41
|
+
$ g -y --interval=秒数
|
|
42
|
+
```
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zen-gitsync",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "控制台输入g,回车,输入提交内容,自动执行git add+commit+push",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"g": "node ./src/gitCommit.js",
|
|
9
|
-
"g:y": "node ./src/gitCommit.js -y"
|
|
9
|
+
"g:y": "node ./src/gitCommit.js -y",
|
|
10
|
+
"g:y:interval": "node ./src/gitCommit.js -y --interval",
|
|
11
|
+
"g:y:interval10": "node ./src/gitCommit.js -y --interval=10"
|
|
10
12
|
},
|
|
11
13
|
"files": [
|
|
12
14
|
"src/gitCommit.js",
|
package/src/gitCommit.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import {exec, execSync} from 'child_process'
|
|
4
4
|
import os from 'os'
|
|
5
|
-
import {
|
|
5
|
+
import {coloredLog} from './utils/index.js';
|
|
6
6
|
import readline from 'readline'
|
|
7
7
|
|
|
8
|
+
let timer = null
|
|
8
9
|
|
|
9
10
|
const judgePlatform = () => {
|
|
10
11
|
// 判断是否是 Windows 系统
|
|
@@ -34,18 +35,29 @@ function rlPromisify(fn) {
|
|
|
34
35
|
const question = rlPromisify(rl.question.bind(rl))
|
|
35
36
|
|
|
36
37
|
class GitCommit {
|
|
37
|
-
constructor() {
|
|
38
|
+
constructor(options) {
|
|
38
39
|
this.statusOutput = null
|
|
40
|
+
this.exit = options.exit
|
|
39
41
|
this.init()
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
exec_exit() {
|
|
45
|
+
if (this.exit) {
|
|
46
|
+
process.exit()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
async init() {
|
|
43
51
|
try {
|
|
44
52
|
judgePlatform()
|
|
45
53
|
|
|
46
54
|
this.statusOutput = this.execSyncGitCommand('git status')
|
|
47
55
|
if (this.statusOutput.includes('nothing to commit, working tree clean')) {
|
|
48
|
-
|
|
56
|
+
if (this.statusOutput.includes('use "git push')) {
|
|
57
|
+
this.exec_push()
|
|
58
|
+
} else {
|
|
59
|
+
this.exec_exit();
|
|
60
|
+
}
|
|
49
61
|
return
|
|
50
62
|
}
|
|
51
63
|
this.execSyncGitCommand('git diff')
|
|
@@ -67,22 +79,27 @@ class GitCommit {
|
|
|
67
79
|
this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
|
|
68
80
|
}
|
|
69
81
|
|
|
70
|
-
|
|
71
82
|
// 检查是否需要拉取更新
|
|
72
83
|
this.statusOutput.includes('use "git pull') && this.execSyncGitCommand('git pull')
|
|
73
84
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
console.log('提交完成。')
|
|
78
|
-
this.execSyncGitCommand(`git log -n 1 --pretty=format:"%H %d %ad%n%B" --date=iso`)
|
|
79
|
-
process.exit();
|
|
80
|
-
})
|
|
85
|
+
this.exec_push()
|
|
86
|
+
|
|
87
|
+
|
|
81
88
|
} catch (e) {
|
|
82
89
|
console.log(`e ==> `, e)
|
|
83
90
|
}
|
|
84
91
|
}
|
|
85
92
|
|
|
93
|
+
exec_push() {
|
|
94
|
+
// 执行 git push
|
|
95
|
+
// this.execSyncGitCommand(`git push`);
|
|
96
|
+
this.execGitCommand('git push', {}, (error, stdout, stderr) => {
|
|
97
|
+
console.log('提交完成。')
|
|
98
|
+
this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
|
|
99
|
+
this.exec_exit();
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
86
103
|
execSyncGitCommand(command, options = {}) {
|
|
87
104
|
try {
|
|
88
105
|
let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
|
|
@@ -116,4 +133,38 @@ class GitCommit {
|
|
|
116
133
|
}
|
|
117
134
|
}
|
|
118
135
|
|
|
119
|
-
|
|
136
|
+
const judgeInterval = () => {
|
|
137
|
+
// 判断是否有 --interval 参数
|
|
138
|
+
const intervalArg = process.argv.find(arg => arg.startsWith('--interval'));
|
|
139
|
+
if (intervalArg) {
|
|
140
|
+
// console.log(`intervalArg ==> `, intervalArg)
|
|
141
|
+
let interval = intervalArg.split('=')[1] || 60 * 60; // 默认间隔为1小时
|
|
142
|
+
// console.log(`interval ==> `, interval)
|
|
143
|
+
interval = parseInt(interval, 10) * 1000; // 将间隔时间转换为毫秒
|
|
144
|
+
// console.log(`interval ==> `, interval)
|
|
145
|
+
if (isNaN(interval)) {
|
|
146
|
+
console.log('无效的间隔时间,请使用 --interval=秒数');
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
if (timer) {
|
|
150
|
+
console.log(`清空定时器`)
|
|
151
|
+
clearInterval(timer);
|
|
152
|
+
timer = null;
|
|
153
|
+
}
|
|
154
|
+
new GitCommit({
|
|
155
|
+
exit: false
|
|
156
|
+
})
|
|
157
|
+
timer = setInterval(() => {
|
|
158
|
+
// console.log(`定时执行`)
|
|
159
|
+
new GitCommit({
|
|
160
|
+
exit: false
|
|
161
|
+
})
|
|
162
|
+
}, interval)
|
|
163
|
+
} else {
|
|
164
|
+
new GitCommit({
|
|
165
|
+
exit: true
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
judgeInterval()
|
package/src/utils/index.js
CHANGED
|
@@ -41,8 +41,10 @@ const coloredLog = (...args) =>{
|
|
|
41
41
|
|
|
42
42
|
// 创建与控制台宽度相同的横线
|
|
43
43
|
// const line = '-'.repeat(terminalWidth);
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
let str = '├─'
|
|
45
|
+
let str2 = '│'
|
|
46
|
+
const start_line = '┌' + '──'.repeat(terminalWidth / 2 - 1) + '┐';
|
|
47
|
+
const end_line = '└' + '──'.repeat(terminalWidth / 2 - 1) + '┘';
|
|
46
48
|
let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
|
|
47
49
|
console.log(start_line);
|
|
48
50
|
_args.map(async (arg, i) => {
|
|
@@ -75,14 +77,16 @@ const coloredLog = (...args) =>{
|
|
|
75
77
|
fix2 = 2
|
|
76
78
|
}
|
|
77
79
|
fix_end = ' '.repeat(terminalWidth - length - 4 - fix2)
|
|
78
|
-
fix_end += "
|
|
80
|
+
fix_end += "│"
|
|
79
81
|
}
|
|
80
82
|
// console.log(`fix_end ==> `, fix_end)
|
|
81
83
|
if (i === 0) {
|
|
82
|
-
console.log(
|
|
84
|
+
console.log(`│ \x1b[1m\x1b[34m> ${arg}\x1b[22m\x1b[39m${fix_end}`);
|
|
85
|
+
let mid = '├─' + '──'.repeat(terminalWidth / 2 - 2) + '─┤';
|
|
86
|
+
console.log(mid);
|
|
83
87
|
} else {
|
|
84
88
|
if(arg.trim().length > 0) {
|
|
85
|
-
console.log(
|
|
89
|
+
console.log(`│${_color} ${arg}${resetColor()}${fix_end}`);
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
});
|