zen-gitsync 1.2.2 → 1.2.4

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 CHANGED
@@ -26,10 +26,25 @@ npm install -g zen-gitsync
26
26
  - `git push`
27
27
 
28
28
  ### 示例:
29
-
29
+ #### 交互式提交:
30
30
  ```bash
31
31
  $ g
32
32
  请输入你的提交信息: 修复了登录页样式问题
33
33
  ```
34
-
35
- 完成提交后,你会看到一个随机颜色的提交信息确认
34
+ #### 直接提交:
35
+ ```bash
36
+ $ g -y
37
+ ```
38
+ #### 定时执行自动提交,默认间隔1小时
39
+ ```bash
40
+ $ g -y --interval
41
+ $ g -y --interval=秒数
42
+ ```
43
+ #### 指定目录提交
44
+ ```bash
45
+ $ g --path=./
46
+ ```
47
+
48
+ ```bash
49
+ $ g --cwd=./
50
+ ```
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
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",
12
+ "g:cwd": "node ./src/gitCommit.js --path=./"
10
13
  },
11
14
  "files": [
12
15
  "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 { coloredLog } from './utils/index.js';
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 系统
@@ -17,6 +18,16 @@ const judgePlatform = () => {
17
18
  }
18
19
  }
19
20
  };
21
+ const getCwd = () => {
22
+ const cwdArg = process.argv.find(arg => arg.startsWith('--path')) || process.argv.find(arg => arg.startsWith('--cwd'));
23
+ if (cwdArg) {
24
+ // console.log(`cwdArg ==> `, cwdArg)
25
+ const [, value] = cwdArg.split('=')
26
+ // console.log(`value ==> `, value)
27
+ return value || process.cwd()
28
+ }
29
+ return process.cwd()
30
+ }
20
31
 
21
32
  // 有时候有乱码呢123神奇
22
33
 
@@ -34,18 +45,29 @@ function rlPromisify(fn) {
34
45
  const question = rlPromisify(rl.question.bind(rl))
35
46
 
36
47
  class GitCommit {
37
- constructor() {
48
+ constructor(options) {
38
49
  this.statusOutput = null
50
+ this.exit = options.exit
39
51
  this.init()
40
52
  }
41
53
 
54
+ exec_exit() {
55
+ if (this.exit) {
56
+ process.exit()
57
+ }
58
+ }
59
+
42
60
  async init() {
43
61
  try {
44
62
  judgePlatform()
45
63
 
46
64
  this.statusOutput = this.execSyncGitCommand('git status')
47
65
  if (this.statusOutput.includes('nothing to commit, working tree clean')) {
48
- process.exit();
66
+ if (this.statusOutput.includes('use "git push')) {
67
+ this.exec_push()
68
+ } else {
69
+ this.exec_exit();
70
+ }
49
71
  return
50
72
  }
51
73
  this.execSyncGitCommand('git diff')
@@ -67,26 +89,31 @@ class GitCommit {
67
89
  this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
68
90
  }
69
91
 
70
-
71
92
  // 检查是否需要拉取更新
72
93
  this.statusOutput.includes('use "git pull') && this.execSyncGitCommand('git pull')
73
94
 
74
- // 执行 git push
75
- // this.execSyncGitCommand(`git push`);
76
- this.execGitCommand('git push', {}, (error, stdout, stderr) => {
77
- console.log('提交完成。')
78
- this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
79
- process.exit();
80
- })
95
+ this.exec_push()
96
+
97
+
81
98
  } catch (e) {
82
99
  console.log(`e ==> `, e)
83
100
  }
84
101
  }
85
102
 
103
+ exec_push() {
104
+ // 执行 git push
105
+ // this.execSyncGitCommand(`git push`);
106
+ this.execGitCommand('git push', {}, (error, stdout, stderr) => {
107
+ console.log('提交完成。')
108
+ this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
109
+ this.exec_exit();
110
+ })
111
+ }
112
+
86
113
  execSyncGitCommand(command, options = {}) {
87
114
  try {
88
- let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
89
- // cwd = process.argv[2] || cwd
115
+ let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024} = options
116
+ let cwd = getCwd()
90
117
  const output = execSync(command, {encoding, maxBuffer, cwd})
91
118
  let result = output.trim()
92
119
  coloredLog(command, result)
@@ -98,8 +125,8 @@ class GitCommit {
98
125
  }
99
126
 
100
127
  execGitCommand(command, options = {}, callback) {
101
- let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
102
- // cwd = process.argv[2] || cwd
128
+ let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024} = options
129
+ let cwd = getCwd()
103
130
  exec(command, {encoding, maxBuffer, cwd}, (error, stdout, stderr) => {
104
131
  if (error) {
105
132
  coloredLog(command, error)
@@ -116,4 +143,38 @@ class GitCommit {
116
143
  }
117
144
  }
118
145
 
119
- new GitCommit()
146
+ const judgeInterval = () => {
147
+ // 判断是否有 --interval 参数
148
+ const intervalArg = process.argv.find(arg => arg.startsWith('--interval'));
149
+ if (intervalArg) {
150
+ // console.log(`intervalArg ==> `, intervalArg)
151
+ let interval = intervalArg.split('=')[1] || 60 * 60; // 默认间隔为1小时
152
+ // console.log(`interval ==> `, interval)
153
+ interval = parseInt(interval, 10) * 1000; // 将间隔时间转换为毫秒
154
+ // console.log(`interval ==> `, interval)
155
+ if (isNaN(interval)) {
156
+ console.log('无效的间隔时间,请使用 --interval=秒数');
157
+ process.exit(1);
158
+ }
159
+ if (timer) {
160
+ console.log(`清空定时器`)
161
+ clearInterval(timer);
162
+ timer = null;
163
+ }
164
+ new GitCommit({
165
+ exit: false
166
+ })
167
+ timer = setInterval(() => {
168
+ // console.log(`定时执行`)
169
+ new GitCommit({
170
+ exit: false
171
+ })
172
+ }, interval)
173
+ } else {
174
+ new GitCommit({
175
+ exit: true
176
+ })
177
+ }
178
+ };
179
+
180
+ judgeInterval()