zen-gitsync 1.2.2 → 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 -2
- package/package.json +4 -2
- package/src/gitCommit.js +63 -12
package/README.md
CHANGED
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:"%B%n%h %d%n%ad" --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()
|