zen-gitsync 2.10.16 → 2.10.18

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
@@ -76,6 +76,7 @@ start /min cmd /k "g -y --path=你要同步的文件夹 --interval"
76
76
  ```shell
77
77
  start /min cmd /k "g --cmd=\"echo hello\" --cmd-interval=5" # 每5秒执行一次echo hello
78
78
  start /min cmd /k "g --cmd=\"echo at-time\" --at=23:59" # 在23:59执行一次echo at-time
79
+ start /min cmd /k "g --cmd=\"echo daily\" --at=23:59 --daily" # 每天23:59执行一次echo daily
79
80
  ```
80
81
 
81
82
  #### 不显示git diff内容
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "2.10.16",
3
+ "version": "2.10.18",
4
4
  "description": "一个 git 提交的工具",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/gitCommit.js CHANGED
@@ -208,34 +208,74 @@ async function main() {
208
208
  const intervalArg = process.argv.find(arg => arg.startsWith('--cmd-interval='));
209
209
 
210
210
  if (atArg) {
211
- // 定点执行
212
211
  const atMatch = atArg.match(/^--at=(['"]?)(.*)\1$/);
213
212
  const atTime = atMatch ? atMatch[2] : '';
214
- const now = new Date();
215
- let target;
216
- if (/^\d{2}:\d{2}$/.test(atTime)) {
217
- // 只给了时:分,今天的
218
- const [h, m] = atTime.split(':');
219
- target = new Date(now.getFullYear(), now.getMonth(), now.getDate(), h, m, 0);
220
- } else {
221
- target = new Date(atTime);
222
- }
223
- const delay = target - now;
224
- if (delay > 0) {
225
- console.log(`将在 ${target.toLocaleString()} 执行: ${cmd}`);
226
- setTimeout(() => {
227
- console.log(`\n[自定义命令执行] ${new Date().toLocaleString()}\n> ${cmd}`);
228
- exec(cmd, (err, stdout, stderr) => {
229
- if (err) {
230
- console.error(`[自定义命令错误]`, err.message);
231
- }
232
- if (stdout) console.log(`[自定义命令输出]\n${stdout}`);
233
- if (stderr) console.error(`[自定义命令错误输出]\n${stderr}`);
234
- });
213
+ const repeatDaily = process.argv.includes('--daily') || process.argv.includes('--repeat=daily') || process.argv.includes('--at-repeat=daily');
214
+
215
+ const runOnce = () => {
216
+ console.log(`\n[自定义命令执行] ${new Date().toLocaleString()}\n> ${cmd}`);
217
+ exec(cmd, (err, stdout, stderr) => {
218
+ if (err) {
219
+ console.error(`[自定义命令错误]`, err.message);
220
+ }
221
+ if (stdout) console.log(`[自定义命令输出]\n${stdout}`);
222
+ if (stderr) console.error(`[自定义命令错误输出]\n${stderr}`);
223
+ });
224
+ };
225
+
226
+ const getNextTarget = (now) => {
227
+ if (/^\d{2}:\d{2}$/.test(atTime)) {
228
+ const [h, m] = atTime.split(':').map((v) => parseInt(v, 10));
229
+ if (!Number.isFinite(h) || !Number.isFinite(m)) return null;
230
+ const base = new Date(now.getFullYear(), now.getMonth(), now.getDate(), h, m, 0, 0);
231
+ if (base.getTime() > now.getTime()) return base;
232
+ return new Date(base.getTime() + 24 * 60 * 60 * 1000);
233
+ }
234
+
235
+ const parsed = new Date(atTime);
236
+ if (!Number.isFinite(parsed.getTime())) return null;
237
+
238
+ if (!repeatDaily) return parsed;
239
+
240
+ const base = new Date(now.getFullYear(), now.getMonth(), now.getDate(), parsed.getHours(), parsed.getMinutes(), parsed.getSeconds(), 0);
241
+ if (base.getTime() > now.getTime()) return base;
242
+ return new Date(base.getTime() + 24 * 60 * 60 * 1000);
243
+ };
244
+
245
+ let atTimer = null;
246
+ const scheduleNext = () => {
247
+ const now = new Date();
248
+ const target = getNextTarget(now);
249
+ if (!target) {
250
+ console.error('无效的时间参数');
251
+ return;
252
+ }
253
+
254
+ let delay = target.getTime() - now.getTime();
255
+ if (!Number.isFinite(delay)) {
256
+ console.error('无效的时间参数');
257
+ return;
258
+ }
259
+
260
+ if (!repeatDaily && delay <= 0) {
261
+ console.log('指定时间已过,不执行自定义命令');
262
+ return;
263
+ }
264
+
265
+ if (delay < 0) delay = 0;
266
+ console.log(`将在 ${target.toLocaleString()} 执行: ${cmd}${repeatDaily ? '(每日循环)' : ''}`);
267
+ atTimer = setTimeout(() => {
268
+ runOnce();
269
+ if (repeatDaily) scheduleNext();
235
270
  }, delay);
236
- } else {
237
- console.log('指定时间已过,不执行自定义命令');
238
- }
271
+ };
272
+
273
+ scheduleNext();
274
+
275
+ process.on('SIGINT', () => {
276
+ if (atTimer) clearTimeout(atTimer);
277
+ process.exit();
278
+ });
239
279
  } else if (intervalArg) {
240
280
  // 定时循环执行
241
281
  const intervalMatch = intervalArg.match(/^--cmd-interval=(['"]?)(.*)\1$/);