gitdone-agent 0.1.0 → 0.1.2
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/index.js +57 -10
- package/package.json +15 -15
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// gitdone-agent — watches a local git repo and pushes snapshots to gitdone.eu
|
|
3
3
|
// Usage: bun index.js --key=gdo_xxx --name=my-repo --path=/path/to/repo [--install]
|
|
4
4
|
|
|
5
|
-
import { execSync } from 'node:child_process'
|
|
5
|
+
import { execSync, spawn } from 'node:child_process'
|
|
6
6
|
import { existsSync, writeFileSync, unlinkSync } from 'node:fs'
|
|
7
7
|
import { resolve, join } from 'node:path'
|
|
8
8
|
import { homedir } from 'node:os'
|
|
@@ -46,14 +46,14 @@ function getStartupDir() {
|
|
|
46
46
|
return join(homedir(), 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup')
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
function
|
|
50
|
-
try { return execSync('where
|
|
51
|
-
try { return execSync('which
|
|
52
|
-
return
|
|
49
|
+
function getNodePath() {
|
|
50
|
+
try { return execSync('where node', { encoding: 'utf8' }).trim().split('\n')[0].trim() } catch {}
|
|
51
|
+
try { return execSync('which node', { encoding: 'utf8' }).trim() } catch {}
|
|
52
|
+
return process.execPath
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
function installStartup({ name, key, path: repoPath, url, interval, projectId }) {
|
|
56
|
-
const
|
|
56
|
+
const nodePath = getNodePath()
|
|
57
57
|
const agentPath = resolve(process.argv[1])
|
|
58
58
|
const startupDir = getStartupDir()
|
|
59
59
|
|
|
@@ -69,7 +69,7 @@ function installStartup({ name, key, path: repoPath, url, interval, projectId })
|
|
|
69
69
|
// VBScript runs the agent silently — no console window pops up on login
|
|
70
70
|
const vbs = [
|
|
71
71
|
'Set WshShell = CreateObject("WScript.Shell")',
|
|
72
|
-
`WshShell.Run """${
|
|
72
|
+
`WshShell.Run """${nodePath}"" ""${agentPath}"" ${argLine}", 0, False`,
|
|
73
73
|
].join('\r\n')
|
|
74
74
|
|
|
75
75
|
const vbsPath = join(startupDir, `gitdone-${name}.vbs`)
|
|
@@ -148,9 +148,42 @@ function getSnapshot(repoPath) {
|
|
|
148
148
|
return { branch, modified, staged, aheadBy, behindBy, lastCommit, statuses, diffs }
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
// ─── Push to API
|
|
151
|
+
// ─── Push to API & execute commands ─────────────────────────────────────────
|
|
152
152
|
|
|
153
|
-
async function
|
|
153
|
+
async function reportCommandResult(url, key, id, status, result) {
|
|
154
|
+
await fetch(`${url}/api/v1/agent/command-result`, {
|
|
155
|
+
method: 'POST',
|
|
156
|
+
headers: { 'Authorization': `Bearer ${key}`, 'Content-Type': 'application/json' },
|
|
157
|
+
body: JSON.stringify({ id, status, result }),
|
|
158
|
+
}).catch(() => {})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function executeCommand(cmd, repoPath, url, key) {
|
|
162
|
+
let result = 'ok'
|
|
163
|
+
let status = 'done'
|
|
164
|
+
try {
|
|
165
|
+
if (cmd.type === 'commit') {
|
|
166
|
+
const msg = (cmd.payload?.message ?? 'commit').replace(/"/g, '\\"')
|
|
167
|
+
git('git add -A', repoPath)
|
|
168
|
+
const out = git(`git commit -m "${msg}"`, repoPath)
|
|
169
|
+
result = out || 'committed'
|
|
170
|
+
} else if (cmd.type === 'push') {
|
|
171
|
+
const out = git('git push', repoPath)
|
|
172
|
+
result = out || 'pushed'
|
|
173
|
+
} else if (cmd.type === 'pull') {
|
|
174
|
+
const out = git('git pull', repoPath)
|
|
175
|
+
result = out || 'pulled'
|
|
176
|
+
}
|
|
177
|
+
console.log(`[${new Date().toLocaleTimeString()}] ✓ ${cmd.type}: ${result}`)
|
|
178
|
+
} catch (err) {
|
|
179
|
+
status = 'error'
|
|
180
|
+
result = err.message
|
|
181
|
+
console.error(`[${new Date().toLocaleTimeString()}] ✗ ${cmd.type} failed: ${result}`)
|
|
182
|
+
}
|
|
183
|
+
await reportCommandResult(url, key, cmd.id, status, result)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function push({ url, key, name, projectId, repoPath, snapshot }) {
|
|
154
187
|
const res = await fetch(`${url}/api/v1/agent/snapshot`, {
|
|
155
188
|
method: 'POST',
|
|
156
189
|
headers: { 'Authorization': `Bearer ${key}`, 'Content-Type': 'application/json' },
|
|
@@ -160,6 +193,10 @@ async function push({ url, key, name, projectId, snapshot }) {
|
|
|
160
193
|
const text = await res.text().catch(() => '')
|
|
161
194
|
throw new Error(`HTTP ${res.status}: ${text}`)
|
|
162
195
|
}
|
|
196
|
+
const data = await res.json().catch(() => ({}))
|
|
197
|
+
for (const cmd of data.commands ?? []) {
|
|
198
|
+
await executeCommand(cmd, repoPath, url, key)
|
|
199
|
+
}
|
|
163
200
|
}
|
|
164
201
|
|
|
165
202
|
// ─── Main ────────────────────────────────────────────────────────────────────
|
|
@@ -175,6 +212,16 @@ async function main() {
|
|
|
175
212
|
|
|
176
213
|
if (install) {
|
|
177
214
|
installStartup(config)
|
|
215
|
+
|
|
216
|
+
// Launch the agent silently in the background right now via wscript.exe
|
|
217
|
+
const vbsPath = join(getStartupDir(), `gitdone-${name}.vbs`)
|
|
218
|
+
const child = spawn('wscript.exe', [vbsPath], { detached: true, stdio: 'ignore' })
|
|
219
|
+
child.unref()
|
|
220
|
+
|
|
221
|
+
console.log()
|
|
222
|
+
console.log('✓ Агентът е стартиран в заден план — можеш да затвориш прозореца.')
|
|
223
|
+
console.log(' При следващо влизане в Windows ще стартира автоматично.')
|
|
224
|
+
process.exit(0)
|
|
178
225
|
}
|
|
179
226
|
|
|
180
227
|
if (!isGitRepo(repoPath)) {
|
|
@@ -194,7 +241,7 @@ async function main() {
|
|
|
194
241
|
async function tick() {
|
|
195
242
|
try {
|
|
196
243
|
const snapshot = getSnapshot(repoPath)
|
|
197
|
-
await push({ url, key, name, projectId, snapshot })
|
|
244
|
+
await push({ url, key, name, projectId, repoPath, snapshot })
|
|
198
245
|
const changed = snapshot.modified.length + snapshot.staged.length
|
|
199
246
|
console.log(`[${new Date().toLocaleTimeString()}] ✓ pushed — branch: ${snapshot.branch}, changes: ${changed}`)
|
|
200
247
|
} catch (err) {
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "gitdone-agent",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Local git agent for gitdone — watches a local repo and sends snapshots to gitdone.eu",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"gitdone-agent": "index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node index.js"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": "\u003e=18"
|
|
14
|
+
}
|
|
15
|
+
}
|