trunkguard 0.1.0 → 0.1.1

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 (3) hide show
  1. package/README.md +6 -0
  2. package/package.json +1 -1
  3. package/src/proc.js +51 -48
package/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  **Let AI agents push straight to `main`. Only tested commits reach your server — and a bad deploy rolls itself back.**
4
4
 
5
+ [![npm](https://img.shields.io/npm/v/trunkguard)](https://www.npmjs.com/package/trunkguard) ![zero dependencies](https://img.shields.io/badge/dependencies-0-brightgreen) ![node](https://img.shields.io/node/v/trunkguard)
6
+
7
+ ![trunkguard demo: the robot stops a commit that breaks the tests; serve rolls back a commit that crashes on start](docs/demo.svg)
8
+
9
+ <sub>Replay of a real run (`scripts/demo-svg.js`): one red commit never leaves `main`, one green-but-broken commit is rolled back in about a second.</sub>
10
+
5
11
  Coding agents (Claude Code, Codex, Cursor, Aider…) commit fast and often. Pull-request review doesn't keep up,
6
12
  and "just push to main" means the first broken commit takes your app down. trunkguard sits between `main` and
7
13
  the running app:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trunkguard",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Release gate for projects where AI coding agents push to main: only commits that pass your tests reach the live server, bad deploys roll back on their own.",
5
5
  "homepage": "https://github.com/nogcar421-byte/trunkguard#readme",
6
6
  "repository": {
package/src/proc.js CHANGED
@@ -1,48 +1,51 @@
1
- 'use strict';
2
- const { spawn, spawnSync } = require('child_process');
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- const WIN = process.platform === 'win32';
7
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
8
- const short = (sha) => String(sha || '').slice(0, 7);
9
-
10
- function git(cwd, ...args) {
11
- const r = spawnSync('git', args, { cwd, encoding: 'utf8', windowsHide: true, maxBuffer: 64 * 1024 * 1024 });
12
- return { ok: r.status === 0 && !r.error, out: String(r.stdout || '').trim(), err: String(r.stderr || (r.error && r.error.message) || '').trim() };
13
- }
14
- const revParse = (cwd, ref) => { const r = git(cwd, 'rev-parse', '--verify', '-q', `${ref}^{commit}`); return r.ok ? r.out : ''; };
15
- const isAncestor = (cwd, a, b) => git(cwd, 'merge-base', '--is-ancestor', a, b).ok;
16
- const changedFiles = (cwd, a, b) => git(cwd, 'diff', '--name-only', a, b).out.split('\n').filter(Boolean);
17
-
18
- // run a shell command to completion, output captured (and optionally written to a file)
19
- function sh(cmd, { cwd, timeoutMs = 0, env, logFile } = {}) {
20
- const t0 = Date.now();
21
- const r = spawnSync(cmd, { cwd, shell: true, encoding: 'utf8', windowsHide: true, timeout: timeoutMs || undefined,
22
- maxBuffer: 256 * 1024 * 1024, env: { ...process.env, ...(env || {}) } });
23
- const out = `${r.stdout || ''}${r.stderr || ''}${r.error ? `\n${r.error.message}` : ''}`;
24
- if (logFile) { fs.mkdirSync(path.dirname(logFile), { recursive: true }); fs.writeFileSync(logFile, `$ ${cmd}\n${out}`); }
25
- return { ok: r.status === 0 && !r.error, code: r.status, timedOut: !!(r.error && r.error.code === 'ETIMEDOUT'), out, sec: Math.round((Date.now() - t0) / 1000) };
26
- }
27
-
28
- // long-running app process; own process group on POSIX so the whole tree can be stopped
29
- function start(cmd, { cwd, env } = {}) {
30
- return spawn(cmd, { cwd, shell: true, stdio: 'inherit', windowsHide: true, detached: !WIN, env: { ...process.env, ...(env || {}) } });
31
- }
32
-
33
- const exited = (child) => child.exitCode !== null || child.signalCode !== null;
34
-
35
- // stop the app and everything it spawned: polite first, forced after timeoutMs
36
- async function stopTree(child, timeoutMs = 15000) {
37
- if (!child || exited(child)) return;
38
- const done = new Promise((r) => child.once('exit', r));
39
- if (WIN) spawnSync('taskkill', ['/PID', String(child.pid), '/T'], { windowsHide: true });
40
- else { try { process.kill(-child.pid, 'SIGTERM'); } catch { /* already gone */ } }
41
- const graceful = await Promise.race([done.then(() => true), sleep(timeoutMs).then(() => false)]);
42
- if (graceful || exited(child)) return;
43
- if (WIN) spawnSync('taskkill', ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true });
44
- else { try { process.kill(-child.pid, 'SIGKILL'); } catch { /* already gone */ } }
45
- await Promise.race([done, sleep(5000)]);
46
- }
47
-
48
- module.exports = { WIN, sleep, short, git, revParse, isAncestor, changedFiles, sh, start, stopTree, exited };
1
+ 'use strict';
2
+ const { spawn, spawnSync } = require('child_process');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ const WIN = process.platform === 'win32';
7
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
8
+ const short = (sha) => String(sha || '').slice(0, 7);
9
+
10
+ function git(cwd, ...args) {
11
+ const r = spawnSync('git', args, { cwd, encoding: 'utf8', windowsHide: true, maxBuffer: 64 * 1024 * 1024 });
12
+ return { ok: r.status === 0 && !r.error, out: String(r.stdout || '').trim(), err: String(r.stderr || (r.error && r.error.message) || '').trim() };
13
+ }
14
+ const revParse = (cwd, ref) => { const r = git(cwd, 'rev-parse', '--verify', '-q', `${ref}^{commit}`); return r.ok ? r.out : ''; };
15
+ const isAncestor = (cwd, a, b) => git(cwd, 'merge-base', '--is-ancestor', a, b).ok;
16
+ const changedFiles = (cwd, a, b) => git(cwd, 'diff', '--name-only', a, b).out.split('\n').filter(Boolean);
17
+
18
+ // run a shell command to completion, output captured (and optionally written to a file)
19
+ function sh(cmd, { cwd, timeoutMs = 0, env, logFile } = {}) {
20
+ const t0 = Date.now();
21
+ const r = spawnSync(cmd, { cwd, shell: true, encoding: 'utf8', windowsHide: true, timeout: timeoutMs || undefined,
22
+ maxBuffer: 256 * 1024 * 1024, env: { ...process.env, ...(env || {}) } });
23
+ const out = `${r.stdout || ''}${r.stderr || ''}${r.error ? `\n${r.error.message}` : ''}`;
24
+ if (logFile) { fs.mkdirSync(path.dirname(logFile), { recursive: true }); fs.writeFileSync(logFile, `$ ${cmd}\n${out}`); }
25
+ return { ok: r.status === 0 && !r.error, code: r.status, timedOut: !!(r.error && r.error.code === 'ETIMEDOUT'), out, sec: Math.round((Date.now() - t0) / 1000) };
26
+ }
27
+
28
+ // long-running app process; own process group on POSIX so the whole tree can be stopped
29
+ function start(cmd, { cwd, env } = {}) {
30
+ return spawn(cmd, { cwd, shell: true, stdio: 'inherit', windowsHide: true, detached: !WIN, env: { ...process.env, ...(env || {}) } });
31
+ }
32
+
33
+ const exited = (child) => child.exitCode !== null || child.signalCode !== null;
34
+
35
+ // stop the app and everything it spawned: polite first, forced after timeoutMs
36
+ async function stopTree(child, timeoutMs = 15000) {
37
+ if (!child || exited(child)) return;
38
+ const done = new Promise((r) => child.once('exit', r));
39
+ // Windows: console apps (node, python…) refuse a plain taskkill at once ("can only be terminated forcefully");
40
+ // waiting out the timeout then would only add downtime to every deploy
41
+ let refused = false;
42
+ if (WIN) refused = spawnSync('taskkill', ['/PID', String(child.pid), '/T'], { windowsHide: true }).status !== 0;
43
+ else { try { process.kill(-child.pid, 'SIGTERM'); } catch { /* already gone */ } }
44
+ const graceful = refused ? false : await Promise.race([done.then(() => true), sleep(timeoutMs).then(() => false)]);
45
+ if (graceful || exited(child)) return;
46
+ if (WIN) spawnSync('taskkill', ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true });
47
+ else { try { process.kill(-child.pid, 'SIGKILL'); } catch { /* already gone */ } }
48
+ await Promise.race([done, sleep(5000)]);
49
+ }
50
+
51
+ module.exports = { WIN, sleep, short, git, revParse, isAncestor, changedFiles, sh, start, stopTree, exited };