-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathinstall.js
More file actions
executable file
Β·1226 lines (1118 loc) Β· 58.6 KB
/
Copy pathinstall.js
File metadata and controls
executable file
Β·1226 lines (1118 loc) Β· 58.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
// caveman β unified cross-platform installer.
//
// One Node script replaces the old install.sh + install.ps1 + src/hooks/install.sh
// + src/hooks/install.ps1 quartet. Single source of truth. Works on macOS, Linux,
// and Windows (PowerShell or cmd) without any of the bash/PS1 quoting bugs
// that previously broke the JSON merge step (issue #249).
//
// Distribution:
// Local clone: node bin/install.js [flags]
// curl|bash: delegated from install.sh shim β npx -y github:JuliusBrussee/caveman -- [flags]
// Windows: pwsh install.ps1 [flags] β same npx delegation
//
// Pure stdlib, zero npm runtime deps.
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const child_process = require('child_process');
const readline = require('readline');
const SETTINGS = require('./lib/settings');
const OPENCLAW = require('./lib/openclaw');
const { stripOpencodeAgentTools } = require('./lib/opencode-agent');
const REPO = 'JuliusBrussee/caveman';
const RAW_BASE = `https://raw.githubusercontent.com/${REPO}/main`;
const HOOKS_REMOTE = `${RAW_BASE}/src/hooks`;
const INIT_SCRIPT_URL = `${RAW_BASE}/src/tools/caveman-init.js`;
const MCP_SHRINK_PKG = 'caveman-shrink';
// Hook files to copy. Statusline ships in both .sh (macOS/Linux) and .ps1
// (Windows) flavors β copy both regardless of host OS so a roaming
// $CLAUDE_CONFIG_DIR (e.g. dotfiles repo) keeps working across platforms.
const HOOK_FILES = [
'package.json',
'caveman-config.js',
'caveman-activate.js',
'caveman-mode-tracker.js',
'caveman-stats.js',
'caveman-statusline.sh',
'caveman-statusline.ps1',
];
// ββ Argv βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function parseArgs(argv) {
const opts = {
dryRun: false, force: false, skipSkills: false,
withHooks: 'auto', withInit: false, withMcpShrink: 'auto',
all: false, minimal: false, listOnly: false, noColor: false,
only: [], uninstall: false, nonInteractive: false,
configDir: null, help: false,
};
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
switch (a) {
case '--dry-run': opts.dryRun = true; break;
case '--force': opts.force = true; break;
case '--skip-skills': opts.skipSkills = true; break;
case '--with-hooks': opts.withHooks = true; break;
case '--no-hooks': opts.withHooks = false; break;
case '--with-init': opts.withInit = true; break;
case '--with-mcp-shrink': opts.withMcpShrink = true; break;
case '--no-mcp-shrink': opts.withMcpShrink = false; break;
case '--all': opts.all = true; break;
case '--minimal': opts.minimal = true; break;
case '--list': opts.listOnly = true; break;
case '--no-color': opts.noColor = true; break;
case '--uninstall': case '-u': opts.uninstall = true; break;
case '--non-interactive': opts.nonInteractive = true; break;
case '-h': case '--help': opts.help = true; break;
// POSIX end-of-options marker. Older curl|bash flows pipe `-- --only foo`
// through npx; some npx versions forward the literal `--`. Accept and
// ignore so we never regress on the headline install command.
case '--': break;
case '--only': {
const v = argv[++i];
if (!v) die('error: --only requires an argument');
opts.only.push(v === 'aider' ? 'aider-desk' : v);
break;
}
case '--config-dir': {
const v = argv[++i];
if (!v || v.startsWith('--')) die('error: --config-dir requires a path');
opts.configDir = expandHome(v);
break;
}
default:
die(`error: unknown flag: ${a}\nrun 'caveman --help' for usage`);
}
}
if (opts.all && opts.minimal) die('error: --all and --minimal are mutually exclusive');
if (opts.all) { opts.withHooks = true; opts.withInit = true; opts.withMcpShrink = true; }
if (opts.minimal) { opts.withHooks = false; opts.withInit = false; opts.withMcpShrink = false; }
if (opts.withHooks === 'auto') opts.withHooks = true;
if (opts.withMcpShrink === 'auto') opts.withMcpShrink = true;
// Validate --only ids against the provider matrix. PROVIDERS is defined later
// in the file but is in scope by the time this function runs.
if (opts.only.length) {
const knownIds = new Set(PROVIDERS.map(p => p.id));
for (const id of opts.only) {
if (!knownIds.has(id)) {
die(`error: unknown agent: ${id}\n see 'caveman --list' for valid ids`);
}
}
}
return opts;
}
function die(msg) { process.stderr.write(msg + '\n'); process.exit(2); }
// ββ Color helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function makeChalk(noColor) {
const useColor = !noColor && process.stdout.isTTY && !process.env.NO_COLOR;
const wrap = (codes) => (s) => useColor ? `\x1b[${codes}m${s}\x1b[0m` : s;
return {
orange: wrap('38;5;172'), dim: wrap('2'), red: wrap('31'),
green: wrap('32'), yellow: wrap('33'),
};
}
// ββ Env guards βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function checkWslWindowsNode() {
if (process.platform !== 'win32') return;
// Windows-Node executing inside WSL has homedir like /mnt/c/Users/... which
// breaks every config-dir resolution. Detect and abort with a clear hint.
if (process.env.WSL_DISTRO_NAME) {
die('caveman: detected Windows Node.js running inside WSL.\n' +
' Install Linux-native Node inside your WSL distro and re-run there.\n' +
' (WSL_DISTRO_NAME=' + process.env.WSL_DISTRO_NAME + ')');
}
try {
const v = fs.readFileSync('/proc/version', 'utf8').toLowerCase();
if (v.includes('microsoft') || v.includes('wsl')) {
die('caveman: detected Windows Node.js running inside WSL (/proc/version).\n' +
' Install Linux-native Node inside your WSL distro and re-run there.');
}
} catch (_) { /* /proc/version absent on real Windows β fine */ }
}
function checkNodeVersion() {
const major = parseInt(process.versions.node.split('.')[0], 10);
if (major < 18) die(`caveman: Node ${process.versions.node} too old. Need Node β₯18. https://nodejs.org`);
}
// ββ Provider matrix ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Single source of truth. Replaces the 6 parallel bash arrays in old install.sh.
//
// Detection rules:
// - `command:<bin>` β bin on PATH. Most reliable signal.
// - `vscode-ext:<needle>` / `cursor-ext:<needle>` β extension dir name match.
// - `jetbrains-plugin:<needle>` β JetBrains plugin dir match.
// - `dir:<path>` / `file:<path>` β kept ONLY for agents that ship no CLI
// and no extension marker (true dir-only signal).
//
// `soft: true` means detection is best-effort (config-dir only or no
// reliable probe). Soft providers are EXCLUDED from auto-detect and only
// install when the user passes `--only <id>`. This stops the installer from
// firing `npx skills add ...` against agents the user has never installed
// just because some other tool created `~/.foo` along the way.
const PROVIDERS = [
{ id: 'claude', label: 'Claude Code', mech: 'claude plugin install', detect: 'command:claude' },
{ id: 'gemini', label: 'Gemini CLI', mech: 'gemini extensions install', detect: 'command:gemini' },
{ id: 'opencode', label: 'opencode', mech: 'native opencode plugin', detect: 'command:opencode' },
{ id: 'openclaw', label: 'OpenClaw', mech: 'workspace skill + SOUL.md', detect: 'command:openclaw||dir:$HOME/.openclaw/workspace' },
{ id: 'codex', label: 'Codex CLI', mech: 'npx skills add (codex)', detect: 'command:codex', profile: 'codex' },
// IDE / VS Code-family β extension probes are precise. Cursor/Windsurf also
// ship CLI binaries; we drop the dir fallback because the dir lingers after
// uninstall and false-positives heavily.
{ id: 'cursor', label: 'Cursor', mech: 'npx skills add (cursor)', detect: 'command:cursor||macapp:Cursor', profile: 'cursor' },
{ id: 'windsurf', label: 'Windsurf', mech: 'npx skills add (windsurf)', detect: 'command:windsurf||macapp:Windsurf', profile: 'windsurf' },
{ id: 'cline', label: 'Cline', mech: 'npx skills add (cline)', detect: 'vscode-ext:cline', profile: 'cline' },
{ id: 'continue', label: 'Continue', mech: 'npx skills add (continue)', detect: 'vscode-ext:continue.continue||vscode-ext:continue', profile: 'continue' },
{ id: 'kilo', label: 'Kilo Code', mech: 'npx skills add (kilo)', detect: 'vscode-ext:kilocode', profile: 'kilo' },
{ id: 'roo', label: 'Roo Code', mech: 'npx skills add (roo)', detect: 'vscode-ext:roo||vscode-ext:rooveterinaryinc.roo-cline||cursor-ext:roo', profile: 'roo' },
{ id: 'augment', label: 'Augment Code', mech: 'npx skills add (augment)', detect: 'vscode-ext:augment||jetbrains-plugin:augment', profile: 'augment' },
// GitHub Copilot β `gh` (GitHub CLI) is on most dev machines but isn't
// Copilot. There's no reliable always-on Copilot probe (subscription state
// is auth-gated). Mark soft β opt-in via --only copilot.
{ id: 'copilot', label: 'GitHub Copilot', mech: 'npx skills add (github-copilot)', detect: 'command:copilot', profile: 'github-copilot', soft: true },
// CLI agents β require the binary. The `||dir:~/.foo` fallbacks were the
// main source of false positives (warp, kiro, junie etc. leave config dirs
// behind on uninstall).
{ id: 'aider-desk', label: 'Aider Desk', mech: 'npx skills add (aider-desk)', detect: 'command:aider', profile: 'aider-desk' },
{ id: 'amp', label: 'Sourcegraph Amp', mech: 'npx skills add (amp)', detect: 'command:amp', profile: 'amp' },
{ id: 'bob', label: 'IBM Bob', mech: 'npx skills add (bob)', detect: 'command:bob', profile: 'bob' },
{ id: 'crush', label: 'Crush', mech: 'npx skills add (crush)', detect: 'command:crush', profile: 'crush' },
{ id: 'devin', label: 'Devin (terminal)', mech: 'npx skills add (devin)', detect: 'command:devin', profile: 'devin' },
{ id: 'droid', label: 'Droid (Factory)', mech: 'npx skills add (droid)', detect: 'command:droid', profile: 'droid' },
{ id: 'forgecode', label: 'ForgeCode', mech: 'npx skills add (forgecode)', detect: 'command:forge', profile: 'forgecode' },
{ id: 'goose', label: 'Block Goose', mech: 'npx skills add (goose)', detect: 'command:goose', profile: 'goose' },
{ id: 'iflow', label: 'iFlow CLI', mech: 'npx skills add (iflow-cli)', detect: 'command:iflow', profile: 'iflow-cli' },
{ id: 'kiro', label: 'Kiro CLI', mech: 'npx skills add (kiro-cli)', detect: 'command:kiro', profile: 'kiro-cli' },
{ id: 'mistral', label: 'Mistral Vibe', mech: 'npx skills add (mistral-vibe)', detect: 'command:mistral', profile: 'mistral-vibe' },
{ id: 'openhands', label: 'OpenHands', mech: 'npx skills add (openhands)', detect: 'command:openhands', profile: 'openhands' },
{ id: 'qwen', label: 'Qwen Code', mech: 'npx skills add (qwen-code)', detect: 'command:qwen', profile: 'qwen-code' },
{ id: 'rovodev', label: 'Atlassian Rovo Dev', mech: 'npx skills add (rovodev)', detect: 'command:rovodev', profile: 'rovodev' },
{ id: 'tabnine', label: 'Tabnine CLI', mech: 'npx skills add (tabnine-cli)', detect: 'command:tabnine', profile: 'tabnine-cli' },
{ id: 'trae', label: 'Trae', mech: 'npx skills add (trae)', detect: 'command:trae', profile: 'trae' },
{ id: 'warp', label: 'Warp', mech: 'npx skills add (warp)', detect: 'command:warp', profile: 'warp' },
{ id: 'replit', label: 'Replit Agent', mech: 'npx skills add (replit)', detect: 'command:replit', profile: 'replit' },
// Soft (opt-in via --only) β no reliable always-on probe.
// junie: ships only as a JetBrains plugin; jetbrains-plugin probe walks
// ~/.config/JetBrains looking for "junie" β fires on stale plugin caches.
// qoder: dir-only.
// antigravity: lives at ~/.gemini/antigravity which is created by the
// gemini CLI on first use β not a reliable signal of antigravity itself.
{ id: 'junie', label: 'JetBrains Junie', mech: 'npx skills add (junie)', detect: 'jetbrains-plugin:junie', profile: 'junie', soft: true },
{ id: 'qoder', label: 'Qoder', mech: 'npx skills add (qoder)', detect: 'dir:$HOME/.qoder', profile: 'qoder', soft: true },
{ id: 'antigravity',label: 'Google Antigravity', mech: 'npx skills add (antigravity)', detect: 'dir:$HOME/.gemini/antigravity', profile: 'antigravity', soft: true },
];
// ββ Detection βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function hasCmd(cmd) {
try {
if (process.platform === 'win32') {
const r = child_process.spawnSync('where', [cmd], { stdio: 'ignore' });
return r.status === 0;
}
const r = child_process.spawnSync('sh', ['-c', `command -v ${shellEscape(cmd)}`], { stdio: 'ignore' });
return r.status === 0;
} catch (_) { return false; }
}
function shellEscape(s) { return `'${String(s).replace(/'/g, `'\\''`)}'`; }
function expandHome(p) { return p.replace(/^\$HOME/, os.homedir()).replace(/^~/, os.homedir()); }
function vscodeExtPresent(needle) {
const home = os.homedir();
const roots = [
path.join(home, '.vscode/extensions'),
path.join(home, '.vscode-server/extensions'),
path.join(home, '.cursor/extensions'),
path.join(home, '.windsurf/extensions'),
];
const re = new RegExp(needle, 'i');
for (const r of roots) {
if (!fs.existsSync(r)) continue;
let entries;
try { entries = fs.readdirSync(r); } catch (_) { continue; }
if (entries.some(e => re.test(e))) return true;
}
return false;
}
function cursorExtPresent(needle) {
const dir = path.join(os.homedir(), '.cursor/extensions');
if (!fs.existsSync(dir)) return false;
const re = new RegExp(needle, 'i');
try { return fs.readdirSync(dir).some(e => re.test(e)); } catch (_) { return false; }
}
function jetbrainsPresent() {
const home = os.homedir();
return fs.existsSync(path.join(home, 'Library/Application Support/JetBrains'))
|| fs.existsSync(path.join(home, '.config/JetBrains'));
}
function jetbrainsPluginPresent(needle) {
const home = os.homedir();
const roots = [
path.join(home, 'Library/Application Support/JetBrains'),
path.join(home, '.config/JetBrains'),
];
const re = new RegExp(needle, 'i');
for (const r of roots) {
if (!fs.existsSync(r)) continue;
if (walkDir(r, 4).some(p => re.test(path.basename(p)))) return true;
}
return false;
}
function walkDir(root, depth) {
const out = [];
if (depth < 0) return out;
let entries;
try { entries = fs.readdirSync(root, { withFileTypes: true }); } catch (_) { return out; }
for (const e of entries) {
const full = path.join(root, e.name);
if (e.isDirectory()) { out.push(full); out.push(...walkDir(full, depth - 1)); }
}
return out;
}
function macAppPresent(name) {
if (process.platform !== 'darwin') return false;
const candidates = [
`/Applications/${name}.app`,
path.join(os.homedir(), 'Applications', `${name}.app`),
];
return candidates.some(p => fs.existsSync(p));
}
function detectMatch(spec) {
if (!spec) return false;
for (const clause of spec.split('||')) {
const c = clause.trim();
if (!c) continue;
const colon = c.indexOf(':');
const kind = colon === -1 ? c : c.slice(0, colon);
const val = colon === -1 ? '' : expandHome(c.slice(colon + 1));
let ok = false;
switch (kind) {
case 'command': ok = hasCmd(val); break;
case 'dir': ok = safeStat(val, 'isDirectory'); break;
case 'file': ok = safeStat(val, 'isFile'); break;
case 'macapp': ok = macAppPresent(val); break;
case 'vscode-ext': ok = vscodeExtPresent(val); break;
case 'cursor-ext': ok = cursorExtPresent(val); break;
case 'jetbrains-config': ok = jetbrainsPresent(); break;
case 'jetbrains-plugin': ok = jetbrainsPluginPresent(val); break;
}
if (ok) return true;
}
return false;
}
function safeStat(p, method) {
try { return fs.statSync(p)[method](); } catch (_) { return false; }
}
// ββ Repo root resolution βββββββββββββββββββββββββββββββββββββββββββββββββββ
function detectRepoRoot() {
// bin/install.js sits at <repo>/bin/install.js. Walk up one.
const here = path.dirname(__filename);
const root = path.resolve(here, '..');
if (fs.existsSync(path.join(root, 'src', 'hooks')) &&
fs.existsSync(path.join(root, 'agents')) &&
fs.existsSync(path.join(root, 'skills'))) {
return root;
}
return null;
}
// ββ Run helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// On Windows, npm/npx/claude/gemini/codex etc. ship as `.cmd` batch shims.
// Node's spawnSync('claude', ...) returns ENOENT for these unless we either
// (a) set shell:true (cmd.exe respects PATHEXT) or
// (b) resolve the actual `.cmd` path before spawning.
// We pick (a) β simpler, fewer cross-version corner cases. The cost is that
// args with spaces need quoting; we quote them defensively below.
const IS_WIN = process.platform === 'win32';
function quoteWinArg(a) {
if (!IS_WIN) return a;
if (a === '' || /[\s"]/.test(a)) {
// Standard CommandLineToArgvW escaping
return '"' + String(a).replace(/\\(?=\\*"|$)/g, '\\\\').replace(/"/g, '\\"') + '"';
}
return a;
}
function spawnXplat(cmd, args, opts) {
if (IS_WIN) {
const quoted = args.map(quoteWinArg).join(' ');
return child_process.spawnSync(`${cmd} ${quoted}`, [], Object.assign({ shell: true }, opts || {}));
}
return child_process.spawnSync(cmd, args, opts || {});
}
function runSpawn(cmd, args, opts, dry) {
if (dry) { process.stdout.write(` would run: ${cmd} ${args.join(' ')}\n`); return { status: 0 }; }
process.stdout.write(` $ ${cmd} ${args.join(' ')}\n`);
return spawnXplat(cmd, args, Object.assign({ stdio: 'inherit' }, opts || {}));
}
function captureSpawn(cmd, args) {
try { return spawnXplat(cmd, args, { encoding: 'utf8' }); }
catch (_) { return { status: 1, stdout: '', stderr: '' }; }
}
function absoluteNodePath() {
return process.execPath;
}
// ββ Per-provider installers ββββββββββββββββββββββββββββββββββββββββββββββββ
async function installClaude(ctx) {
const { say, note, warn, ok, opts, results } = ctx;
results.detected++;
say('β Claude Code detected');
// Plugin install (idempotent unless --force)
let alreadyInstalled = false;
if (!opts.force) {
const r = captureSpawn('claude', ['plugin', 'list']);
if (r.status === 0 && /caveman/i.test(r.stdout || '')) alreadyInstalled = true;
}
if (alreadyInstalled) {
note(' caveman plugin already installed (use --force to reinstall)');
results.skipped.push(['claude', 'plugin already installed']);
} else {
const r1 = runSpawn('claude', ['plugin', 'marketplace', 'add', REPO], null, opts.dryRun);
const r2 = runSpawn('claude', ['plugin', 'install', 'caveman@caveman'], null, opts.dryRun);
if ((r1.status || 0) === 0 && (r2.status || 0) === 0) results.installed.push('claude');
else results.failed.push(['claude', 'claude plugin install failed']);
}
if (opts.withHooks) {
say(' β installing hooks (--with-hooks)');
const r = await installHooks(ctx);
if (r === 'ok') results.installed.push('claude-hooks');
else if (r === 'skip') results.skipped.push(['claude-hooks', 'already wired']);
else results.failed.push(['claude-hooks', r]);
}
if (opts.withMcpShrink) {
say(' β wiring caveman-shrink MCP proxy (--with-mcp-shrink)');
const r = installMcpShrink(ctx);
if (r.kind === 'ok') results.installed.push('caveman-shrink');
if (r.kind === 'skip') results.skipped.push(['caveman-shrink', r.why]);
if (r.kind === 'fail') results.failed.push(['caveman-shrink', r.why]);
}
process.stdout.write('\n');
}
function installGemini(ctx) {
const { say, note, opts, results } = ctx;
results.detected++;
say('β Gemini CLI detected');
if (!opts.force) {
const r = captureSpawn('gemini', ['extensions', 'list']);
if (r.status === 0 && /caveman/i.test(r.stdout || '')) {
note(' caveman extension already installed (use --force to reinstall)');
results.skipped.push(['gemini', 'extension already installed']);
process.stdout.write('\n');
return;
}
}
const r = runSpawn('gemini', ['extensions', 'install', `https://github.com/${REPO}`], null, opts.dryRun);
if ((r.status || 0) === 0) results.installed.push('gemini');
else results.failed.push(['gemini', 'gemini extensions install failed']);
process.stdout.write('\n');
}
function installViaSkills(ctx, prov) {
const { say, opts, results } = ctx;
results.detected++;
say(`β ${prov.label} detected`);
// --yes --all: skip the upstream skill-selection TUI and confirmation prompts.
// Without these, `curl|bash` (no TTY on stdin) renders an empty checkbox list
// the user can't interact with, then exits 0 with zero skills installed β
// and our installer happily reports success. See issue #370.
// We've already decided which agent to install for via auto-detect / --only;
// making the user re-select 7 skills inside skills CLI would be redundant.
const args = ['-y', 'skills', 'add', REPO, '-a', prov.profile, '--yes', '--all'];
const r = runSpawn('npx', args, null, opts.dryRun);
if ((r.status || 0) === 0) results.installed.push(prov.id);
else results.failed.push([prov.id, `npx skills add (${prov.profile}) failed`]);
process.stdout.write('\n');
}
// ββ opencode native install βββββββββββββββββββββββββββββββββββββββββββββββ
// Drops the in-repo plugin (src/plugins/opencode/) plus skills, agents,
// commands, and an AGENTS.md ruleset into ~/.config/opencode/. Patches
// opencode.json with a "plugin" array entry. Mirrors the Claude Code hook
// architecture as closely as opencode allows β only the statusline is missing
// (opencode's TUI exposes no plugin-writable badge).
const OPENCODE_SKILL_DIRS = ['caveman', 'caveman-commit', 'caveman-review', 'caveman-help', 'caveman-stats', 'caveman-compress', 'cavecrew'];
const OPENCODE_AGENT_FILES = ['cavecrew-investigator.md', 'cavecrew-builder.md', 'cavecrew-reviewer.md'];
const OPENCODE_COMMAND_FILES = ['caveman.md', 'caveman-commit.md', 'caveman-review.md', 'caveman-compress.md', 'caveman-stats.md', 'caveman-help.md'];
const OPENCODE_PLUGIN_REL = './plugins/caveman/plugin.js';
const OPENCODE_AGENTS_MD_SENTINEL = 'Respond terse like smart caveman';
// Marker fence for the opencode AGENTS.md ruleset block. Same convention as
// bin/lib/openclaw.js for SOUL.md β lets us strip our block cleanly even when
// the user has authored content above AND below it.
const OPENCODE_AGENTS_MD_BEGIN = '<!-- caveman-begin -->';
const OPENCODE_AGENTS_MD_END = '<!-- caveman-end -->';
function opencodeConfigDir() {
if (process.env.XDG_CONFIG_HOME) return path.join(process.env.XDG_CONFIG_HOME, 'opencode');
if (IS_WIN) return path.join(process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'), 'opencode');
return path.join(os.homedir(), '.config', 'opencode');
}
function copyDirRecursive(src, dest) {
fs.mkdirSync(dest, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const s = path.join(src, entry.name);
const d = path.join(dest, entry.name);
if (entry.isDirectory()) copyDirRecursive(s, d);
else if (entry.isFile()) fs.copyFileSync(s, d);
}
}
function installOpencode(ctx) {
const { say, note, warn, opts, repoRoot, results } = ctx;
results.detected++;
say('β opencode detected');
if (!repoRoot) {
warn(' opencode native install requires a local clone of the caveman repo.');
note(' Re-run from a clone: git clone https://github.com/' + REPO + ' && cd caveman && node bin/install.js --only opencode');
results.failed.push(['opencode', 'native install requires local repo clone']);
process.stdout.write('\n');
return;
}
const dir = opencodeConfigDir();
const pluginDir = path.join(dir, 'plugins', 'caveman');
const commandsDir = path.join(dir, 'commands');
const agentsDir = path.join(dir, 'agents');
const skillsDir = path.join(dir, 'skills');
const opencodeJson = path.join(dir, 'opencode.json');
const agentsMd = path.join(dir, 'AGENTS.md');
if (opts.dryRun) {
note(` would mkdir ${pluginDir}/, ${commandsDir}/, ${agentsDir}/, ${skillsDir}/`);
note(` would copy plugin.js + package.json + caveman-config.cjs into ${pluginDir}/`);
note(` would copy ${OPENCODE_COMMAND_FILES.length} command files into ${commandsDir}/`);
note(` would copy ${OPENCODE_AGENT_FILES.length} cavecrew agents into ${agentsDir}/`);
note(` would copy ${OPENCODE_SKILL_DIRS.length} skill dirs into ${skillsDir}/`);
note(` would patch ${opencodeJson} with "plugin" entry${opts.withMcpShrink ? ' + caveman-shrink MCP' : ''}`);
note(` would write Tier-3 ruleset to ${agentsMd}`);
results.installed.push('opencode');
process.stdout.write('\n');
return;
}
try {
// 1. Plugin dir β copy plugin.js, package.json, caveman-config.js (sibling).
// Same `--force` semantic as commands/agents/skills below: re-runs leave
// user edits to plugin.js alone unless --force is passed.
fs.mkdirSync(pluginDir, { recursive: true });
const pluginSrc = path.join(repoRoot, 'src', 'plugins', 'opencode');
const pluginPayload = [
[path.join(pluginSrc, 'plugin.js'), path.join(pluginDir, 'plugin.js')],
[path.join(pluginSrc, 'package.json'), path.join(pluginDir, 'package.json')],
// Renamed to .cjs because the plugin dir is "type": "module" β a bare .js
// sibling would be loaded as ESM and break the plugin's require() bridge.
[path.join(repoRoot, 'src', 'hooks', 'caveman-config.js'),
path.join(pluginDir, 'caveman-config.cjs')],
];
for (const [src, dest] of pluginPayload) {
if (fs.existsSync(dest) && !opts.force) {
note(` skipped ${dest} (exists; --force to overwrite)`);
continue;
}
fs.copyFileSync(src, dest);
}
process.stdout.write(` installed: ${pluginDir}\n`);
// 2. Commands.
fs.mkdirSync(commandsDir, { recursive: true });
const cmdSrcDir = path.join(pluginSrc, 'commands');
for (const f of OPENCODE_COMMAND_FILES) {
const src = path.join(cmdSrcDir, f);
const dest = path.join(commandsDir, f);
if (fs.existsSync(dest) && !opts.force) { note(` skipped ${dest} (exists; --force to overwrite)`); continue; }
fs.copyFileSync(src, dest);
process.stdout.write(` installed: ${dest}\n`);
}
// 3. Subagents. Source files target Claude Code's schema (`tools: [...]`
// YAML array); OpenCode rejects that form and refuses to boot until the
// file is removed. Strip the `tools:` line on copy β OpenCode falls back
// to its default tool set, and subagent prompts already self-restrict in
// the body. Issue 386.
fs.mkdirSync(agentsDir, { recursive: true });
const agentSrcDir = path.join(repoRoot, 'agents');
for (const f of OPENCODE_AGENT_FILES) {
const src = path.join(agentSrcDir, f);
const dest = path.join(agentsDir, f);
if (!fs.existsSync(src)) continue;
if (fs.existsSync(dest) && !opts.force) { note(` skipped ${dest} (exists; --force to overwrite)`); continue; }
fs.writeFileSync(dest, stripOpencodeAgentTools(fs.readFileSync(src, 'utf8')));
process.stdout.write(` installed: ${dest}\n`);
}
// 4. Skills β opencode auto-discovers SKILL.md from ~/.config/opencode/skills/.
fs.mkdirSync(skillsDir, { recursive: true });
const skillSrcDir = path.join(repoRoot, 'skills');
for (const name of OPENCODE_SKILL_DIRS) {
const src = path.join(skillSrcDir, name);
const dest = path.join(skillsDir, name);
if (!fs.existsSync(src)) continue;
if (fs.existsSync(dest) && !opts.force) { note(` skipped ${dest}/ (exists; --force to overwrite)`); continue; }
copyDirRecursive(src, dest);
process.stdout.write(` installed: ${dest}/\n`);
}
// 5. AGENTS.md β Tier-3 always-on ruleset. Wrapped in begin/end markers so
// a later --uninstall can strip our block cleanly even if the user has
// authored content above AND below it. Idempotency check uses the begin
// marker (the legacy sentinel still matches old installs).
const ruleBody = fs.readFileSync(path.join(repoRoot, 'src', 'rules', 'caveman-activate.md'), 'utf8').trimEnd() + '\n';
const fencedBlock = `${OPENCODE_AGENTS_MD_BEGIN}\n${ruleBody}${OPENCODE_AGENTS_MD_END}\n`;
if (fs.existsSync(agentsMd)) {
const existing = fs.readFileSync(agentsMd, 'utf8');
const alreadyFenced = existing.includes(OPENCODE_AGENTS_MD_BEGIN)
&& existing.includes(OPENCODE_AGENTS_MD_END);
const alreadyByLegacySentinel = !alreadyFenced && existing.includes(OPENCODE_AGENTS_MD_SENTINEL);
if (alreadyFenced) {
note(` ${agentsMd} already contains caveman ruleset`);
} else if (alreadyByLegacySentinel) {
note(` ${agentsMd} contains a legacy (un-fenced) caveman block β leaving as-is`);
note(' re-run with --force to replace it with a fenced block');
if (opts.force) {
// Replace the entire file with a clean fenced version. The legacy
// path didn't fence, so we can't isolate the block β full rewrite is
// the only safe option under --force.
fs.writeFileSync(agentsMd, fencedBlock, { mode: 0o644 });
process.stdout.write(` rewrote ${agentsMd} with fenced caveman block\n`);
}
} else {
const sep = existing.endsWith('\n\n') ? '' : (existing.endsWith('\n') ? '\n' : '\n\n');
fs.writeFileSync(agentsMd, existing + sep + fencedBlock, { mode: 0o644 });
process.stdout.write(` appended caveman ruleset to ${agentsMd}\n`);
}
} else {
fs.writeFileSync(agentsMd, fencedBlock, { mode: 0o644 });
process.stdout.write(` installed: ${agentsMd}\n`);
}
// 6. opencode.json β add plugin entry; optional caveman-shrink MCP.
let cfg = SETTINGS.readSettings(opencodeJson);
if (cfg === null) {
warn(` ${opencodeJson} unparseable; will not touch it. Edit manually then re-run.`);
results.failed.push(['opencode', 'opencode.json unparseable']);
process.stdout.write('\n');
return;
}
// Preserve the original on first install only β repeat installs would
// otherwise overwrite the only known-good copy with an already-merged file.
const opencodeBak = opencodeJson + '.bak';
if (fs.existsSync(opencodeJson) && !fs.existsSync(opencodeBak)) {
try { fs.copyFileSync(opencodeJson, opencodeBak); } catch (_) {}
}
if (!Array.isArray(cfg.plugin)) cfg.plugin = [];
if (!cfg.plugin.includes(OPENCODE_PLUGIN_REL)) {
cfg.plugin.push(OPENCODE_PLUGIN_REL);
}
if (opts.withMcpShrink) {
if (!cfg.mcp || typeof cfg.mcp !== 'object') cfg.mcp = {};
if (!cfg.mcp['caveman-shrink']) {
cfg.mcp['caveman-shrink'] = {
type: 'local',
command: ['npx', '-y', MCP_SHRINK_PKG],
enabled: true,
};
process.stdout.write(' registered caveman-shrink MCP server\n');
}
}
SETTINGS.writeSettings(opencodeJson, cfg);
process.stdout.write(` patched: ${opencodeJson}\n`);
results.installed.push('opencode');
} catch (e) {
warn(' opencode install failed: ' + (e && e.message || e));
results.failed.push(['opencode', (e && e.message) || 'unknown error']);
}
process.stdout.write('\n');
}
// ββ OpenClaw native install βββββββββββββββββββββββββββββββββββββββββββββββ
// Drops skills/caveman/ into the OpenClaw workspace and appends a small
// auto-injected bootstrap block to the workspace SOUL.md. Always-on behavior
// comes from SOUL.md (auto-injected each turn); the skill folder makes
// caveman discoverable via `openclaw skills list`. See bin/lib/openclaw.js
// for the actual file writes.
function installOpenclaw(ctx) {
const { say, note, warn, opts, repoRoot, results } = ctx;
results.detected++;
say('β OpenClaw detected');
const log = {
write: (s) => process.stdout.write(s),
note: (s) => note(s),
warn: (s) => warn(s),
};
const r = OPENCLAW.installOpenclaw({
workspace: process.env.OPENCLAW_WORKSPACE || undefined,
repoRoot,
dryRun: opts.dryRun,
force: opts.force,
log,
});
if (r.ok) results.installed.push('openclaw');
else results.failed.push(['openclaw', r.reason || 'install failed']);
process.stdout.write('\n');
}
// ββ Hooks installer ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Replaces src/hooks/install.sh + src/hooks/install.ps1.
async function installHooks(ctx) {
const { note, warn, opts, repoRoot, configDir } = ctx;
const hooksDir = path.join(configDir, 'hooks');
const settingsPath = path.join(configDir, 'settings.json');
const sourceDir = repoRoot ? path.join(repoRoot, 'src', 'hooks') : null;
if (opts.dryRun) {
note(` would mkdir -p ${hooksDir}`);
for (const f of HOOK_FILES) note(` would install ${path.join(hooksDir, f)}`);
note(` would merge SessionStart + UserPromptSubmit + statusline into ${settingsPath}`);
return 'ok';
}
fs.mkdirSync(hooksDir, { recursive: true });
// Copy or download each hook file. Local-clone-first for offline installs.
for (const f of HOOK_FILES) {
const dest = path.join(hooksDir, f);
if (sourceDir && fs.existsSync(path.join(sourceDir, f))) {
fs.copyFileSync(path.join(sourceDir, f), dest);
} else {
try { await downloadTo(`${HOOKS_REMOTE}/${f}`, dest); }
catch (e) { return `download ${f} failed: ${e.message}`; }
}
process.stdout.write(` installed: ${dest}\n`);
}
// chmod statusline (no-op on Windows)
try { fs.chmodSync(path.join(hooksDir, 'caveman-statusline.sh'), 0o755); } catch (_) {}
// Merge into settings.json
let settings = SETTINGS.readSettings(settingsPath);
if (settings === null) {
warn(' settings.json unparseable; will not touch it. Edit manually then re-run.');
return 'settings.json unparseable';
}
// Backup once, preserved across reinstalls. Without the !fs.existsSync(bak)
// guard, the second install would overwrite the only known-good copy with
// the already-merged file, destroying recovery.
const bak = settingsPath + '.bak';
if (fs.existsSync(settingsPath) && !fs.existsSync(bak)) {
try { fs.copyFileSync(settingsPath, bak); } catch (_) {}
}
const node = absoluteNodePath();
const activate = path.join(hooksDir, 'caveman-activate.js');
const tracker = path.join(hooksDir, 'caveman-mode-tracker.js');
const statusline = path.join(hooksDir, 'caveman-statusline.sh');
// Migrate any legacy bare-`node` invocations of our managed scripts.
SETTINGS.rewriteLegacyManagedHookCommands(settings, node);
SETTINGS.addCommandHook(settings, 'SessionStart', {
command: `"${node}" "${activate}"`,
marker: 'caveman-activate',
timeout: 5,
statusMessage: 'Loading caveman mode...',
});
SETTINGS.addCommandHook(settings, 'UserPromptSubmit', {
command: `"${node}" "${tracker}"`,
marker: 'caveman-mode-tracker',
timeout: 5,
statusMessage: 'Tracking caveman mode...',
});
// Statusline β set if absent or already pointing at our script.
// Windows: prefer pwsh (PowerShell 7+, cross-platform), fall back to
// powershell.exe (Windows PowerShell 5.1, ships with every Windows install).
// Use -ExecutionPolicy Bypass so users without RemoteSigned policy can run.
const psHost = IS_WIN && hasCmd('pwsh') ? 'pwsh' : (IS_WIN ? 'powershell' : null);
const slCmd = IS_WIN
? `${psHost} -NoProfile -ExecutionPolicy Bypass -File "${path.join(hooksDir, 'caveman-statusline.ps1')}"`
: `bash "${statusline}"`;
if (!settings.statusLine) {
settings.statusLine = { type: 'command', command: slCmd };
process.stdout.write(' statusline badge configured.\n');
} else {
const existing = typeof settings.statusLine === 'string'
? settings.statusLine
: (settings.statusLine.command || '');
if (existing.includes(statusline) || existing.includes('caveman-statusline')) {
process.stdout.write(' statusline badge already configured.\n');
} else {
process.stdout.write(' NOTE: existing statusline detected β caveman badge NOT added.\n');
process.stdout.write(' See src/hooks/README.md to add the badge to your existing statusline.\n');
}
}
// Defensive validation before write β Claude Code Zod will discard the
// entire settings.json if any single hook is malformed (#249-class footgun).
SETTINGS.validateHookFields(settings);
SETTINGS.writeSettings(settingsPath, settings);
process.stdout.write(` hooks wired in ${settingsPath}\n`);
return 'ok';
}
// ββ MCP shrink wiring βββββββββββββββββββββββββββββββββββββββββββββββββββββ
function installMcpShrink(ctx) {
const { note, warn, opts } = ctx;
// Probe npm first β registry outage = clean skip with manual snippet.
const probe = captureSpawn('npm', ['view', MCP_SHRINK_PKG, 'name']);
if (probe.status !== 0) {
warn(` 'npm view ${MCP_SHRINK_PKG}' returned no metadata β registry unreachable or package missing.`);
note(' Skipping registration. Re-run --with-mcp-shrink when the registry is reachable.');
return { kind: 'skip', why: 'npm registry probe failed' };
}
// Detect modern `claude mcp add`
const help = captureSpawn('claude', ['mcp', '--help']);
if (help.status !== 0) {
note(" 'claude mcp add' not available on this CLI. Add the snippet from");
note(' src/hooks/README.md to your Claude Code MCP config manually.');
return { kind: 'skip', why: 'manual config required' };
}
const r = runSpawn('claude', ['mcp', 'add', 'caveman-shrink', '--', 'npx', '-y', MCP_SHRINK_PKG], null, opts.dryRun);
if ((r.status || 0) === 0) {
note(' registered. Wrap an upstream by editing the mcpServers entry β see:');
note(` https://github.com/${REPO}/tree/main/src/mcp-servers/caveman-shrink`);
return { kind: 'ok' };
}
return { kind: 'fail', why: 'claude mcp add failed' };
}
// ββ Init writers (per-repo rule files) ββββββββββββββββββββββββββββββββββββ
async function runInit(ctx) {
const { note, warn, opts, repoRoot } = ctx;
const local = repoRoot && path.join(repoRoot, 'src/tools/caveman-init.js');
const args = [process.cwd()];
if (opts.dryRun) args.push('--dry-run');
if (opts.force) args.push('--force');
if (local && fs.existsSync(local)) {
const r = runSpawn(absoluteNodePath(), [local, ...args], null, opts.dryRun);
return (r.status || 0) === 0;
}
// Curl-pipe fallback
if (opts.dryRun) {
note(` would download ${INIT_SCRIPT_URL} and run it on ${process.cwd()}`);
return true;
}
try {
const tmp = path.join(os.tmpdir(), `caveman-init-${process.pid}.js`);
await downloadTo(INIT_SCRIPT_URL, tmp);
const r = child_process.spawnSync(absoluteNodePath(), [tmp, ...args], { stdio: 'inherit' });
try { fs.unlinkSync(tmp); } catch (_) {}
return (r.status || 0) === 0;
} catch (e) {
warn(' ' + e.message);
return false;
}
}
// ββ HTTPS download via stdlib βββββββββββββββββββββββββββββββββββββββββββββ
function downloadTo(url, dest) {
// Prefer curl/wget when available (better proxy + cert handling on legacy
// systems); fall back to Node https.
if (hasCmd('curl')) {
const r = child_process.spawnSync('curl', ['-fsSL', '-o', dest, url], { stdio: 'inherit' });
if (r.status === 0) return;
throw new Error(`curl failed for ${url}`);
}
const https = require('https');
return new Promise((resolve, reject) => {
const req = https.get(url, (res) => {
if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
resolve(downloadTo(res.headers.location, dest));
return;
}
if (res.statusCode !== 200) { reject(new Error(`HTTP ${res.statusCode} for ${url}`)); return; }
const out = fs.createWriteStream(dest);
res.pipe(out);
out.on('finish', () => out.close(resolve));
out.on('error', reject);
});
req.on('error', reject);
});
}
// ββ Uninstall βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function uninstall(ctx) {
const { say, note, warn, ok, opts, configDir } = ctx;
say('πͺ¨ caveman uninstall');
if (opts.dryRun) note(' (dry run β nothing will be removed)');
// Hooks: remove from settings.json + delete hook files.
const hooksDir = path.join(configDir, 'hooks');
const settingsPath = path.join(configDir, 'settings.json');
if (fs.existsSync(settingsPath)) {
const settings = SETTINGS.readSettings(settingsPath);
if (settings) {
const removed = SETTINGS.removeCavemanHooks(settings, 'caveman');
// Drop our statusline if it points at our script
if (settings.statusLine) {
const cmd = typeof settings.statusLine === 'string' ? settings.statusLine : (settings.statusLine.command || '');
if (cmd.includes('caveman-statusline')) delete settings.statusLine;
}
SETTINGS.validateHookFields(settings);
if (!opts.dryRun) SETTINGS.writeSettings(settingsPath, settings);
ok(` removed ${removed} caveman hook entr${removed === 1 ? 'y' : 'ies'} from settings.json`);
}
}
if (fs.existsSync(hooksDir)) {
for (const f of HOOK_FILES) {
const p = path.join(hooksDir, f);
if (!fs.existsSync(p)) continue;
if (!opts.dryRun) { try { fs.unlinkSync(p); } catch (_) {} }
note(` removed ${p}`);
}
// Don't rmdir hooksDir β other plugins may use it.
}
// Plugin uninstall on Claude. Probe `plugin list` first so a re-run on a
// machine where caveman was never installed (or was already removed) doesn't
// print "Plugin not installed" stderr noise.
if (hasCmd('claude')) {
const probe = captureSpawn('claude', ['plugin', 'list']);
if (probe.status === 0 && /caveman/i.test(probe.stdout || '')) {
const r = runSpawn('claude', ['plugin', 'uninstall', 'caveman@caveman'], null, opts.dryRun);
if ((r.status || 0) === 0) ok(' removed claude plugin');
} else {
note(' claude plugin not installed β skipping');
}
// caveman-shrink MCP β only run if `claude mcp` subcommand exists. Tolerate
// non-zero exit (server may have never been registered).
const mcpHelp = captureSpawn('claude', ['mcp', '--help']);
if (mcpHelp.status === 0) {
runSpawn('claude', ['mcp', 'remove', 'caveman-shrink'], null, opts.dryRun);
}
}
// Gemini extension. Same idempotency probe as claude.
if (hasCmd('gemini')) {
const probe = captureSpawn('gemini', ['extensions', 'list']);
if (probe.status === 0 && /caveman/i.test(probe.stdout || '')) {
runSpawn('gemini', ['extensions', 'uninstall', 'caveman'], null, opts.dryRun);
} else {
note(' gemini extension not installed β skipping');
}
}
// opencode native install β strip plugin entry, MCP entry, and our files.
// Probed by the existence of the plugin dir we own; if absent, skip silently.
const ocDir = opencodeConfigDir();
const ocPluginDir = path.join(ocDir, 'plugins', 'caveman');
if (fs.existsSync(ocPluginDir)) {
const ocJson = path.join(ocDir, 'opencode.json');
if (fs.existsSync(ocJson)) {
const cfg = SETTINGS.readSettings(ocJson);
if (cfg) {
if (Array.isArray(cfg.plugin)) {
cfg.plugin = cfg.plugin.filter(p => p !== OPENCODE_PLUGIN_REL);
if (cfg.plugin.length === 0) delete cfg.plugin;
}
if (cfg.mcp && typeof cfg.mcp === 'object' && cfg.mcp['caveman-shrink']) {
delete cfg.mcp['caveman-shrink'];
if (Object.keys(cfg.mcp).length === 0) delete cfg.mcp;
}
if (!opts.dryRun) SETTINGS.writeSettings(ocJson, cfg);
ok(` pruned caveman entries from ${ocJson}`);
}
}
if (!opts.dryRun) { try { fs.rmSync(ocPluginDir, { recursive: true, force: true }); } catch (_) {} }
note(` removed ${ocPluginDir}`);
// Commands, agents, skills β only files matching our manifest (don't
// sweep the parent dirs; user may have other entries there).
for (const f of OPENCODE_COMMAND_FILES) {
const p = path.join(ocDir, 'commands', f);
if (fs.existsSync(p) && !opts.dryRun) { try { fs.unlinkSync(p); } catch (_) {} }
}
for (const f of OPENCODE_AGENT_FILES) {
const p = path.join(ocDir, 'agents', f);
if (fs.existsSync(p) && !opts.dryRun) { try { fs.unlinkSync(p); } catch (_) {} }
}
for (const name of OPENCODE_SKILL_DIRS) {
const p = path.join(ocDir, 'skills', name);
if (fs.existsSync(p) && !opts.dryRun) { try { fs.rmSync(p, { recursive: true, force: true }); } catch (_) {} }
}
// AGENTS.md β strip the fenced caveman block (preserves user content
// above and below). If the file is empty after the strip, remove it.
// Falls back to legacy unfenced-sentinel handling for installs that
// pre-date the marker fence.
const ocAgentsMd = path.join(ocDir, 'AGENTS.md');
if (fs.existsSync(ocAgentsMd)) {
const body = fs.readFileSync(ocAgentsMd, 'utf8');
const begin = body.indexOf(OPENCODE_AGENTS_MD_BEGIN);
const end = body.indexOf(OPENCODE_AGENTS_MD_END);
if (begin !== -1 && end !== -1 && end > begin) {
const before = body.slice(0, begin).replace(/\n+$/, '\n');
const after = body.slice(end + OPENCODE_AGENTS_MD_END.length).replace(/^\n+/, '\n');
let next = (before + after).trimEnd();
next = next ? next + '\n' : '';
if (!opts.dryRun) {
if (next === '') {
try { fs.unlinkSync(ocAgentsMd); } catch (_) {}
} else {
fs.writeFileSync(ocAgentsMd, next, { mode: 0o644 });
}
}
note(next === '' ? ` removed ${ocAgentsMd}` : ` stripped caveman block from ${ocAgentsMd}`);
} else if (body.includes(OPENCODE_AGENTS_MD_SENTINEL)) {
// Legacy install (no marker fence). Remove only if the file is ours.
if (body.trim() === '' || body.trim().startsWith(OPENCODE_AGENTS_MD_SENTINEL)) {
if (!opts.dryRun) { try { fs.unlinkSync(ocAgentsMd); } catch (_) {} }
note(` removed ${ocAgentsMd}`);