zen-gitsync 2.1.9 → 2.1.10

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
@@ -75,6 +75,12 @@ $ g --cwd=<path>
75
75
  start /min cmd /k "g -y --path=你要同步的文件夹 --interval"
76
76
  ```
77
77
 
78
+ #### cmd 定时执行命令
79
+ ```shell
80
+ start /min cmd /k "g --cmd=\"echo hello\" --cmd-interval=5" # 每5秒执行一次echo hello
81
+ start /min cmd /k "g --cmd=\"echo at-time\" --at=23:59" # 在23:59执行一次echo at-time
82
+ ```
83
+
78
84
  #### 不显示git diff内容
79
85
  ```shell
80
86
  $ g --no-diff
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "2.1.9",
3
+ "version": "2.1.10",
4
4
  "description": "一个 git 自动查看差异并提交的工具",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -23,6 +23,8 @@
23
23
  "g:y": "g -y",
24
24
  "g:reset": "git reset --hard origin/main",
25
25
  "g:ui": "node ./src/gitCommit.js ui",
26
+ "g:test-cmd": "node ./src/gitCommit.js --cmd=\"echo zen-gitsync test\" --cmd-interval=5",
27
+ "g:test-cmd-at": "node ./src/gitCommit.js --cmd=\"echo at-time-test\" --at=21:09",
26
28
  "start:vue": "cd ./src/ui/client && npm run dev",
27
29
  "start:server": "node -e \"import('./src/ui/server/index.js').then(module => module.default())\""
28
30
  },
package/src/gitCommit.js CHANGED
@@ -12,6 +12,7 @@ import config from './config.js';
12
12
  import dateFormat from 'date-fns/format';
13
13
  import logUpdate from 'log-update';
14
14
  import startUIServer from './ui/server/index.js';
15
+ import { exec } from 'child_process';
15
16
 
16
17
  let countdownInterval = null;
17
18
 
@@ -138,7 +139,77 @@ async function main() {
138
139
 
139
140
  await handleConfigCommands();
140
141
 
141
- judgeInterval();
142
+ // ========== 新增:自定义cmd定时/定点执行功能 ==========
143
+ const cmdArg = process.argv.find(arg => arg.startsWith('--cmd='));
144
+ if (cmdArg) {
145
+ const cmd = cmdArg.split('=')[1].replace(/^['"]|['"]$/g, '');
146
+ const atArg = process.argv.find(arg => arg.startsWith('--at='));
147
+ const intervalArg = process.argv.find(arg => arg.startsWith('--cmd-interval='));
148
+ if (atArg) {
149
+ // 定点执行
150
+ const atTime = atArg.split('=')[1].replace(/^['"]|['"]$/g, '');
151
+ const now = new Date();
152
+ let target;
153
+ if (/^\d{2}:\d{2}$/.test(atTime)) {
154
+ // 只给了时:分,今天的
155
+ const [h, m] = atTime.split(':');
156
+ target = new Date(now.getFullYear(), now.getMonth(), now.getDate(), h, m, 0);
157
+ } else {
158
+ target = new Date(atTime);
159
+ }
160
+ const delay = target - now;
161
+ if (delay > 0) {
162
+ console.log(`将在 ${target.toLocaleString()} 执行: ${cmd}`);
163
+ setTimeout(() => {
164
+ console.log(`\n[自定义命令执行] ${new Date().toLocaleString()}\n> ${cmd}`);
165
+ exec(cmd, (err, stdout, stderr) => {
166
+ if (err) {
167
+ console.error(`[自定义命令错误]`, err.message);
168
+ }
169
+ if (stdout) console.log(`[自定义命令输出]\n${stdout}`);
170
+ if (stderr) console.error(`[自定义命令错误输出]\n${stderr}`);
171
+ });
172
+ }, delay);
173
+ } else {
174
+ console.log('指定时间已过,不执行自定义命令');
175
+ }
176
+ } else if (intervalArg) {
177
+ // 定时循环执行
178
+ const interval = parseInt(intervalArg.split('=')[1], 10) * 1000;
179
+ console.log(`每隔 ${interval/1000} 秒执行: ${cmd}`);
180
+ setInterval(() => {
181
+ console.log(`\n[自定义命令执行] ${new Date().toLocaleString()}\n> ${cmd}`);
182
+ exec(cmd, (err, stdout, stderr) => {
183
+ if (err) {
184
+ console.error(`[自定义命令错误]`, err.message);
185
+ }
186
+ if (stdout) console.log(`[自定义命令输出]\n${stdout}`);
187
+ if (stderr) console.error(`[自定义命令错误输出]\n${stderr}`);
188
+ });
189
+ }, interval);
190
+ } else {
191
+ // 立即执行一次
192
+ console.log(`[自定义命令立即执行] > ${cmd}`);
193
+ exec(cmd, (err, stdout, stderr) => {
194
+ if (err) {
195
+ console.error(`[自定义命令错误]`, err.message);
196
+ }
197
+ if (stdout) console.log(`[自定义命令输出]\n${stdout}`);
198
+ if (stderr) console.error(`[自定义命令错误输出]\n${stderr}`);
199
+ });
200
+ }
201
+ }
202
+ // ========== 新增功能结束 ==========
203
+
204
+ // 判断是否需要执行git自动提交
205
+ const hasGitTask = process.argv.some(arg =>
206
+ arg.startsWith('--interval') ||
207
+ arg === '-y' ||
208
+ arg.startsWith('-m')
209
+ );
210
+ if (hasGitTask || !cmdArg) {
211
+ judgeInterval();
212
+ }
142
213
  }
143
214
 
144
215
  const showStartInfo = (interval) => {
@@ -303,7 +303,15 @@ Options:
303
303
  addResetScript Add "g:reset": "git reset --hard origin/<current-branch>" to package.json scripts
304
304
  ui Launch graphical user interface (v2.0.0)
305
305
 
306
+ --cmd="your-cmd" Execute custom cmd command (immediately, at a time, or periodically)
307
+ --cmd-interval=<seconds> Execute custom cmd every N seconds
308
+ --at="HH:MM" Execute custom cmd at a specific time (today) or --at="YYYY-MM-DD HH:MM:SS"
309
+
306
310
  Example:
311
+ g --cmd="echo hello" --cmd-interval=5 # 每5秒执行一次echo hello
312
+ g --cmd="echo at-time" --at=23:59 # 在23:59执行一次echo at-time
313
+ g --cmd="echo now" # 立即执行一次echo now
314
+ g --cmd="echo hi" --cmd-interval=10 --interval=60 # cmd和git自动提交并行
307
315
  g -m "Initial commit" Commit with a custom message
308
316
  g -m=Fix-bug Commit with a custom message (no spaces around '=')
309
317
  g -y Auto commit with the default message