-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.js
More file actions
1194 lines (993 loc) · 32.7 KB
/
bootstrap.js
File metadata and controls
1194 lines (993 loc) · 32.7 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
/**
* AI Development Bootstrap Script
* - Deterministic project context (Repomix) + optional semantic expansion (Serena)
* - Commit policy enforcement (main commits + checkpoint commits)
* - Automatic snapshot regeneration after each commit (post-commit hook)
*
* Philosophy: minimal setup, repo is the source of truth, sync via git history + snapshots.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const cp = require('child_process');
const COLORS = {
reset: '\x1b[0m',
dim: '\x1b[2m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
};
function log(msg, color = 'reset') {
const c = COLORS[color] || COLORS.reset;
process.stdout.write(c + msg + COLORS.reset + '\n');
}
function runCommand(cmd, { silent = false } = {}) {
try {
const out = cp.execSync(cmd, { stdio: silent ? 'pipe' : 'inherit', encoding: 'utf8' });
return out ?? '';
} catch (e) {
if (silent) return '';
throw e;
}
}
function hasCommand(cmd) {
const whichCmd = process.platform === 'win32' ? `where ${cmd}` : `command -v ${cmd}`;
const res = runCommand(whichCmd, { silent: true });
return !!(res && res.trim());
}
function ensureDir(dirPath) {
if (!dirPath || dirPath === '.' || dirPath === '/') return;
if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath, { recursive: true });
}
function safeRead(filePath) {
try {
return fs.readFileSync(filePath, 'utf8');
} catch {
return '';
}
}
function writeFileSafe(filePath, content, { overwrite = false } = {}) {
const exists = fs.existsSync(filePath);
if (exists && !overwrite) {
log(` ↪ Skipped (exists): ${filePath}`, 'yellow');
return { wrote: false, skipped: true };
}
ensureDir(path.dirname(filePath));
fs.writeFileSync(filePath, content, 'utf8');
log(` ✓ ${filePath}`, 'green');
return { wrote: true, skipped: false };
}
function chmodSafe(filePath, mode) {
try {
fs.chmodSync(filePath, mode);
} catch {
// Non-fatal (Windows, some FS)
}
}
function timestampId() {
return new Date().toISOString().replace(/[:.]/g, '-');
}
function createBackup(pathsToBackup) {
const backupDir = `.ai-dev-backup-${timestampId()}`;
let did = false;
for (const p of pathsToBackup) {
if (!fs.existsSync(p)) continue;
const dest = path.join(backupDir, p);
ensureDir(path.dirname(dest));
try {
fs.cpSync(p, dest, { recursive: true });
did = true;
} catch (e) {
// fs.cpSync may fail on very old node; fallback
try {
if (fs.lstatSync(p).isDirectory()) {
ensureDir(dest);
// minimal dir copy fallback (shallow)
for (const entry of fs.readdirSync(p)) {
const src2 = path.join(p, entry);
const dst2 = path.join(dest, entry);
if (fs.lstatSync(src2).isDirectory()) continue;
fs.copyFileSync(src2, dst2);
}
} else {
fs.copyFileSync(p, dest);
}
did = true;
} catch {
log(` ⚠️ Backup failed for: ${p}`, 'yellow');
}
}
}
if (did) log(`🛟 Backup created: ${backupDir}`, 'green');
return did ? backupDir : null;
}
function ensureGitRepo() {
if (!fs.existsSync('.git')) {
log('🧩 No .git detected. Running: git init', 'yellow');
runCommand('git init', { silent: false });
}
ensureDir('.git/hooks');
}
function warnDirtyWorkingTree() {
const status = runCommand('git status --porcelain', { silent: true });
if (status && status.trim()) {
log('⚠️ Working tree is DIRTY (uncommitted changes detected).', 'yellow');
log(' Recommendation: commit/stash before running bootstrap on an existing project.', 'yellow');
}
}
function detectProjectType() {
const hasGit = fs.existsSync('.git');
const hasCode =
fs.existsSync('src') ||
fs.existsSync('lib') ||
fs.existsSync('app') ||
fs.existsSync('backend') ||
fs.existsSync('packages') ||
fs.existsSync('services');
const hasDocs = fs.existsSync('docs');
const hasArch = fs.existsSync('docs/ARCHITECTURE.md');
if (hasGit && (hasCode || hasDocs) && !hasArch) return 'existing';
return 'new';
}
function detectModules() {
try {
const candidates = ['src', 'app', 'lib', 'backend', 'packages', 'services'];
const found = [];
for (const base of candidates) {
if (!fs.existsSync(base)) continue;
const items = fs.readdirSync(base, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => `${base}/${d.name}/`);
if (items.length) found.push(...items.map(x => `- ${x}`));
}
if (!found.length) return '- No common module directories found';
return found.join('\n');
} catch {
return '- Unable to detect modules';
}
}
function detectTechStack() {
const detected = [];
try {
// Node / TS
if (fs.existsSync('package.json')) {
detected.push('- Node.js (package.json)');
try {
const pkg = JSON.parse(safeRead('package.json'));
const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
if (deps.typescript) detected.push('- TypeScript');
if (deps.react) detected.push('- React');
if (deps.next) detected.push('- Next.js');
if (deps.express) detected.push('- Express');
if (deps['@nestjs/core']) detected.push('- NestJS');
if (deps.prisma) detected.push('- Prisma');
} catch {
detected.push('- Node.js (unable to parse dependencies)');
}
}
// Python
if (fs.existsSync('pyproject.toml')) detected.push('- Python (pyproject.toml)');
if (fs.existsSync('requirements.txt')) detected.push('- Python (requirements.txt)');
// Go / Rust / Java / Ruby / PHP / .NET
if (fs.existsSync('go.mod')) detected.push('- Go (go.mod)');
if (fs.existsSync('Cargo.toml')) detected.push('- Rust (Cargo.toml)');
if (fs.existsSync('pom.xml') || fs.existsSync('build.gradle')) detected.push('- Java (Maven/Gradle)');
if (fs.existsSync('Gemfile')) detected.push('- Ruby (Gemfile)');
if (fs.existsSync('composer.json')) detected.push('- PHP (composer.json)');
// .NET: global.json or any *.csproj in repo root (fs.existsSync does not expand globs)
let hasCsproj = false;
if (fs.existsSync('global.json')) {
hasCsproj = true;
} else {
try {
const entries = fs.readdirSync('.', { withFileTypes: true });
hasCsproj = entries.some(e => e.isFile() && e.name.endsWith('.csproj'));
} catch {
// ignore
}
}
if (hasCsproj) detected.push('- .NET');
// Containers
if (fs.existsSync('Dockerfile') || fs.existsSync('docker-compose.yml') || fs.existsSync('compose.yaml')) detected.push('- Docker');
} catch {
// ignore
}
return detected.length ? detected.join('\n') : '- Unable to detect tech stack';
}
function createDirectories() {
const dirs = [
'docs/adr',
'.mcp',
'scripts',
'.serena',
'.github/workflows',
];
for (const d of dirs) {
ensureDir(d);
log(` ✓ ${d}/`, 'green');
}
}
function ensureGitignore() {
const gitignorePath = '.gitignore';
const mcpSection = `
# AI Development System - Generated Artifacts
.mcp/context.xml
.mcp/context_incremental.txt
.mcp/post-commit.log
.ai-dev-backup-*/
# Environment (secrets)
.env
`;
let content = fs.existsSync(gitignorePath)
? fs.readFileSync(gitignorePath, 'utf8')
: '';
if (content.includes('.mcp/context.xml')) {
log(' ✓ .gitignore already configured', 'green');
return;
}
const newContent = content.trimEnd() + mcpSection + '\n';
fs.writeFileSync(gitignorePath, newContent, 'utf8');
log(' ✓ Updated .gitignore', 'green');
}
function createDocs(projectType) {
const isDegraded = projectType === 'existing';
const degradedBlock = isDegraded
? `> ⚠️ **DEGRADED MODE TEMPLATE**
>
> **For AI (Claude Code/Cursor):**
> - This file is INCOMPLETE. Treat recommendations as SUGGESTIONS only.
> - Before making architectural changes, request human review.
> - Prioritize incremental changes over redesigns.
> - If something is unclear, ASK. State assumptions explicitly.
>
> **For Developers:**
> - Review and complete all sections marked with [TODO]
> - Document your current architecture truthfully
> - Update this file as you refine the system
> - Remove this warning when complete
>
> **To exit degraded mode:** Complete all [TODO] sections below.
>
---
`
: '';
const architecture = `# ARCHITECTURE
${degradedBlock}## Purpose
${isDegraded ? '[TODO: Describe what this system does and why it exists]' : 'Describe what this system does and why it exists.'}
## System Invariants (Non‑Negotiable)
- LLMs are executors, not sources of truth
- Repo (Git) is the source of truth
- Architecture docs > code
- Decisions are written (ADR), not implied
- Context is rebuilt from the repository snapshot, not from chat history
## Modules
${isDegraded ? `[TODO: List your main modules/components]
Detected modules (verify):
${detectModules()}` : '[List your main modules/components]'}
## Tech Stack
${isDegraded ? `[TODO: Verify and document]
Detected (best‑effort):
${detectTechStack()}` : '[Document your tech stack]'}
## Context Strategy (Onion Model)
Layer 0 — Always included:
- docs/ARCHITECTURE.md
- docs/CONVENTIONS.md
- latest ADRs (docs/adr)
Layer 1 — Incremental delta:
- git diff against base branch (default: main)
- changed files content
Layer 2 — Dynamic expansion (on demand):
- callers / references / dependencies
- retrieved via Serena (optional)
## Operating Rules
- Use **meaningful commits** as checkpoints of intent (even if small)
- After each commit: regenerate snapshot (.mcp/context.xml)
- Use ADRs for architectural decisions that change boundaries, invariants, or contracts
`;
const conventions = `# CONVENTIONS
## Commit Message Policy
All commits MUST follow one of two templates.
### Main commits (feature work)
\`type(scope): short description\`
Where \`type\` ∈ \`feat|fix|refactor|docs|test|chore\`
Examples:
- feat(auth): add oauth skeleton
- fix(api): handle empty token
- refactor(core): split router
### Checkpoint commits (micro checkpoints)
\`checkpoint(scope): short description\`
Examples:
- checkpoint(ui): adjust spacing
- checkpoint(docs): update wording
Enforcement:
- Local git hook: .git/hooks/commit-msg
- CI safety net: .github/workflows/commit-policy.yml
## Degraded Mode
If docs/ARCHITECTURE.md contains "DEGRADED MODE TEMPLATE":
- AI must treat architecture as incomplete
- AI must ask clarifying questions
- AI must avoid large refactors without explicit approval
## Snapshot / Context
- Full snapshot: .mcp/context.xml (generated by Repomix)
- Incremental snapshot: .mcp/context_incremental.txt (generated by scripts/generate-context.sh)
## ADR
Use ADRs for:
- changing module boundaries
- introducing new core dependencies
- changing APIs/contracts
- changing invariants or data flows
`;
const adrTemplate = `# ADR-XXX: <Title>
**Status:** Draft | Accepted | Rejected
**Date:** YYYY-MM-DD
**Deciders:** <names>
## Context
<What problem are we solving?>
## Decision
<What did we decide?>
## Rationale
<Why this decision?>
## Consequences
<Good and bad effects>
## Alternatives Considered
<Other options>
## References
- Links / docs / PRs
`;
writeFileSafe('docs/ARCHITECTURE.md', architecture, { overwrite: false });
writeFileSafe('docs/CONVENTIONS.md', conventions, { overwrite: false });
writeFileSafe('docs/adr/ADR_TEMPLATE.md', adrTemplate, { overwrite: false });
}
function createCursorRules() {
const rules = `# Cursor AI Rules
You operate inside a deterministic AI-assisted development system.
## Absolute rules
- Repo (Git) is the source of truth.
- Do NOT rely on chat history as source of truth.
- Follow docs/ARCHITECTURE.md and docs/CONVENTIONS.md.
- Write small, incremental changes; prefer patch-sized PRs.
- For architectural changes: create/update an ADR and request approval.
## Commit policy
- Suggest commit messages that match CONVENTIONS.md.
- Prefer **checkpoint** commits for micro-steps during implementation.
- Prefer **main** commits when completing a meaningful slice.
## Working in Degraded Mode
If you see in docs/ARCHITECTURE.md:
"⚠️ DEGRADED MODE TEMPLATE"
**This means:**
- Documentation is incomplete
- Your recommendations are SUGGESTIONS only
- Request approval before architectural changes
**DO:**
- Ask clarifying questions
- Explain assumptions explicitly
- Suggest small changes first
**DON'T:**
- Assume architectural decisions
- Make large refactors
- Change module boundaries
## Safety
- Do not modify docs/ARCHITECTURE.md, docs/CONVENTIONS.md, or ADRs unless explicitly instructed.
- Avoid deleting files unless explicitly requested.
`;
writeFileSafe('.cursorrules', rules, { overwrite: false });
}
function createRepomixConfig() {
// Repomix config (Onion Model header + git signals)
// The actual packed content is controlled by include/ignore patterns.
const config = {
output: {
filePath: '.mcp/context.xml',
style: 'xml',
headerText:
'# AI Development System Context (Onion Model)\n' +
'Layer 0 (Always): docs/ARCHITECTURE.md, docs/CONVENTIONS.md, latest ADRs\n' +
'Layer 1 (Delta): git diffs and changed files (use scripts/generate-context.sh for incremental)\n' +
'Layer 2 (Expansion): on demand via Serena tools\n' +
'\n' +
'This snapshot represents current project reality from the repository.\n',
},
include: [
'docs/**',
'src/**',
'app/**',
'lib/**',
'backend/**',
'packages/**',
'services/**',
'README.md',
'Dockerfile',
'docker-compose.yml',
'compose.yaml',
],
ignore: {
useDefaultPatterns: true,
customPatterns: [
'node_modules/**',
'.git/**',
'.ai-dev-backup-*/**',
'.serena/cache/**',
'.serena/tmp/**',
'.serena/logs/**',
'.mcp/context.xml',
'.mcp/context_incremental.txt',
'**/*.log',
'**/*.tmp',
'**/.DS_Store',
],
},
git: {
includeDiffs: true,
includeLogs: true,
logsCount: 30,
},
};
writeFileSafe('repomix.config.json', JSON.stringify(config, null, 2) + '\n', { overwrite: false });
}
function createMcpSnippets() {
// These are EXAMPLES only. Real locations vary by OS/client.
const claudeExample = {
mcpServers: {
repomix: {
command: 'npx',
args: ['-y', 'repomix', '--mcp'],
},
serena: {
command: 'uvx',
args: ['--from', 'git+https://github.com/oraios/serena.git', 'serena', 'start-mcp-server'],
},
},
};
const cursorExample = {
mcpServers: {
repomix: {
command: 'npx',
args: ['-y', 'repomix', '--mcp'],
},
serena: {
command: 'uvx',
args: ['--from', 'git+https://github.com/oraios/serena.git', 'serena', 'start-mcp-server'],
},
},
};
writeFileSafe('.mcp/claude_desktop_config.example.json', JSON.stringify(claudeExample, null, 2) + '\n', { overwrite: false });
writeFileSafe('.mcp/cursor_mcp_config.example.json', JSON.stringify(cursorExample, null, 2) + '\n', { overwrite: false });
}
function createGitHubAction() {
const workflow = `name: Commit Policy
on:
pull_request:
push:
branches: [ main, master ]
jobs:
commit-policy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate commit messages
shell: bash
run: |
set -e
BASE="\${{ github.event.pull_request.base.sha }}"
HEAD="\${{ github.sha }}"
# For push events where BASE is empty, validate the last 50 commits.
if [ -z "$BASE" ]; then
RANGE="HEAD~50..HEAD"
else
RANGE="$BASE..$HEAD"
fi
echo "Validating commit subjects in range: $RANGE"
BAD=0
while IFS= read -r subject; do
if [[ "$subject" =~ ^(Merge\ |Revert\ ) ]]; then
continue
fi
if [[ "$subject" =~ ^(feat|fix|refactor|docs|test|chore)(\([a-z0-9_-]+\))?:\ .+ ]]; then
continue
fi
if [[ "$subject" =~ ^checkpoint(\([a-z0-9_-]+\))?:\ .+ ]]; then
continue
fi
echo "❌ Invalid commit subject: $subject"
BAD=1
done < <(git log --format=%s "$RANGE")
if [ "$BAD" -eq 1 ]; then
echo "Commit policy failed."
exit 1
fi
echo "✅ Commit policy passed."
`;
writeFileSafe('.github/workflows/commit-policy.yml', workflow, { overwrite: false });
}
function createGitHooks({ overwrite = false } = {}) {
const commitMsgHook = `#!/bin/sh
# Commit Message Policy (main + checkpoint)
MSG_FILE="$1"
SUBJECT="$(head -n 1 "$MSG_FILE" | tr -d '\\r')"
# Allow merge/revert commits
echo "$SUBJECT" | grep -Eq '^(Merge |Revert )' && exit 0
# Main commits
echo "$SUBJECT" | grep -Eq '^(feat|fix|refactor|docs|test|chore)(\\([a-z0-9_-]+\\))?: .+' && exit 0
# Checkpoint commits
echo "$SUBJECT" | grep -Eq '^checkpoint(\\([a-z0-9_-]+\\))?: .+' && exit 0
echo ""
echo "❌ Invalid commit message:"
echo " $SUBJECT"
echo ""
echo "Allowed formats:"
echo " - feat(scope): description"
echo " - fix(scope): description"
echo " - refactor(scope): description"
echo " - docs(scope): description"
echo " - test(scope): description"
echo " - chore(scope): description"
echo " - checkpoint(scope): description"
echo ""
exit 1
`;
const postCommitHook = `#!/bin/sh
# Post-commit hook: regenerate deterministic snapshot for handoff (Cursor ⇄ Claude)
# Non-fatal: commit already happened; we log errors.
LOG_DIR=".mcp"
LOG_FILE="$LOG_DIR/post-commit.log"
mkdir -p "$LOG_DIR"
echo "---- $(date) ----" >> "$LOG_FILE"
if command -v npx >/dev/null 2>&1; then
# Repomix reads repomix.config.json and writes .mcp/context.xml
npx -y repomix >> "$LOG_FILE" 2>&1 || echo "[WARN] repomix failed" >> "$LOG_FILE"
else
echo "[WARN] npx not found; cannot run repomix" >> "$LOG_FILE"
fi
# Serena is optional; do NOT auto-index here (can be heavy). Provide manual script instead.
exit 0
`;
// Backup if we are overwriting
const hookPaths = ['.git/hooks/commit-msg', '.git/hooks/post-commit'];
if (overwrite) createBackup(hookPaths);
writeFileSafe('.git/hooks/commit-msg', commitMsgHook, { overwrite });
writeFileSafe('.git/hooks/post-commit', postCommitHook, { overwrite });
chmodSafe('.git/hooks/commit-msg', 0o755);
chmodSafe('.git/hooks/post-commit', 0o755);
}
function createScripts({ overwrite = false } = {}) {
const commitCheckpoint = `#!/bin/sh
# Quick checkpoint commit (enforced by commit-msg hook)
# Usage: scripts/commit-checkpoint.sh <scope> <message...>
set -e
SCOPE="$1"
shift || true
MSG="$*"
if [ -z "$SCOPE" ] || [ -z "$MSG" ]; then
echo "Usage: scripts/commit-checkpoint.sh <scope> <message...>"
exit 1
fi
git add -A
git commit -m "checkpoint($SCOPE): $MSG"
`;
const commitMain = `#!/bin/sh
# Main commit
# Usage: scripts/commit-main.sh <type> <scope> <message...>
# type: feat|fix|refactor|docs|test|chore
set -e
TYPE="$1"
SCOPE="$2"
shift 2 || true
MSG="$*"
if [ -z "$TYPE" ] || [ -z "$SCOPE" ] || [ -z "$MSG" ]; then
echo "Usage: scripts/commit-main.sh <type> <scope> <message...>"
exit 1
fi
git add -A
git commit -m "$TYPE($SCOPE): $MSG"
`;
const genContext = `#!/bin/sh
# Incremental context generator (Onion Model)
# Usage: scripts/generate-context.sh [base_branch] [output_file]
set -e
BASE_BRANCH="\${1:-main}"
OUT="\${2:-.mcp/context_incremental.txt}"
mkdir -p .mcp
echo "# AI Development System Context (Incremental / Onion Model)" > "$OUT"
echo "" >> "$OUT"
# Layer 0 — Laws
for f in docs/ARCHITECTURE.md docs/CONVENTIONS.md; do
if [ -f "$f" ]; then
echo "=== $f ===" >> "$OUT"
cat "$f" >> "$OUT"
echo "" >> "$OUT"
fi
done
# Latest ADRs (up to 5)
ls -1t docs/adr/ADR-*.md 2>/dev/null | head -n 5 | while read -r adr; do
echo "--- $adr ---" >> "$OUT"
cat "$adr" >> "$OUT"
echo "" >> "$OUT"
done
# Layer 1 — Delta
echo "=== git diff $BASE_BRANCH...HEAD ===" >> "$OUT"
git diff "$BASE_BRANCH...HEAD" >> "$OUT" || true
echo "" >> "$OUT"
# Changed files content
CHANGED=$(git diff --name-only "$BASE_BRANCH...HEAD" || true)
for file in $CHANGED; do
if [ -f "$file" ]; then
echo "=== $file ===" >> "$OUT"
cat "$file" >> "$OUT"
echo "" >> "$OUT"
fi
done
echo "✅ Wrote incremental context: $OUT"
`;
const adrCreate = `#!/bin/sh
# Create ADR from template with auto-increment ID
# Usage: scripts/create-adr.sh <slug>
set -e
SLUG="$1"
if [ -z "$SLUG" ]; then
echo "Usage: scripts/create-adr.sh <slug>"
exit 1
fi
mkdir -p docs/adr
LAST=$(ls -1 docs/adr/ADR-[0-9][0-9][0-9]-*.md 2>/dev/null | sed -E 's/.*ADR-([0-9]+)-.*/\\1/' | sort -n | tail -1)
if [ -z "$LAST" ]; then
NEXT=1
else
NEXT=$((LAST + 1))
fi
ID=$(printf "%03d" "$NEXT")
FILE="docs/adr/ADR-$ID-$SLUG.md"
cat > "$FILE" <<'EOF'
# ADR-XXX: <Title>
**Status:** Draft | Accepted | Rejected
**Date:** YYYY-MM-DD
**Deciders:** <names>
## Context
<What problem are we solving?>
## Decision
<What did we decide?>
## Rationale
<Why this decision?>
## Consequences
<Good and bad effects>
## Alternatives Considered
<Other options>
## References
- Links / docs / PRs
EOF
# Replace placeholders
DATE=$(date +%Y-%m-%d)
sed -i.bak -e "s/ADR-XXX/ADR-$ID/g" -e "s/YYYY-MM-DD/$DATE/g" "$FILE" 2>/dev/null || true
rm -f "$FILE.bak" 2>/dev/null || true
echo "✅ Created $FILE"
echo "Next:"
echo " 1) Fill sections"
echo " 2) Commit: git commit -m \"docs(adr): add ADR-$ID $SLUG\""
`;
const serenaIndex = `#!/bin/sh
# Optional: index the project for Serena (can be heavy)
# Requires uv/uvx or python environment; see Serena docs.
# Usage: scripts/serena-index.sh
set -e
echo "Starting Serena MCP server / indexing is environment-specific."
echo "Suggested (uvx) command:"
echo " uvx --from git+https://github.com/oraios/serena.git serena project index"
echo ""
echo "If you already have Serena installed as a CLI, run:"
echo " serena project index"
`;
// Windows convenience wrappers (optional)
const commitCheckpointBat = `@echo off
setlocal enabledelayedexpansion
set SCOPE=%1
if "%SCOPE%"=="" goto usage
shift
set "MSG="
set "FIRST=1"
:buildmsg
if "%~1"=="" goto checkmsg
if "!FIRST!"=="1" (set "MSG=%~1" & set "FIRST=0") else (set "MSG=!MSG! %~1")
shift
goto buildmsg
:checkmsg
if "%MSG%"=="" goto usage
git add -A
git commit -m "checkpoint(%SCOPE%): %MSG%"
exit /b 0
:usage
echo Usage: scripts\\commit-checkpoint.bat ^<scope^> ^<message...^>
exit /b 1
`;
const commitMainBat = `@echo off
setlocal enabledelayedexpansion
set TYPE=%1
set SCOPE=%2
if "%TYPE%"=="" goto usage
if "%SCOPE%"=="" goto usage
shift
shift
set "MSG="
set "FIRST=1"
:buildmsg
if "%~1"=="" goto checkmsg
if "!FIRST!"=="1" (set "MSG=%~1" & set "FIRST=0") else (set "MSG=!MSG! %~1")
shift
goto buildmsg
:checkmsg
if "%MSG%"=="" goto usage
git add -A
git commit -m "%TYPE%(%SCOPE%): %MSG%"
exit /b 0
:usage
echo Usage: scripts\\commit-main.bat ^<type^> ^<scope^> ^<message...^>
echo type: feat^|fix^|refactor^|docs^|test^|chore
exit /b 1
`;
const genContextBat = `@echo off
setlocal enabledelayedexpansion
set BASE_BRANCH=%1
if "%BASE_BRANCH%"=="" set BASE_BRANCH=main
set OUT=%2
if "%OUT%"=="" set OUT=.mcp\context_incremental.txt
if not exist .mcp mkdir .mcp
echo # AI Development System Context (Incremental / Onion Model)> "%OUT%"
echo.>> "%OUT%"
for %%F in (docs\ARCHITECTURE.md docs\CONVENTIONS.md) do (
if exist %%F (
echo === %%F ===>> "%OUT%"
type %%F>> "%OUT%"
echo.>> "%OUT%"
)
)
for /f "delims=" %%A in ('dir /b /o-d docs\adr\ADR-*.md 2^>nul') do (
echo --- docs\adr\%%A --->> "%OUT%"
type docs\adr\%%A>> "%OUT%"
echo.>> "%OUT%"
goto afteradr
)
:afteradr
echo === git diff %BASE_BRANCH%...HEAD ===>> "%OUT%"
git diff %BASE_BRANCH%...HEAD>> "%OUT%" 2>nul
echo.>> "%OUT%"
for /f "delims=" %%F in ('git diff --name-only %BASE_BRANCH%...HEAD 2^>nul') do (
if exist %%F (
echo === %%F ===>> "%OUT%"
type %%F>> "%OUT%"
echo.>> "%OUT%"
)
)
echo Wrote incremental context: %OUT%
`;
writeFileSafe('scripts/commit-checkpoint.sh', commitCheckpoint, { overwrite });
writeFileSafe('scripts/commit-main.sh', commitMain, { overwrite });
writeFileSafe('scripts/generate-context.sh', genContext, { overwrite });
writeFileSafe('scripts/create-adr.sh', adrCreate, { overwrite });
writeFileSafe('scripts/serena-index.sh', serenaIndex, { overwrite });
// Windows scripts
writeFileSafe('scripts/commit-checkpoint.bat', commitCheckpointBat, { overwrite });
writeFileSafe('scripts/commit-main.bat', commitMainBat, { overwrite });
writeFileSafe('scripts/generate-context.bat', genContextBat, { overwrite });
chmodSafe('scripts/commit-checkpoint.sh', 0o755);
chmodSafe('scripts/commit-main.sh', 0o755);
chmodSafe('scripts/generate-context.sh', 0o755);
chmodSafe('scripts/create-adr.sh', 0o755);
chmodSafe('scripts/serena-index.sh', 0o755);
}
function resolveClaudeDesktopConfigPath() {
// Best-effort paths; may vary by install/OS.
if (process.platform === 'win32') {
const appdata = process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming');
return path.join(appdata, 'Claude', 'claude_desktop_config.json');
}
if (process.platform === 'darwin') {
return path.join(os.homedir(), 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json');
}
// linux
return path.join(os.homedir(), '.config', 'Claude', 'claude_desktop_config.json');
}
function deepMerge(obj, patch) {
if (!patch || typeof patch !== 'object') return obj;
const out = Array.isArray(obj) ? [...obj] : { ...(obj || {}) };
for (const [k, v] of Object.entries(patch)) {
if (v && typeof v === 'object' && !Array.isArray(v)) {
out[k] = deepMerge(out[k], v);
} else {
out[k] = v;
}
}
return out;
}
function setupMcpForClaudeDesktop({ overwriteExistingServers = true } = {}) {
const cfgPath = resolveClaudeDesktopConfigPath();
const dir = path.dirname(cfgPath);
ensureDir(dir);
const existingRaw = safeRead(cfgPath);
let existing = {};
if (existingRaw && existingRaw.trim()) {
try {
existing = JSON.parse(existingRaw);
} catch {
// If config is invalid JSON, keep a backup and start fresh
createBackup([cfgPath]);
existing = {};
}
}
const patch = {
mcpServers: {
repomix: {
command: 'npx',
args: ['-y', 'repomix', '--mcp'],
},
serena: {
command: 'uvx',
args: ['--from', 'git+https://github.com/oraios/serena.git', 'serena', 'start-mcp-server'],
},
},
};
// If user doesn't want to overwrite existing server definitions, respect them.
if (!overwriteExistingServers && existing && existing.mcpServers) {