-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclaude-setup
More file actions
executable file
·937 lines (834 loc) · 31.5 KB
/
claude-setup
File metadata and controls
executable file
·937 lines (834 loc) · 31.5 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
#!/bin/bash
#
# claude-setup v0.1
# Setup Claude Code infrastructure with framework detection
#
# Usage:
# ./claude-setup # Interactive mode
# ./claude-setup --force # Overwrite existing .claude
# ./claude-setup --merge # Merge with existing .claude (skip duplicates)
# ./claude-setup --yes # Auto-detect, no prompts
# ./claude-setup --help # Show help
#
set -e # Exit on error
VERSION="0.3.0"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Flags
FORCE_MODE=false
YES_MODE=false
DEBUG_MODE=false
MERGE_MODE=false
# Parse arguments
for arg in "$@"; do
case $arg in
--force|-f)
FORCE_MODE=true
shift
;;
--yes|-y)
YES_MODE=true
shift
;;
--debug|-d)
DEBUG_MODE=true
shift
;;
--merge|-m)
MERGE_MODE=true
shift
;;
--help|-h)
echo "claude-setup v$VERSION"
echo ""
echo "Setup Claude Code infrastructure with framework detection"
echo ""
echo "Usage:"
echo " claude-setup Interactive mode with detection"
echo " claude-setup --force Overwrite existing .claude directory"
echo " claude-setup --merge Merge with existing .claude (skip duplicates)"
echo " claude-setup --yes Auto-detect, no prompts"
echo " claude-setup --debug Enable debug output for troubleshooting"
echo " claude-setup --help Show this help"
echo ""
exit 0
;;
*)
echo "Unknown option: $arg"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Determine script directory (handles npm global install and symlinks)
SOURCE="${BASH_SOURCE[0]}"
# Resolve symlinks
while [ -L "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# Check if running from npm install or from repo
if [ -d "$SCRIPT_DIR/cli/core" ]; then
# Running from repo root
TEMPLATE_DIR="$SCRIPT_DIR/cli"
elif [ -d "$SCRIPT_DIR/core" ]; then
# Running from npm install (shouldn't happen, but keep as fallback)
TEMPLATE_DIR="$SCRIPT_DIR"
else
echo -e "${RED}Error: Cannot find core templates directory${NC}"
echo "Looked in: $SCRIPT_DIR/cli/core and $SCRIPT_DIR/core"
exit 1
fi
PROJECT_DIR=$(pwd)
# Header
echo ""
echo -e "${BLUE}┌────────────────────────────────────────┐${NC}"
echo -e "${BLUE}│ Claude Code Setup v$VERSION │${NC}"
echo -e "${BLUE}└────────────────────────────────────────┘${NC}"
echo ""
# Step 1: Check if .claude exists
if [ -d ".claude" ]; then
if [ "$FORCE_MODE" = true ]; then
echo -e "${YELLOW}⚠ Removing existing .claude directory (--force)${NC}"
rm -rf .claude
elif [ "$MERGE_MODE" = true ]; then
echo -e "${BLUE}ℹ Merging with existing .claude directory (--merge)${NC}"
echo -e "${BLUE} Existing files will be preserved${NC}"
else
echo -e "${YELLOW}⚠ .claude directory already exists${NC}"
if [ "$YES_MODE" = false ]; then
echo ""
echo "Choose an option:"
echo " [o] Overwrite entire .claude directory"
echo " [m] Merge (keep existing files, add new ones)"
echo " [c] Cancel"
read -p "Your choice (o/m/C): " -n 1 -r
echo
case $REPLY in
[Oo])
rm -rf .claude
;;
[Mm])
MERGE_MODE=true
echo -e "${BLUE}ℹ Merging with existing .claude directory${NC}"
;;
*)
echo "Cancelled."
exit 0
;;
esac
else
echo "Use --force to overwrite, --merge to merge, or remove .claude manually"
exit 1
fi
fi
fi
# Step 2: Detect available kits
echo -e "${BLUE}Detecting project frameworks...${NC}"
echo ""
# Helper function to get display name from kit.json (Bash 3 compatible)
get_display_name() {
local kit_dir="$1"
local kit_json="$kit_dir/kit.json"
if command -v jq &> /dev/null; then
jq -r '.displayName // .name' "$kit_json" 2>/dev/null || basename "$kit_dir"
else
basename "$kit_dir"
fi
}
# Discover all available kits
declare -a ALL_KITS
declare -a DETECTED_KITS
if [ -d "$TEMPLATE_DIR/kits" ]; then
for kit_dir in "$TEMPLATE_DIR/kits"/*; do
if [ -d "$kit_dir" ] && [ -f "$kit_dir/kit.json" ]; then
kit_name=$(basename "$kit_dir")
ALL_KITS+=("$kit_name")
fi
done
fi
# Function to detect if this is a monorepo and get workspace patterns
detect_monorepo() {
local workspace_paths=""
# 1. Check for pnpm-workspace.yaml (PRIMARY FIX for pnpm monorepos)
if [ -f pnpm-workspace.yaml ]; then
# Parse pnpm-workspace.yaml patterns
# Format: " - 'pattern'" or " - pattern"
workspace_paths=$(grep -E "^[[:space:]]*-[[:space:]]*" pnpm-workspace.yaml 2>/dev/null | \
sed -E "s/^[[:space:]]*-[[:space:]]*['\"]?([^'\"]+)['\"]?[[:space:]]*$/\1/" | \
tr '\n' ' ')
# 2. Check for Yarn/npm workspaces in package.json
elif [ -f package.json ] && grep -q '"workspaces"' package.json 2>/dev/null; then
workspace_paths=$(get_workspace_paths)
# 3. Check for lerna.json
elif [ -f lerna.json ]; then
if command -v jq &> /dev/null; then
workspace_paths=$(jq -r '.packages[]? // empty' lerna.json 2>/dev/null | tr '\n' ' ')
fi
# Lerna default if no packages specified
[ -z "$workspace_paths" ] && workspace_paths="packages/*"
fi
# Fallback: Check if common workspace directories exist
if [ -z "$workspace_paths" ]; then
local found_dirs=""
for dir in apps packages services libs modules tools; do
if [ -d "$dir" ] && [ -n "$(ls -A "$dir" 2>/dev/null)" ]; then
found_dirs="$found_dirs $dir/*"
fi
done
workspace_paths="$found_dirs"
fi
echo "$workspace_paths"
}
# Function to extract workspace paths from package.json
get_workspace_paths() {
local paths=""
# Try to use jq if available for accurate parsing
if command -v jq &> /dev/null; then
# Handle both array format: {"workspaces": ["apps/*"]}
# and object format: {"workspaces": {"packages": ["apps/*"]}}
paths=$(jq -r '
if .workspaces then
if (.workspaces | type) == "array" then
.workspaces[]
elif (.workspaces | type) == "object" and .workspaces.packages then
.workspaces.packages[]
else
empty
end
else
empty
end
' package.json 2>/dev/null)
fi
# Fallback: Use expanded common patterns if jq not available or no workspaces found
if [ -z "$paths" ]; then
paths="apps/* packages/* services/* libs/* modules/* tools/* internal/* components/*"
fi
echo "$paths"
}
# Function to run detection in root and monorepo subdirectories
run_detection_multipath() {
local detect_cmd="$1"
local debug="${DEBUG_MODE:-false}"
# Fast path: Try root directory first (single-service projects)
if [ "$debug" = true ]; then
echo " [DEBUG] Checking root directory..." >&2
echo " [DEBUG] Running: $detect_cmd" >&2
fi
if eval "$detect_cmd" 2>/dev/null; then
[ "$debug" = true ] && echo " [DEBUG] ✓ Detected in root" >&2
return 0
fi
[ "$debug" = true ] && echo " [DEBUG] ✗ Not found in root" >&2
# Check for monorepo configuration (pnpm, npm, yarn, lerna)
local workspace_paths=$(detect_monorepo)
if [ -n "$workspace_paths" ]; then
if [ "$debug" = true ]; then
echo " [DEBUG] Monorepo detected" >&2
echo " [DEBUG] Workspace paths: $workspace_paths" >&2
fi
# Search each workspace path pattern
for pattern in $workspace_paths; do
# Enable nullglob to handle patterns with no matches gracefully
shopt -s nullglob
# Force glob expansion by assigning to array
expanded_dirs=($pattern)
shopt -u nullglob
[ "$debug" = true ] && echo " [DEBUG] Checking pattern: $pattern" >&2
# Iterate over expanded directories
for subdir in "${expanded_dirs[@]}"; do
if [ -d "$subdir" ]; then
if [ "$debug" = true ]; then
echo " [DEBUG] Checking: $subdir" >&2
echo " [DEBUG] Running in $subdir: $detect_cmd" >&2
fi
# Run detection in subdirectory (use subshell to preserve cwd)
if (cd "$subdir" && eval "$detect_cmd" 2>/dev/null); then
[ "$debug" = true ] && echo " [DEBUG] ✓ Detected in $subdir" >&2
return 0
fi
[ "$debug" = true ] && echo " [DEBUG] ✗ Not found in $subdir" >&2
fi
done
done
else
[ "$debug" = true ] && echo " [DEBUG] No monorepo configuration detected" >&2
fi
return 1
}
# Detect which kits match the project
if [ "$DEBUG_MODE" = true ]; then
echo -e "${YELLOW}[DEBUG MODE ENABLED]${NC}"
echo ""
fi
for kit_name in "${ALL_KITS[@]}"; do
kit_dir="$TEMPLATE_DIR/kits/$kit_name"
kit_json="$kit_dir/kit.json"
if [ "$DEBUG_MODE" = true ]; then
echo -e "${YELLOW}Checking kit: $kit_name${NC}"
fi
if [ -f "$kit_json" ]; then
# Extract detect command
if command -v jq &> /dev/null; then
detect_cmd=$(jq -r '.detect.command // ""' "$kit_json" 2>/dev/null)
else
# Fallback: extract command field (less reliable, install jq for best results)
# Extract from opening quote to last quote on line, handling escaped quotes inside
detect_cmd=$(grep '"command"' "$kit_json" | sed -n 's/.*"command"[[:space:]]*:[[:space:]]*"\(.*\)"[^"]*$/\1/p' || echo "")
fi
# Run detection command (monorepo-aware)
if [ -n "$detect_cmd" ]; then
if run_detection_multipath "$detect_cmd"; then
DETECTED_KITS+=("$kit_name")
display_name=$(get_display_name "$kit_dir")
echo -e "${GREEN}✓${NC} Detected: $display_name"
elif [ "$DEBUG_MODE" = true ]; then
display_name=$(get_display_name "$kit_dir")
echo -e "${RED}✗${NC} Not detected: $display_name"
fi
elif [ "$DEBUG_MODE" = true ]; then
echo " [DEBUG] No detect command found in kit.json"
fi
fi
[ "$DEBUG_MODE" = true ] && echo ""
done
if [ ${#DETECTED_KITS[@]} -eq 0 ]; then
echo -e "${YELLOW}ℹ${NC} No frameworks auto-detected"
fi
echo ""
# Step 3: Kit selection
declare -a KITS_TO_INSTALL
if [ "$YES_MODE" = true ]; then
# Auto-install all detected kits
KITS_TO_INSTALL=("${DETECTED_KITS[@]}")
if [ ${#KITS_TO_INSTALL[@]} -gt 0 ]; then
echo -e "${BLUE}Auto-installing detected kits:${NC}"
for kit in "${KITS_TO_INSTALL[@]}"; do
display_name=$(get_display_name "$TEMPLATE_DIR/kits/$kit")
echo -e " - $display_name"
done
else
echo -e "${BLUE}Installing core infrastructure only${NC}"
fi
else
# Interactive selection
if [ ${#DETECTED_KITS[@]} -gt 0 ]; then
echo -e "${BLUE}Detected kits available:${NC}"
for kit in "${DETECTED_KITS[@]}"; do
display_name=$(get_display_name "$TEMPLATE_DIR/kits/$kit")
read -p "Install $display_name kit? (Y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
KITS_TO_INSTALL+=("$kit")
fi
done
fi
# Ask if user wants to see additional kits
echo ""
read -p "Install additional kits? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo -e "${BLUE}Additional kits available:${NC}"
for kit in "${ALL_KITS[@]}"; do
# Skip if already in detected kits
if [[ " ${DETECTED_KITS[@]} " =~ " ${kit} " ]]; then
continue
fi
display_name=$(get_display_name "$TEMPLATE_DIR/kits/$kit")
read -p "Install $display_name kit? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
KITS_TO_INSTALL+=("$kit")
fi
done
fi
fi
echo ""
echo -e "${BLUE}Installing infrastructure...${NC}"
echo ""
# Step 4: Create .claude directory structure
mkdir -p .claude/{skills,hooks,agents,commands}
# Step 5: Copy core infrastructure
echo -e "${GREEN}✓${NC} Creating .claude directory"
# Copy hooks (merge mode - skip existing files)
HOOKS_COPIED=0
HOOKS_SKIPPED=0
for hook_file in "$TEMPLATE_DIR/core/hooks/"*; do
hook_name=$(basename "$hook_file")
if [ -e ".claude/hooks/$hook_name" ]; then
HOOKS_SKIPPED=$((HOOKS_SKIPPED + 1))
else
cp -r "$hook_file" .claude/hooks/
HOOKS_COPIED=$((HOOKS_COPIED + 1))
fi
done
chmod +x .claude/hooks/*.sh 2>/dev/null || true
if [ $HOOKS_SKIPPED -gt 0 ]; then
echo -e "${GREEN}✓${NC} Installed $HOOKS_COPIED hook(s), skipped $HOOKS_SKIPPED existing hook(s)"
else
echo -e "${GREEN}✓${NC} Installed core hooks (skill-activation-prompt, post-tool-use-tracker)"
fi
# Copy skill-developer (merge mode - skip if exists)
if [ -d ".claude/skills/skill-developer" ]; then
echo -e "${YELLOW}⚠${NC} Skipped skill-developer (already exists)"
else
cp -r "$TEMPLATE_DIR/core/skills/skill-developer" .claude/skills/
echo -e "${GREEN}✓${NC} Installed skill-developer"
fi
# Copy core agents (merge mode - skip existing files)
AGENTS_COPIED=0
AGENTS_SKIPPED=0
for agent_file in "$TEMPLATE_DIR/core/agents/"*.md; do
[ -e "$agent_file" ] || continue
agent_name=$(basename "$agent_file")
if [ -e ".claude/agents/$agent_name" ]; then
AGENTS_SKIPPED=$((AGENTS_SKIPPED + 1))
else
cp "$agent_file" .claude/agents/
AGENTS_COPIED=$((AGENTS_COPIED + 1))
fi
done
if [ $AGENTS_COPIED -gt 0 ] || [ $AGENTS_SKIPPED -gt 0 ]; then
if [ $AGENTS_SKIPPED -gt 0 ]; then
echo -e "${GREEN}✓${NC} Installed $AGENTS_COPIED agent(s), skipped $AGENTS_SKIPPED existing agent(s)"
else
echo -e "${GREEN}✓${NC} Installed core agents ($AGENTS_COPIED files)"
fi
fi
# Copy core commands (merge mode - skip existing files)
COMMANDS_COPIED=0
COMMANDS_SKIPPED=0
for command_file in "$TEMPLATE_DIR/core/commands/"*.md; do
[ -e "$command_file" ] || continue
command_name=$(basename "$command_file")
if [ -e ".claude/commands/$command_name" ]; then
COMMANDS_SKIPPED=$((COMMANDS_SKIPPED + 1))
else
cp "$command_file" .claude/commands/
COMMANDS_COPIED=$((COMMANDS_COPIED + 1))
fi
done
if [ $COMMANDS_COPIED -gt 0 ] || [ $COMMANDS_SKIPPED -gt 0 ]; then
if [ $COMMANDS_SKIPPED -gt 0 ]; then
echo -e "${GREEN}✓${NC} Installed $COMMANDS_COPIED command(s), skipped $COMMANDS_SKIPPED existing command(s)"
else
echo -e "${GREEN}✓${NC} Installed slash commands ($COMMANDS_COPIED files)"
fi
fi
# Step 6: Install selected kits
INSTALLED_SKILLS=""
for kit in "${KITS_TO_INSTALL[@]}"; do
kit_dir="$TEMPLATE_DIR/kits/$kit"
if [ ! -d "$kit_dir" ]; then
echo -e "${YELLOW}⚠${NC} Warning: Kit directory not found: $kit"
continue
fi
# Install skills from kit (merge mode - skip existing)
if [ -d "$kit_dir/skills" ]; then
for skill_dir in "$kit_dir/skills"/*; do
if [ -d "$skill_dir" ]; then
skill_name=$(basename "$skill_dir")
if [ -d ".claude/skills/$skill_name" ]; then
echo -e " ${YELLOW}⚠${NC} Skipped skill: $skill_name (already exists)"
else
cp -r "$skill_dir" .claude/skills/
INSTALLED_SKILLS="$INSTALLED_SKILLS $skill_name"
fi
fi
done
fi
# Install agents from kit (merge mode - skip existing files)
if [ -d "$kit_dir/agents" ]; then
for agent_file in "$kit_dir/agents/"*.md; do
[ -e "$agent_file" ] || continue
agent_name=$(basename "$agent_file")
if [ -e ".claude/agents/$agent_name" ]; then
echo -e " ${YELLOW}⚠${NC} Skipped agent: $agent_name (already exists)"
else
cp "$agent_file" .claude/agents/
fi
done
fi
# Install commands from kit (merge mode - skip existing files)
if [ -d "$kit_dir/commands" ]; then
for command_file in "$kit_dir/commands/"*.md; do
[ -e "$command_file" ] || continue
command_name=$(basename "$command_file")
if [ -e ".claude/commands/$command_name" ]; then
echo -e " ${YELLOW}⚠${NC} Skipped command: $command_name (already exists)"
else
cp "$command_file" .claude/commands/
fi
done
fi
display_name=$(get_display_name "$kit_dir")
echo -e "${GREEN}✓${NC} Installed $display_name kit"
done
if [ ${#KITS_TO_INSTALL[@]} -eq 0 ]; then
echo -e "${BLUE}No kits selected - core infrastructure only${NC}"
fi
# Step 7: Create or merge settings.json
echo ""
echo -e "${BLUE}Configuring Claude Code settings...${NC}"
# Define new hooks configuration
NEW_HOOKS_CONFIG=$(cat << 'HOOKEOF'
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/skill-activation-prompt.sh"
}
]
}
],
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/post-tool-use-tracker.sh"
}
]
}
]
},
"permissions": {
"editPermissions": "auto-accept",
"writePermissions": "auto-accept"
}
}
HOOKEOF
)
if [ -f ".claude/settings.json" ]; then
echo -e "${YELLOW}Existing settings.json found - merging configurations${NC}"
if command -v jq &> /dev/null; then
# Backup existing settings
cp .claude/settings.json .claude/settings.json.backup
# Merge hooks using jq
# This preserves existing hooks and adds new ones if they don't exist
echo "$NEW_HOOKS_CONFIG" | jq -s '
.[0] as $existing |
.[1] as $new |
$existing |
.hooks.UserPromptSubmit = (
($existing.hooks.UserPromptSubmit // []) +
($new.hooks.UserPromptSubmit // [])
) |
.hooks.PostToolUse = (
($existing.hooks.PostToolUse // []) +
($new.hooks.PostToolUse // [])
) |
.permissions = ($existing.permissions // $new.permissions)
' .claude/settings.json.backup - > .claude/settings.json
echo -e "${GREEN}✓${NC} Merged settings.json (backup saved as settings.json.backup)"
else
echo -e "${YELLOW}⚠${NC} jq not found - cannot merge settings.json"
echo " Your existing settings.json was preserved"
echo " You'll need to manually add the hooks from the installation"
fi
else
# Create new settings.json
echo "$NEW_HOOKS_CONFIG" > .claude/settings.json
echo -e "${GREEN}✓${NC} Created settings.json"
fi
# Step 7b: Optional Stop hooks (for build checking)
echo ""
echo -e "${BLUE}Checking project structure for Stop hooks...${NC}"
# Detect project type
PACKAGE_JSON_COUNT=$(find . -maxdepth 3 -name "package.json" -not -path "*/node_modules/*" 2>/dev/null | wc -l | tr -d ' ')
if [ "$PACKAGE_JSON_COUNT" -gt 3 ]; then
echo -e "${YELLOW}Detected monorepo structure ($PACKAGE_JSON_COUNT package.json files)${NC}"
echo ""
echo "Stop hooks can automatically check TypeScript compilation on save."
echo -e "${YELLOW}⚠ WARNING: Stop hooks require customization for your project structure${NC}"
echo " - tsc-check.sh has hardcoded service names that need updating"
echo " - See .claude/hooks/README.md and CONFIG.md for customization guide"
echo ""
if [ "$YES_MODE" = true ]; then
ENABLE_STOP_HOOKS="n"
echo "Skipping Stop hooks in --yes mode (requires manual configuration)"
else
read -p "Enable Stop hooks? (requires customization) [y/N]: " ENABLE_STOP_HOOKS
fi
if [[ "$ENABLE_STOP_HOOKS" =~ ^[Yy]$ ]]; then
echo -e "${BLUE}Adding Stop hooks to settings.json...${NC}"
if command -v jq &> /dev/null; then
# Add Stop hooks configuration
jq '.hooks.Stop = [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/tsc-check.sh"
},
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/trigger-build-resolver.sh"
},
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/error-handling-reminder.sh"
}
]
}
]' .claude/settings.json > .claude/settings.tmp.json
mv .claude/settings.tmp.json .claude/settings.json
echo -e "${GREEN}✓${NC} Stop hooks enabled"
echo -e "${YELLOW}⚠${NC} Remember to customize .claude/hooks/tsc-check.sh with your service names"
echo " See .claude/hooks/CONFIG.md for instructions"
else
echo -e "${YELLOW}⚠${NC} jq not found - cannot add Stop hooks"
fi
else
echo -e "${BLUE}ℹ${NC} Skipped Stop hooks (you can add them later if needed)"
fi
else
echo -e "${BLUE}Single-service project detected${NC}"
echo -e "${BLUE}ℹ${NC} Stop hooks not recommended for single-service projects"
echo " (They are designed for monorepos with multiple services)"
fi
# Step 8: Create skill-rules.json
# Start with base structure from skill-developer fragment
if [ -f ".claude/skills/skill-developer/skill-rules-fragment.json" ] && command -v jq &> /dev/null; then
# Use the skill-developer fragment as the base
jq '{"version": "1.0", "skills": .}' .claude/skills/skill-developer/skill-rules-fragment.json > .claude/skills/skill-rules.json
else
# Fallback to minimal base if jq not available or fragment missing
cat > .claude/skills/skill-rules.json << 'EOF'
{
"version": "1.0",
"skills": {
"skill-developer": {
"type": "domain",
"enforcement": "suggest",
"priority": "high",
"promptTriggers": {
"keywords": ["skill", "create skill", "skill triggers", "skill-rules.json"]
}
}
}
}
EOF
fi
# Merge skill-rules fragments from installed kits
for kit in "${KITS_TO_INSTALL[@]}"; do
kit_dir="$TEMPLATE_DIR/kits/$kit"
# Look for fragments in each skill directory
if [ -d "$kit_dir/skills" ]; then
for skill_dir in "$kit_dir/skills"/*; do
if [ -d "$skill_dir" ]; then
fragment_file="$skill_dir/skill-rules-fragment.json"
if [ -f "$fragment_file" ]; then
if command -v jq &> /dev/null; then
# Use jq to merge the fragment into skill-rules.json
# Fragment is structured as {"skill-name": {...}}, not {"skills": {...}}
jq -s '.[0] * {"skills": (.[0].skills + .[1])}' \
.claude/skills/skill-rules.json "$fragment_file" > .claude/skills/skill-rules.tmp.json
mv .claude/skills/skill-rules.tmp.json .claude/skills/skill-rules.json
else
echo -e "${YELLOW}⚠${NC} jq not found - skipping skill-rules merge for $(basename "$skill_dir")"
echo " Install jq for automatic skill trigger configuration"
fi
fi
fi
done
fi
done
echo -e "${GREEN}✓${NC} Created skill-rules.json"
# Step 8b: Merge agent-rules fragments from core agents
echo ""
echo -e "${BLUE}Merging agent activation rules...${NC}"
# Add agents property to skill-rules.json if it doesn't exist
if command -v jq &> /dev/null; then
# Check if agents property exists, if not add it
if ! jq -e '.agents' .claude/skills/skill-rules.json > /dev/null 2>&1; then
jq '. + {"agents": {}}' .claude/skills/skill-rules.json > .claude/skills/skill-rules.tmp.json
mv .claude/skills/skill-rules.tmp.json .claude/skills/skill-rules.json
fi
# Merge core agent rules
for agent_rules in "$TEMPLATE_DIR/core/agents/"*-rules.json; do
if [ -f "$agent_rules" ]; then
# Each agent-rules file contains {"agent-name": {...}}
jq -s '.[0] * {"agents": (.[0].agents + .[1])}' \
.claude/skills/skill-rules.json "$agent_rules" > .claude/skills/skill-rules.tmp.json
mv .claude/skills/skill-rules.tmp.json .claude/skills/skill-rules.json
fi
done
# Merge agent rules from installed kits
for kit in "${KITS_TO_INSTALL[@]}"; do
kit_dir="$TEMPLATE_DIR/kits/$kit"
# Look for agent-rules fragments in kit agent directories
if [ -d "$kit_dir/agents" ]; then
for agent_rules in "$kit_dir/agents/"*-rules.json; do
if [ -f "$agent_rules" ]; then
jq -s '.[0] * {"agents": (.[0].agents + .[1])}' \
.claude/skills/skill-rules.json "$agent_rules" > .claude/skills/skill-rules.tmp.json
mv .claude/skills/skill-rules.tmp.json .claude/skills/skill-rules.json
fi
done
fi
done
echo -e "${GREEN}✓${NC} Merged agent activation rules"
else
echo -e "${YELLOW}⚠${NC} jq not found - skipping agent-rules merge"
echo " Agent auto-activation will not work without jq"
fi
# Step 8c: Validate generated JSON files
echo ""
echo -e "${BLUE}Validating configuration files...${NC}"
VALIDATION_FAILED=false
# Validate settings.json
if [ -f ".claude/settings.json" ]; then
if command -v jq &> /dev/null; then
if jq empty .claude/settings.json 2>/dev/null; then
echo -e "${GREEN}✓${NC} settings.json is valid"
else
echo -e "${RED}✗${NC} settings.json is invalid JSON"
VALIDATION_FAILED=true
fi
else
echo -e "${YELLOW}⚠${NC} Cannot validate settings.json (jq not found)"
fi
else
echo -e "${RED}✗${NC} settings.json not found"
VALIDATION_FAILED=true
fi
# Validate skill-rules.json
if [ -f ".claude/skills/skill-rules.json" ]; then
if command -v jq &> /dev/null; then
if jq empty .claude/skills/skill-rules.json 2>/dev/null; then
echo -e "${GREEN}✓${NC} skill-rules.json is valid"
else
echo -e "${RED}✗${NC} skill-rules.json is invalid JSON"
VALIDATION_FAILED=true
fi
else
echo -e "${YELLOW}⚠${NC} Cannot validate skill-rules.json (jq not found)"
fi
else
echo -e "${RED}✗${NC} skill-rules.json not found"
VALIDATION_FAILED=true
fi
if [ "$VALIDATION_FAILED" = true ]; then
echo ""
echo -e "${RED}✗${NC} Installation completed with validation errors"
echo " Check the files above for JSON syntax issues"
exit 1
fi
# Step 9: Install hook dependencies
echo ""
echo -e "${BLUE}Installing hook dependencies...${NC}"
cd .claude/hooks
if npm install --silent > /dev/null 2>&1; then
echo -e "${GREEN}✓${NC} Hook dependencies installed"
else
echo -e "${YELLOW}⚠${NC} Failed to install hook dependencies (you can run 'cd .claude/hooks && npm install' manually)"
fi
cd "$PROJECT_DIR"
# Step 10: Update .gitignore
if [ -f .gitignore ]; then
if ! grep -q "# Claude Code" .gitignore; then
echo "" >> .gitignore
echo "# Claude Code" >> .gitignore
echo ".claude/settings.local.json" >> .gitignore
echo ".claude/hooks/node_modules/" >> .gitignore
echo -e "${GREEN}✓${NC} Updated .gitignore"
fi
else
cat > .gitignore << 'EOF'
# Claude Code
.claude/settings.local.json
.claude/hooks/node_modules/
EOF
echo -e "${GREEN}✓${NC} Created .gitignore"
fi
# Step 11: Summary
echo ""
echo -e "${BLUE}┌────────────────────────────────────────┐${NC}"
echo -e "${BLUE}│ Setup Complete! 🎉 │${NC}"
echo -e "${BLUE}└────────────────────────────────────────┘${NC}"
echo ""
echo "Files created:"
echo " .claude/hooks/ (2 essential hooks)"
echo " .claude/skills/ (skill-developer$INSTALLED_SKILLS)"
echo " .claude/agents/ ($(ls -1 .claude/agents/*.md 2>/dev/null | wc -l) agents)"
echo " .claude/commands/ ($(ls -1 .claude/commands/*.md 2>/dev/null | wc -l) commands)"
echo " .claude/settings.json"
echo " .claude/skills/skill-rules.json"
echo ""
echo "Next steps:"
echo ""
echo "1. Test skill activation:"
echo ""
# Generate test examples based on installed kits
test_examples_shown=false
for kit in "${KITS_TO_INSTALL[@]}"; do
case "$kit" in
react)
echo " React skill:"
echo " - Edit a .tsx file or ask 'How do I create a React component?'"
echo ""
test_examples_shown=true
;;
mui)
echo " MUI skill:"
echo " - Edit a component file or ask 'How do I style with MUI?'"
echo ""
test_examples_shown=true
;;
nodejs)
echo " Node.js skill:"
echo " - Edit a service file or ask 'How do I create a service layer?'"
echo ""
test_examples_shown=true
;;
express)
echo " Express skill:"
echo " - Edit a route file or ask 'How do I create an API endpoint?'"
echo ""
test_examples_shown=true
;;
prisma)
echo " Prisma skill:"
echo " - Edit schema.prisma or ask 'How do I create a Prisma model?'"
echo ""
test_examples_shown=true
;;
tanstack-query)
echo " TanStack Query skill:"
echo " - Ask 'How do I fetch data with TanStack Query?'"
echo ""
test_examples_shown=true
;;
tanstack-router)
echo " TanStack Router skill:"
echo " - Ask 'How do I create a route with TanStack Router?'"
echo ""
test_examples_shown=true
;;
esac
done
if [ "$test_examples_shown" = false ]; then
echo " Try editing files or asking Claude questions!"
echo " The skill-developer skill is always available."
echo ""
fi
echo ""
echo "2. Review configuration:"
echo " - .claude/settings.json (hook configuration)"
echo " - .claude/skills/skill-rules.json (skill triggers)"
echo ""
echo "Documentation: https://github.com/your-org/claude-code-infrastructure-showcase"
echo ""