-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·272 lines (233 loc) · 8.09 KB
/
install.sh
File metadata and controls
executable file
·272 lines (233 loc) · 8.09 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
#!/bin/bash
# CodeKeeper Installation Script
# This script helps you quickly set up CodeKeeper in your project
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Banner
echo -e "${CYAN}"
echo "╔═══════════════════════════════════════╗"
echo "║ ║"
echo "║ 🛡️ CodeKeeper Installation Script ║"
echo "║ React/Next.js AI Guardrails ║"
echo "║ ║"
echo "╚═══════════════════════════════════════╝"
echo -e "${NC}"
# Function to print colored messages
print_step() {
echo -e "${BLUE}▶${NC} $1"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
# Check if we're in a git repository
check_git_repo() {
if [ -d .git ]; then
print_success "Git repository detected"
return 0
else
print_warning "Not a git repository. Some features may be limited."
return 1
fi
}
# Detect project type
detect_project_type() {
print_step "Detecting project type..."
PROJECT_TYPE="react"
if [ -f "package.json" ]; then
if grep -q "next" package.json 2>/dev/null; then
PROJECT_TYPE="nextjs"
print_success "Next.js project detected"
elif grep -q "react" package.json 2>/dev/null; then
PROJECT_TYPE="react"
print_success "React project detected"
else
print_warning "Node.js project detected. CodeKeeper is optimized for React/Next.js projects."
PROJECT_TYPE="react"
fi
else
print_error "package.json not found. CodeKeeper requires a Node.js project with React or Next.js."
exit 1
fi
}
# Copy validation scripts
copy_validation_scripts() {
print_step "Copying validation scripts..."
# Create scripts directory if it doesn't exist
mkdir -p scripts/validation
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Copy validation scripts
if [ -d "$SCRIPT_DIR/scripts/validation" ]; then
cp -r "$SCRIPT_DIR/scripts/validation/"* scripts/validation/
print_success "Validation scripts copied to scripts/validation/"
else
print_error "Could not find validation scripts. Please ensure you're running from the CodeKeeper directory."
exit 1
fi
# Copy the individual validation scripts (granular approach)
print_success "Individual validation scripts copied (granular approach)"
print_step "Each script handles a specific concern for better debugging"
}
# Setup git hooks
setup_git_hooks() {
if ! check_git_repo; then
print_warning "Skipping git hooks setup (not a git repository)"
return
fi
print_step "Setting up git hooks..."
# Check if lefthook is installed
if command -v lefthook &> /dev/null; then
print_success "Lefthook is already installed"
else
print_step "Installing lefthook..."
if command -v npm &> /dev/null; then
npm install -g lefthook
print_success "Lefthook installed via npm"
elif command -v brew &> /dev/null; then
brew install lefthook
print_success "Lefthook installed via Homebrew"
else
print_warning "Could not install lefthook automatically. Please install it manually:"
echo " npm install -g lefthook"
echo " or"
echo " brew install lefthook"
return
fi
fi
# Copy lefthook configuration
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ -f "$SCRIPT_DIR/lefthook.yml" ]; then
cp "$SCRIPT_DIR/lefthook.yml" .
print_success "Lefthook configuration copied"
# Install git hooks
lefthook install
print_success "Git hooks installed"
else
print_warning "Could not find lefthook.yml configuration"
fi
}
# Add npm scripts
add_npm_scripts() {
if [ ! -f "package.json" ]; then
return
fi
print_step "Adding npm scripts..."
# Check if jq is available for JSON manipulation
if command -v jq &> /dev/null; then
# Add validation scripts to package.json
jq '.scripts."validate:types" = "node scripts/validation/check-as-casts.js"' package.json > tmp.json && mv tmp.json package.json
jq '.scripts."validate:complexity" = "node scripts/validation/check-file-complexity.js"' package.json > tmp.json && mv tmp.json package.json
jq '.scripts."validate:docs" = "node scripts/validation/check-jsdoc.js"' package.json > tmp.json && mv tmp.json package.json
print_success "Added npm scripts for validation"
else
print_warning "jq not found. Please manually add these scripts to package.json:"
echo ' "validate:types": "node scripts/validation/check-as-casts.js"'
echo ' "validate:complexity": "node scripts/validation/check-file-complexity.js"'
echo ' "validate:docs": "node scripts/validation/check-jsdoc.js"'
fi
}
# Create configuration file
create_config() {
print_step "Creating configuration file..."
cat > .codekeeper.json << EOF
{
"projectType": "${PROJECT_TYPE}",
"rules": {
"typeSafety": {
"enabled": true,
"allowedPatterns": ["as const"]
},
"complexity": {
"enabled": true,
"limits": {
"lines": $([ "$PROJECT_TYPE" = "react" ] && echo 300 || echo 500),
"functions": $([ "$PROJECT_TYPE" = "react" ] && echo 10 || echo 15),
"nestingDepth": 10
}
},
"imports": {
"enabled": true,
"preventBarrelFiles": true,
"enforceAliases": true
},
"documentation": {
"enabled": true,
"requireJSDoc": true
}
}
}
EOF
print_success "Configuration file created: .codekeeper.json"
}
# Test installation
test_installation() {
print_step "Testing installation..."
# Test individual validation scripts
local test_passed=true
for script in "check-as-casts.js" "check-file-complexity.js" "check-jsdoc.js"; do
if [ -f "scripts/validation/$script" ]; then
print_success "✓ $script found"
else
print_warning "✗ $script missing"
test_passed=false
fi
done
if [ "$test_passed" = true ]; then
print_success "All validation scripts installed successfully"
else
print_warning "Some validation scripts may be missing"
fi
}
# Main installation flow
main() {
echo -e "${BOLD}Starting CodeKeeper installation...${NC}\n"
# Check current directory
print_step "Installing in: $(pwd)"
echo -n "Continue? (y/n) "
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
exit 0
fi
echo ""
# Run installation steps
detect_project_type
copy_validation_scripts
setup_git_hooks
add_npm_scripts
create_config
test_installation
# Success message
echo ""
echo -e "${GREEN}${BOLD}✅ CodeKeeper installation complete!${NC}"
echo ""
echo -e "${CYAN}Next steps:${NC}"
echo " 1. Review the configuration in .codekeeper.json"
echo " 2. Run validation: ${BOLD}npm run validate${NC}"
echo " 3. Customize rules for your React/Next.js project"
echo ""
echo -e "${CYAN}Quick commands:${NC}"
echo " • npm run validate:types - Check type safety"
echo " • npm run validate:complexity - Check file complexity"
echo " • npm run validate:docs - Check JSDoc documentation"
echo ""
echo -e "${CYAN}Pro tip:${NC} Configure your editor to run validation on save!"
echo ""
echo -e "${YELLOW}Documentation:${NC} https://github.com/thedaviddias/codekeeper"
echo ""
}
# Run main installation
main