|
1 |
| -const exec = require('child_process').exec; |
| 1 | +const { execSync } = require("child_process"); |
2 | 2 |
|
3 |
| -let now = dateFormatter(); |
| 3 | +function executeGitPush() { |
| 4 | + const now = formatDateTime(); |
4 | 5 |
|
5 |
| -execute( |
6 |
| - ` |
7 |
| - git add . |
8 |
| - git commit -m 'updated at ${now}' |
9 |
| - git push |
10 |
| - ` |
11 |
| -); |
| 6 | + try { |
| 7 | + // 根据操作系统选择命令格式 |
| 8 | + const isWindows = process.platform === "win32"; |
| 9 | + const command = isWindows |
| 10 | + ? `git add . && git commit -m "Updated at ${now}" && git push` |
| 11 | + : `git add .; git commit -m "Updated at ${now}"; git push`; |
12 | 12 |
|
13 |
| -function execute(cmd) { |
14 |
| - exec(cmd, function (error, stdout, stderr) { |
15 |
| - if (error) { |
16 |
| - console.error(error); |
17 |
| - } else { |
18 |
| - console.log(`SUCCESS PUSH: updated at ${now}`); |
19 |
| - } |
20 |
| - }); |
21 |
| -} |
| 13 | + execSync(command, { |
| 14 | + stdio: "inherit", |
| 15 | + shell: isWindows ? process.env.ComSpec : "/bin/bash", |
| 16 | + }); |
22 | 17 |
|
23 |
| -function dateFormatter(fmt = 'YYYY-MM-DD hh:mm:ss') { |
24 |
| - try { |
25 |
| - const date = new Date(); |
26 |
| - let ret; |
27 |
| - let opt = { |
28 |
| - 'Y+': date.getFullYear().toString(), // 年 |
29 |
| - 'M+': (date.getMonth() + 1).toString(), // 月 |
30 |
| - 'D+': date.getDate().toString(), // 日 |
31 |
| - 'h+': date.getHours().toString(), // 时 |
32 |
| - 'm+': date.getMinutes().toString(), // 分 |
33 |
| - 's+': date.getSeconds().toString(), // 秒 |
34 |
| - // 有其他格式化字符需求可以继续添加,必须转化成字符串 |
35 |
| - }; |
36 |
| - for (let k in opt) { |
37 |
| - ret = new RegExp('(' + k + ')').exec(fmt); |
38 |
| - if (ret) { |
39 |
| - fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')); |
40 |
| - } |
41 |
| - } |
42 |
| - return fmt; |
| 18 | + console.log(`\nSUCCESS: Pushed changes at ${now}`); |
43 | 19 | } catch (error) {
|
44 |
| - return ''; |
| 20 | + console.error("\nERROR: Push failed. Possible reasons:"); |
| 21 | + console.error("- No changes to commit"); |
| 22 | + console.error("- Network issues"); |
| 23 | + console.error("- Authentication problems"); |
| 24 | + process.exit(1); |
45 | 25 | }
|
46 | 26 | }
|
| 27 | + |
| 28 | +function formatDateTime(format = "YYYY-MM-DD HH:mm:ss") { |
| 29 | + const pad = (n, len) => String(n).padStart(len, "0"); |
| 30 | + const date = new Date(); |
| 31 | + |
| 32 | + return format |
| 33 | + .replace(/YYYY/g, pad(date.getFullYear(), 4)) |
| 34 | + .replace(/MM/g, pad(date.getMonth() + 1, 2)) |
| 35 | + .replace(/DD/g, pad(date.getDate(), 2)) |
| 36 | + .replace(/HH/g, pad(date.getHours(), 2)) |
| 37 | + .replace(/mm/g, pad(date.getMinutes(), 2)) |
| 38 | + .replace(/ss/g, pad(date.getSeconds(), 2)); |
| 39 | +} |
| 40 | + |
| 41 | +// 执行主函数 |
| 42 | +executeGitPush(); |
0 commit comments