Skip to content

Commit 74be1e1

Browse files
author
Shaowei SW5 Guo
committed
Updated at 2025-02-21 10:59:59
1 parent 074b104 commit 74be1e1

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

push.js

+35-39
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
1-
const exec = require('child_process').exec;
1+
const { execSync } = require("child_process");
22

3-
let now = dateFormatter();
3+
function executeGitPush() {
4+
const now = formatDateTime();
45

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`;
1212

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+
});
2217

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}`);
4319
} 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);
4525
}
4626
}
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

Comments
 (0)