gitdone-agent 0.1.1 → 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.
Files changed (2) hide show
  1. package/index.js +51 -4
  2. package/package.json +1 -1
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'
@@ -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 push({ url, key, name, projectId, snapshot }) {
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,6 +1,6 @@
1
1
  {
2
2
  "name": "gitdone-agent",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Local git agent for gitdone — watches a local repo and sends snapshots to gitdone.eu",
5
5
  "type": "module",
6
6
  "bin": {