-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-git.cjs
More file actions
150 lines (119 loc) · 3.39 KB
/
Copy pathauto-git.cjs
File metadata and controls
150 lines (119 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const chokidar = require("chokidar");
const simpleGit = require("simple-git");
const git = simpleGit();
// ================= CONFIG =================
const PROJECT_PATH = process.cwd();
const AUTO_PUSH = true; // toggle push
const DEBOUNCE_TIME = 60000; // 60 seconds
// ================= STATE =================
let timer = null;
let isProcessing = false;
// ================= UTIL =================
function getTimeStamp() {
return new Date().toISOString().replace("T", " ").substring(0, 19);
}
// ================= SMART MESSAGE GENERATOR =================
async function generateSmartMessage(status) {
const files = [
...status.modified,
...status.created,
...status.deleted
];
if (files.length === 0) {
return "chore: minor updates";
}
const hasFrontend = files.some(f =>
f.includes(".html") ||
f.includes(".css") ||
f.includes(".jsx") ||
f.includes(".tsx") ||
f.includes(".vue")
);
const hasBackend = files.some(f =>
f.includes(".js") ||
f.includes(".ts") ||
f.includes("server") ||
f.includes("api") ||
f.includes("controller") ||
f.includes("routes")
);
const hasConfig = files.some(f =>
f.includes(".json") ||
f.includes(".env") ||
f.includes("config") ||
f.includes(".yml")
);
const hasDocs = files.some(f =>
f.includes(".md") ||
f.includes("README")
);
let message = "";
if (hasFrontend && hasBackend) {
message = "feat: update fullstack changes";
} else if (hasFrontend) {
message = "ui: update frontend UI and styles";
} else if (hasBackend) {
message = "fix: update backend logic and APIs";
} else if (hasConfig) {
message = "chore: update configuration files";
} else if (hasDocs) {
message = "docs: update documentation";
} else {
message = "refactor: update project files";
}
// Add file context if small change
if (files.length <= 3) {
message += ` (${files.join(", ")})`;
}
return message;
}
// ================= MAIN GIT FLOW =================
async function runGitAutoCommit() {
try {
if (isProcessing) return;
isProcessing = true;
console.log("\n🔍 Checking git status...");
const status = await git.status();
if (status.isClean()) {
console.log("✅ No changes detected. Skipping commit.");
isProcessing = false;
return;
}
console.log("📦 Changes detected. Preparing smart commit...");
await git.add(".");
const commitMessage = await generateSmartMessage(status);
await git.commit(commitMessage);
console.log(`📝 Smart Commit: ${commitMessage}`);
if (AUTO_PUSH) {
console.log("🚀 Pushing to remote...");
await git.push("origin", "main");
console.log("✅ Pushed successfully!");
}
console.log("🎉 Cycle complete.\n");
} catch (err) {
console.error("❌ Error:", err.message);
} finally {
isProcessing = false;
}
}
// ================= FILE WATCHER =================
console.log("👀 Smart Git Auto-Commit Agent Running...");
const watcher = chokidar.watch(PROJECT_PATH, {
ignored: [
/node_modules/,
/\.git/,
/dist/,
/build/,
/out/
],
persistent: true,
ignoreInitial: true
});
watcher.on("all", (event, filePath) => {
console.log(`📄 Change detected: ${filePath}`);
// debounce logic
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
runGitAutoCommit();
}, DEBOUNCE_TIME);
});