@mohshomis/ckpt 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.
- package/dist/cli.js +54 -3
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from 'commander';
|
|
3
3
|
import path from 'path';
|
|
4
|
+
import { spawn } from 'child_process';
|
|
5
|
+
import fs from 'fs';
|
|
4
6
|
import * as core from './core.js';
|
|
5
7
|
import { startWatch } from './watch.js';
|
|
6
8
|
const program = new Command();
|
|
7
9
|
program
|
|
8
10
|
.name('ckpt')
|
|
9
11
|
.description('Automatic checkpoints for AI coding sessions. Per-step undo on top of git.')
|
|
10
|
-
.version('0.1.
|
|
12
|
+
.version('0.1.1')
|
|
11
13
|
.option('-C, --path <dir>', 'Run as if ckpt was started in <dir>');
|
|
12
14
|
function getCwd() {
|
|
13
15
|
const opts = program.opts();
|
|
@@ -16,10 +18,59 @@ function getCwd() {
|
|
|
16
18
|
// ── watch ─────────────────────────────────────────────────────────────────
|
|
17
19
|
program
|
|
18
20
|
.command('watch')
|
|
19
|
-
.description('Auto-snapshot file changes
|
|
21
|
+
.description('Auto-snapshot file changes in the background — returns control immediately')
|
|
20
22
|
.option('-d, --debounce <ms>', 'Quiet period before snapshotting (ms)', '2000')
|
|
23
|
+
.option('--foreground', 'Run in foreground instead of background (blocks terminal)')
|
|
21
24
|
.action((opts) => {
|
|
22
|
-
|
|
25
|
+
if (opts.foreground) {
|
|
26
|
+
startWatch(getCwd(), parseInt(opts.debounce));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// Spawn as a detached background process
|
|
30
|
+
const cwd = getCwd();
|
|
31
|
+
const ckptBin = process.argv[1];
|
|
32
|
+
const args = ['watch', '--foreground', '-d', opts.debounce, '-C', cwd];
|
|
33
|
+
const logFile = path.join(cwd, '.ckpt', 'watch.log');
|
|
34
|
+
fs.mkdirSync(path.join(cwd, '.ckpt'), { recursive: true });
|
|
35
|
+
const out = fs.openSync(logFile, 'a');
|
|
36
|
+
const child = spawn(process.execPath, [ckptBin, ...args], {
|
|
37
|
+
detached: true,
|
|
38
|
+
stdio: ['ignore', out, out],
|
|
39
|
+
cwd,
|
|
40
|
+
});
|
|
41
|
+
// Save PID so we can stop it later
|
|
42
|
+
fs.writeFileSync(path.join(cwd, '.ckpt', 'watch.pid'), String(child.pid), 'utf8');
|
|
43
|
+
child.unref();
|
|
44
|
+
// Start session if not already active
|
|
45
|
+
let session = core.status(cwd);
|
|
46
|
+
if (!session) {
|
|
47
|
+
session = core.startSession(cwd);
|
|
48
|
+
}
|
|
49
|
+
console.log(`⚡ ckpt watching in background (pid ${child.pid})`);
|
|
50
|
+
console.log(` Session: ${session.id}`);
|
|
51
|
+
console.log(` Log: ${logFile}`);
|
|
52
|
+
console.log(` Run "ckpt stop" to stop watching.\n`);
|
|
53
|
+
});
|
|
54
|
+
// ── stop ──────────────────────────────────────────────────────────────────
|
|
55
|
+
program
|
|
56
|
+
.command('stop')
|
|
57
|
+
.description('Stop the background watcher')
|
|
58
|
+
.action(() => {
|
|
59
|
+
const cwd = getCwd();
|
|
60
|
+
const pidFile = path.join(cwd, '.ckpt', 'watch.pid');
|
|
61
|
+
if (!fs.existsSync(pidFile)) {
|
|
62
|
+
console.log('No watcher running.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const pid = parseInt(fs.readFileSync(pidFile, 'utf8'));
|
|
66
|
+
try {
|
|
67
|
+
process.kill(pid, 'SIGTERM');
|
|
68
|
+
console.log(`⏹ Watcher stopped (pid ${pid}).`);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
console.log('Watcher was not running.');
|
|
72
|
+
}
|
|
73
|
+
fs.unlinkSync(pidFile);
|
|
23
74
|
});
|
|
24
75
|
// ── start ─────────────────────────────────────────────────────────────────
|
|
25
76
|
program
|