-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-interactive.sh
More file actions
executable file
Β·341 lines (287 loc) Β· 11.6 KB
/
install-interactive.sh
File metadata and controls
executable file
Β·341 lines (287 loc) Β· 11.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
#!/usr/bin/env bash
# install-interactive.sh β Interactive module selector for claude-code-hooks
# Allows users to choose which scripts to install (core + optional + tools)
set -e
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# === Helper Functions ===
print_header() {
echo ""
echo -e "${BLUE}ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "${BLUE}β${NC} Claude Code Hooks β Interactive Installer ${BLUE}β${NC}"
echo -e "${BLUE}ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo ""
}
print_section() {
echo -e "${YELLOW}$1${NC}"
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
}
print_info() {
echo -e "${GREEN}β${NC} $1"
}
print_warning() {
echo -e "${YELLOW}β ${NC} $1"
}
prompt_yes_no() {
local prompt="$1"
local default="${2:-yes}"
if [[ "$default" == "yes" ]]; then
read -p "$prompt [Y/n]: " -n 1 -r
else
read -p "$prompt [y/N]: " -n 1 -r
fi
echo
if [[ $REPLY =~ ^[Yy]$ ]] || ([[ -z "$REPLY" ]] && [[ "$default" == "yes" ]]); then
return 0
else
return 1
fi
}
# === Main Flow ===
print_header
# Step 1: Detect environment
print_section "Step 1: Detecting Environment"
REPO_PATH="${1:-.}"
if [ ! -d "$REPO_PATH/scripts" ]; then
echo -e "${RED}Error: Not in claude-code-hooks directory${NC}"
echo "Usage: $0 [path/to/claude-code-hooks]"
exit 1
fi
OS_TYPE=$(uname -s)
case "$OS_TYPE" in
Darwin) OS="macOS" ;;
Linux) OS="Linux" ;;
MINGW*|MSYS*|CYGWIN*) OS="Windows Git Bash" ;;
*) OS="Unknown" ;;
esac
print_info "Detected OS: $OS"
print_info "Repository path: $REPO_PATH"
# Step 2: Present module selection
print_section "Step 2: Choose Modules to Install"
echo ""
echo "Select which modules you want to install:"
echo ""
# Core modules (always selected)
echo -e "${GREEN}[REQUIRED]${NC} Core Hooks (5 scripts) β Essential security & notifications"
echo " β’ cc-stop-hook.sh β Task completion notifications"
echo " β’ cc-safety-gate.sh β Bash command security gate (23+ patterns)"
echo " β’ wait-notify.sh β Permission request reminders"
echo " β’ cancel-wait.sh β Cancel notification when you respond"
echo " β’ guard-large-files.sh β Prevent reading large/auto-gen files"
INSTALL_CORE=true
print_info "Will install core hooks"
echo ""
echo -e "${YELLOW}[OPTIONAL]${NC} Extended Hooks (5 scripts) β Advanced security"
if prompt_yes_no "Install extended security hooks?"; then
echo " β Configuration protection (config-change-guard.sh)"
echo " β MCP tool safety (mcp-guard.sh)"
echo " β Prompt injection detection (injection-scan.sh)"
echo " β Project file protection (project-context-guard.sh)"
echo " β Cost summary display (openrouter-cost-summary.sh)"
INSTALL_EXTENDED=true
else
print_warning "Skipping extended hooks"
INSTALL_EXTENDED=false
fi
echo ""
echo -e "${YELLOW}[OPTIONAL]${NC} Standalone Tools (5 scripts) β Manual/periodic tasks"
if prompt_yes_no "Install standalone tools?"; then
echo " β Process cleanup (reap-orphans.sh)"
echo " β Task dispatch (dispatch-claude.sh)"
echo " β Status check (check-claude-status.sh)"
echo " β Skill indexing (generate-skill-index.sh)"
echo " β Notification library (send-notification.sh)"
INSTALL_TOOLS=true
else
print_warning "Skipping standalone tools"
INSTALL_TOOLS=false
fi
echo ""
echo -e "${YELLOW}[OPTIONAL]${NC} OpenRouter Statusline Integration"
if prompt_yes_no "Setup OpenRouter credit monitoring in statusline?"; then
INSTALL_STATUSLINE=true
echo " β Will configure claude-hud plugin with OpenRouter display"
else
print_warning "Skipping statusline setup"
INSTALL_STATUSLINE=false
fi
# Step 3: Summary
print_section "Step 3: Installation Summary"
echo ""
echo "Modules to install:"
echo " β Core hooks (required) 5 scripts"
[ "$INSTALL_EXTENDED" = true ] && echo " β Extended hooks (optional) 5 scripts" || echo " β’ Extended hooks (skipped)"
[ "$INSTALL_TOOLS" = true ] && echo " β Standalone tools (optional) 5 scripts" || echo " β’ Standalone tools (skipped)"
[ "$INSTALL_STATUSLINE" = true ] && echo " β OpenRouter statusline (optional)" || echo " β’ OpenRouter statusline (skipped)"
echo ""
echo "Installation destination:"
echo " ~/.claude/scripts/claude-hooks/"
echo " ~/.cchooks/ (config & logs)"
echo ""
if ! prompt_yes_no "Proceed with installation?"; then
echo -e "${YELLOW}Installation cancelled.${NC}"
exit 0
fi
# Step 4: Run actual installation
print_section "Step 4: Installing"
INSTALL_DIR="${HOME}/.claude/scripts/claude-hooks"
mkdir -p "${INSTALL_DIR}"
# Copy core scripts
print_info "Installing core hooks..."
for script in cc-stop-hook.sh cc-safety-gate.sh wait-notify.sh cancel-wait.sh guard-large-files.sh; do
cp "$REPO_PATH/scripts/$script" "${INSTALL_DIR}/"
chmod +x "${INSTALL_DIR}/$script"
done
# Copy libraries (always needed)
print_info "Installing libraries..."
for lib in common.sh platform-shim.sh send-notification.sh; do
cp "$REPO_PATH/scripts/$lib" "${INSTALL_DIR}/"
chmod +x "${INSTALL_DIR}/$lib"
done
# Copy extended hooks if selected
if [ "$INSTALL_EXTENDED" = true ]; then
print_info "Installing extended hooks..."
for script in config-change-guard.sh mcp-guard.sh injection-scan.sh project-context-guard.sh openrouter-cost-summary.sh; do
cp "$REPO_PATH/scripts/$script" "${INSTALL_DIR}/"
chmod +x "${INSTALL_DIR}/$script"
done
fi
# Copy tools if selected
if [ "$INSTALL_TOOLS" = true ]; then
print_info "Installing standalone tools..."
for script in reap-orphans.sh dispatch-claude.sh check-claude-status.sh generate-skill-index.sh setup-statusline.sh; do
cp "$REPO_PATH/scripts/$script" "${INSTALL_DIR}/"
chmod +x "${INSTALL_DIR}/$script"
done
fi
# Copy tools
print_info "Installing utilities..."
cp "$REPO_PATH/tools/merge-hooks.js" "${INSTALL_DIR}/"
cp "$REPO_PATH/tools/select-modules.js" "${INSTALL_DIR}/"
# Setup statusline if requested
if [ "$INSTALL_STATUSLINE" = true ]; then
print_info "Setting up OpenRouter statusline..."
mkdir -p "${INSTALL_DIR}/statusline"
cp "$REPO_PATH/tools/statusline/run-hud.sh" "${INSTALL_DIR}/statusline/"
cp "$REPO_PATH/tools/statusline/openrouter-statusline.js" "${INSTALL_DIR}/statusline/"
chmod +x "${INSTALL_DIR}/statusline"/*.sh
fi
# Step 5: Merge hooks into settings.json
print_section "Step 5: Configuring Settings"
SETTINGS="${HOME}/.claude/settings.json"
# Generate hooks patch dynamically
PATCH_FILE="${TMPDIR:-${TEMP:-/tmp}}/hooks-patch.json"
mkdir -p "$(dirname "$PATCH_FILE")"
# Windows detection
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*) CMD_PREFIX="bash " ;;
*) CMD_PREFIX="" ;;
esac
# Build hooks JSON
node -e "
const fs = require('fs');
const path = require('path');
const dir = '${INSTALL_DIR}'.replace(/\\\\/g, '/');
const prefix = '${CMD_PREFIX}';
const cmd = (script) => prefix + path.join('${INSTALL_DIR}', script).replace(/\\\\/g, '/');
const hooks = {
hooks: {
Stop: [{matcher: '*', hooks: [{type: 'command', command: cmd('cc-stop-hook.sh'), timeout: 15}]}],
PreToolUse: [
{matcher: 'Bash', hooks: [{type: 'command', command: cmd('cc-safety-gate.sh'), timeout: 5}]},
{matcher: 'Read|Edit|Write', hooks: [{type: 'command', command: cmd('guard-large-files.sh'), timeout: 5}]}
],
PermissionRequest: [{matcher: '*', hooks: [{type: 'command', command: cmd('wait-notify.sh'), timeout: 5}]}],
Notification: [{matcher: '*', hooks: [{type: 'command', command: cmd('wait-notify.sh'), timeout: 5}]}],
PostToolUse: [{matcher: '*', hooks: [{type: 'command', command: cmd('cancel-wait.sh'), timeout: 3}]}],
UserPromptSubmit: [{matcher: '*', hooks: [{type: 'command', command: cmd('cancel-wait.sh'), timeout: 3}]}]
}
};
fs.writeFileSync('${PATCH_FILE}', JSON.stringify(hooks, null, 2));
console.log('OK');
"
# Merge hooks
print_info "Merging hooks into settings.json..."
node "${INSTALL_DIR}/merge-hooks.js" "${SETTINGS}" "${PATCH_FILE}" "${SETTINGS}"
rm -f "${PATCH_FILE}"
# Step 6: Create config files
print_section "Step 6: Configuration Files"
mkdir -p "${HOME}/.cchooks/logs"
# Create notify.conf if missing
if [ ! -f "${INSTALL_DIR}/notify.conf" ]; then
print_info "Creating notify.conf template..."
cat > "${INSTALL_DIR}/notify.conf" << 'CONFEOF'
# Claude Code Hooks Notification Configuration
# Copy appropriate credentials from ~/.cchooks/secrets.env
CC_NOTIFY_BACKEND=auto
CC_NOTIFY_TARGET=""
CC_WAIT_NOTIFY_SECONDS=30
CC_NOTIFY_CHANNEL="feishu"
# Add backend URLs or credentials below (or use ~/.cchooks/secrets.env)
# Example:
# NOTIFY_FEISHU_URL=https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx
CONFEOF
chmod 600 "${INSTALL_DIR}/notify.conf"
fi
# Step 7: Verify installation
print_section "Step 7: Verification"
ERRORS=0
for script in cc-stop-hook.sh cc-safety-gate.sh wait-notify.sh cancel-wait.sh guard-large-files.sh common.sh platform-shim.sh; do
if [ -x "${INSTALL_DIR}/$script" ]; then
print_info "$script β"
else
echo -e "${RED}β${NC} $script - NOT FOUND"
ERRORS=$((ERRORS + 1))
fi
done
if [ -f "${SETTINGS}" ]; then
if python3 -c "import json; json.load(open('${SETTINGS}'))" 2>/dev/null; then
print_info "settings.json is valid β"
else
echo -e "${RED}β${NC} settings.json has JSON errors"
ERRORS=$((ERRORS + 1))
fi
fi
# Step 8: Setup optional components
if [ "$INSTALL_EXTENDED" = true ]; then
read -p "Would you like to enable extended hooks now? [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_info "Extended hooks are installed but DISABLED by default."
print_info "To enable, add them to settings.json hooks manually."
print_info "See documentation for examples."
fi
fi
if [ "$INSTALL_TOOLS" = true ] && grep -q "reap-orphans.sh" "${INSTALL_DIR}/reap-orphans.sh" 2>/dev/null; then
read -p "Would you like to setup automatic reap-orphans scheduling? [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_info "To setup automatic cleanup scheduling on macOS, run:"
echo " launchctl load ~/Library/LaunchAgents/com.cchooks.reaper.plist"
echo ""
print_info "For Linux, add to crontab:"
echo " 0 2 * * * ~/.claude/scripts/claude-hooks/reap-orphans.sh"
fi
fi
# Final summary
print_section "Installation Complete! π"
if [ $ERRORS -eq 0 ]; then
echo -e "${GREEN}β All modules installed successfully!${NC}"
else
echo -e "${YELLOW}β Some issues detected. Please review above.${NC}"
fi
echo ""
echo "Next steps:"
echo " 1. Restart Claude Code to load new settings"
echo " 2. Configure notify.conf with your notification backend"
echo " 3. See README.md or CLAUDE-SCRIPTS-REFERENCE.md for full documentation"
echo ""
echo "Quick commands:"
[ "$INSTALL_TOOLS" = true ] && echo " β’ Check task status: check-claude-status.sh"
[ "$INSTALL_TOOLS" = true ] && echo " β’ Dispatch task: dispatch-claude.sh 'command'"
[ "$INSTALL_TOOLS" = true ] && echo " β’ Clean processes: reap-orphans.sh"
echo ""