@haposoft/cafekit 0.3.2 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,476 @@
1
+ # Impact Analysis Scripts
2
+
3
+ Helper scripts để thực hiện advanced impact analysis techniques.
4
+
5
+ ## 📋 Overview
6
+
7
+ Các scripts này implement industry techniques từ `references/industry-techniques.md`:
8
+
9
+ 1. **ast-analyze.js** - AST-based analysis (Technique #3)
10
+ 2. **find-dependencies.sh** - Dependency analysis (Technique #2)
11
+ 3. **calculate-risk.js** - Risk scoring (Technique #6)
12
+ 4. **run-analysis.sh** - Master script chạy tất cả
13
+
14
+ ## 🚀 Quick Start
15
+
16
+ ### Prerequisites
17
+
18
+ ```bash
19
+ # For AST analysis (optional)
20
+ npm install --save-dev @babel/parser @babel/traverse
21
+
22
+ # For all scripts
23
+ chmod +x scripts/*.sh
24
+ ```
25
+
26
+ ### Run Complete Analysis
27
+
28
+ ```bash
29
+ # Analyze uncommitted changes
30
+ ./scripts/run-analysis.sh
31
+
32
+ # Analyze specific files
33
+ ./scripts/run-analysis.sh --files "src/services/authService.ts,src/components/Login.tsx"
34
+
35
+ # Custom output file
36
+ ./scripts/run-analysis.sh --output my-report.md
37
+ ```
38
+
39
+ ## 📚 Individual Scripts
40
+
41
+ ### 1. AST Analysis
42
+
43
+ Phân tích function signatures, type changes, breaking changes.
44
+
45
+ **Usage:**
46
+ ```bash
47
+ # Analyze single file
48
+ node scripts/ast-analyze.js src/services/authService.ts
49
+
50
+ # Compare before/after
51
+ git show HEAD~1:src/services/authService.ts > /tmp/before.ts
52
+ node scripts/ast-analyze.js src/services/authService.ts --compare /tmp/before.ts
53
+ ```
54
+
55
+ **Output:**
56
+ ```
57
+ 📊 AST Analysis Results
58
+
59
+ File: src/services/authService.ts
60
+
61
+ 📈 Summary:
62
+ Functions: 8 (5 exported, 3 async)
63
+ Classes: 2
64
+ Interfaces: 3
65
+ Types: 1
66
+
67
+ 🔧 Functions:
68
+ 📤 async login(email, password) - line 23
69
+ 📤 async logout() - line 45
70
+ 📤 validateUser(email) - line 67
71
+ ...
72
+
73
+ 🔍 Changes Detected
74
+
75
+ ⚠️ Functions Modified (BREAKING):
76
+ ~ login
77
+ Before: email, password
78
+ After: email, password, rememberMe
79
+ Reason: Parameter count changed: 2 → 3
80
+ ```
81
+
82
+ **Features:**
83
+ - Detect function signature changes
84
+ - Detect interface/type changes
85
+ - Identify breaking changes
86
+ - Compare before/after versions
87
+
88
+ ### 2. Dependency Analysis
89
+
90
+ Tìm tất cả files bị ảnh hưởng bởi code changes.
91
+
92
+ **Usage:**
93
+ ```bash
94
+ # Analyze dependencies for a file
95
+ ./scripts/find-dependencies.sh src/services/authService.ts
96
+
97
+ # Set custom source directory
98
+ SRC_DIR=app ./scripts/find-dependencies.sh src/services/authService.ts
99
+ ```
100
+
101
+ **Output:**
102
+ ```
103
+ 🔍 Finding dependencies for: src/services/authService.ts
104
+
105
+ 📦 Direct Imports:
106
+ Files that import this module:
107
+
108
+ - src/screens/Auth/LoginScreen.tsx
109
+ - src/hooks/useAuth.ts
110
+ - src/api/client.ts
111
+ ...
112
+
113
+ 🔧 Function/Class Usage:
114
+ Searching for: login
115
+ - src/screens/Auth/LoginScreen.tsx
116
+ - src/screens/Auth/SignupScreen.tsx
117
+ ...
118
+
119
+ 🌐 API Endpoint Usage:
120
+ Endpoint: /api/auth/login
121
+ - src/screens/Auth/LoginScreen.tsx
122
+ ...
123
+
124
+ ⚛️ Component Usage:
125
+ - src/screens/Home/HomeScreen.tsx
126
+ ...
127
+
128
+ 🧪 Related Tests:
129
+ - src/services/__tests__/authService.test.ts
130
+ ...
131
+
132
+ 📊 Summary:
133
+ Direct imports: 12
134
+ Related tests: 3
135
+
136
+ 💡 Recommendations:
137
+ ⚠️ HIGH IMPACT: This file is imported by many files (12)
138
+ → Test thoroughly before deploying
139
+ → Consider backward compatibility
140
+ ```
141
+
142
+ **Features:**
143
+ - Find direct imports
144
+ - Find function/class usage
145
+ - Find API endpoint consumers
146
+ - Find component usage
147
+ - Find related tests
148
+ - Provide recommendations
149
+
150
+ ### 3. Risk Calculation
151
+
152
+ Đánh giá mức độ risk của code changes.
153
+
154
+ **Usage:**
155
+ ```bash
156
+ # Calculate risk for uncommitted changes
157
+ node scripts/calculate-risk.js
158
+
159
+ # Calculate risk for specific files
160
+ node scripts/calculate-risk.js --files "file1.ts,file2.ts"
161
+
162
+ # JSON output
163
+ node scripts/calculate-risk.js --json
164
+ ```
165
+
166
+ **Output:**
167
+ ```
168
+ 🎯 Risk Assessment Report
169
+
170
+ ════════════════════════════════════════════════════════════
171
+
172
+ 📊 Risk Score: 18/25
173
+ 🎚️ Risk Level: ⚠️⚠️ HIGH
174
+
175
+ 📋 Summary:
176
+ Files changed: 5
177
+ Lines changed: 250 (+180, -70)
178
+ Dependencies: 12 files affected
179
+ Breaking changes: 2
180
+
181
+ 📁 File Classification:
182
+ 🔴 auth: 2 file(s)
183
+ - src/services/authService.ts
184
+ - src/utils/biometric/biometricHelper.ts
185
+ 🟡 api: 1 file(s)
186
+ - src/api/auth.ts
187
+ 🟢 frontend: 2 file(s)
188
+ - src/screens/Auth/LoginScreen.tsx
189
+ - src/components/LoginButton.tsx
190
+
191
+ ⚖️ Risk Breakdown:
192
+ • 2 auth file(s) changed
193
+ Points: 10 (weight: 5)
194
+ • 250 lines changed (large)
195
+ Points: 2 (weight: 2)
196
+ • 12 files depend on changes (some)
197
+ Points: 2 (weight: 2)
198
+ • 2 potential breaking change(s)
199
+ Points: 4 (weight: 2)
200
+
201
+ ⚠️ Potential Breaking Changes:
202
+ 1. src/services/authService.ts
203
+ Type: function_signature
204
+ Function signature may have changed
205
+
206
+ 2. src/api/auth.ts
207
+ Type: api_endpoint
208
+ API endpoint may have changed
209
+
210
+ 💡 Recommendations:
211
+ 🔴 [CRITICAL] Extensive testing required
212
+ Run full test suite including integration and E2E tests
213
+ 🔴 [CRITICAL] Code review required
214
+ Get at least 2 senior developers to review changes
215
+ 🔴 [CRITICAL] Security review
216
+ Review authentication flow and test all auth scenarios
217
+ 🟡 [HIGH] Update all consumers
218
+ Update 12 files that depend on changed code
219
+
220
+ ════════════════════════════════════════════════════════════
221
+ ```
222
+
223
+ **Features:**
224
+ - File type risk scoring
225
+ - Change size risk scoring
226
+ - Dependency risk scoring
227
+ - Breaking change detection
228
+ - Actionable recommendations
229
+
230
+ **Risk Levels:**
231
+ - **CRITICAL** (≥15): Extensive testing, multiple reviews required
232
+ - **HIGH** (≥10): Thorough testing, code review required
233
+ - **MEDIUM** (≥5): Standard testing, review recommended
234
+ - **LOW** (<5): Basic testing sufficient
235
+
236
+ ### 4. Master Script
237
+
238
+ Chạy tất cả analysis techniques và tạo comprehensive report.
239
+
240
+ **Usage:**
241
+ ```bash
242
+ # Run complete analysis
243
+ ./scripts/run-analysis.sh
244
+
245
+ # Analyze specific files
246
+ ./scripts/run-analysis.sh --files "file1.ts,file2.ts"
247
+
248
+ # Custom output
249
+ ./scripts/run-analysis.sh --output my-report.md
250
+ ```
251
+
252
+ **Output:**
253
+ - Comprehensive markdown report
254
+ - Includes all analysis results
255
+ - Actionable recommendations
256
+ - Testing checklist
257
+
258
+ **Report Sections:**
259
+ 1. Change Detection
260
+ 2. Risk Assessment
261
+ 3. Dependency Analysis
262
+ 4. AST Analysis (semantic changes)
263
+ 5. Test Coverage Analysis
264
+ 6. Feature Impact Map
265
+ 7. Recommendations
266
+ 8. Summary
267
+
268
+ ## 🔧 Configuration
269
+
270
+ ### Environment Variables
271
+
272
+ ```bash
273
+ # Source directory (default: src)
274
+ export SRC_DIR=app
275
+
276
+ # Output file (default: impact-analysis-report.md)
277
+ export OUTPUT_FILE=my-report.md
278
+ ```
279
+
280
+ ### Risk Scoring Configuration
281
+
282
+ Edit `calculate-risk.js` to customize risk weights:
283
+
284
+ ```javascript
285
+ const RISK_CONFIG = {
286
+ fileTypes: {
287
+ database: 5, // High risk
288
+ api: 3, // Medium risk
289
+ frontend: 1, // Low risk
290
+ // ... customize
291
+ },
292
+
293
+ changeSize: {
294
+ huge: { threshold: 500, weight: 3 },
295
+ large: { threshold: 200, weight: 2 },
296
+ // ... customize
297
+ },
298
+
299
+ levels: {
300
+ critical: { threshold: 15 },
301
+ high: { threshold: 10 },
302
+ // ... customize
303
+ }
304
+ };
305
+ ```
306
+
307
+ ## 📊 Integration Examples
308
+
309
+ ### Pre-commit Hook
310
+
311
+ ```bash
312
+ #!/bin/bash
313
+ # .git/hooks/pre-commit
314
+
315
+ echo "Running impact analysis..."
316
+ ./scripts/run-analysis.sh --output .git/impact-report.md
317
+
318
+ # Show summary
319
+ echo ""
320
+ echo "Impact Analysis Summary:"
321
+ node scripts/calculate-risk.js
322
+
323
+ # Ask for confirmation if high risk
324
+ RISK_LEVEL=$(node scripts/calculate-risk.js --json | jq -r '.risk.level')
325
+ if [ "$RISK_LEVEL" = "HIGH" ] || [ "$RISK_LEVEL" = "CRITICAL" ]; then
326
+ echo ""
327
+ echo "⚠️ HIGH RISK changes detected!"
328
+ read -p "Continue with commit? (y/n) " -n 1 -r
329
+ echo
330
+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
331
+ exit 1
332
+ fi
333
+ fi
334
+ ```
335
+
336
+ ### CI/CD Pipeline
337
+
338
+ ```yaml
339
+ # .github/workflows/impact-analysis.yml
340
+ name: Impact Analysis
341
+
342
+ on: [pull_request]
343
+
344
+ jobs:
345
+ analyze:
346
+ runs-on: ubuntu-latest
347
+ steps:
348
+ - uses: actions/checkout@v2
349
+ with:
350
+ fetch-depth: 0
351
+
352
+ - name: Setup Node.js
353
+ uses: actions/setup-node@v2
354
+ with:
355
+ node-version: '18'
356
+
357
+ - name: Install dependencies
358
+ run: npm install --save-dev @babel/parser @babel/traverse
359
+
360
+ - name: Run Impact Analysis
361
+ run: |
362
+ chmod +x scripts/*.sh
363
+ ./scripts/run-analysis.sh --output impact-report.md
364
+
365
+ - name: Comment on PR
366
+ uses: actions/github-script@v6
367
+ with:
368
+ script: |
369
+ const fs = require('fs');
370
+ const report = fs.readFileSync('impact-report.md', 'utf8');
371
+ github.rest.issues.createComment({
372
+ issue_number: context.issue.number,
373
+ owner: context.repo.owner,
374
+ repo: context.repo.repo,
375
+ body: report
376
+ });
377
+
378
+ - name: Check Risk Level
379
+ run: |
380
+ RISK_LEVEL=$(node scripts/calculate-risk.js --json | jq -r '.risk.level')
381
+ echo "Risk Level: $RISK_LEVEL"
382
+ if [ "$RISK_LEVEL" = "CRITICAL" ]; then
383
+ echo "::error::CRITICAL risk level detected"
384
+ exit 1
385
+ fi
386
+ ```
387
+
388
+ ### VS Code Task
389
+
390
+ ```json
391
+ // .vscode/tasks.json
392
+ {
393
+ "version": "2.0.0",
394
+ "tasks": [
395
+ {
396
+ "label": "Impact Analysis",
397
+ "type": "shell",
398
+ "command": "./scripts/run-analysis.sh",
399
+ "problemMatcher": [],
400
+ "presentation": {
401
+ "reveal": "always",
402
+ "panel": "new"
403
+ }
404
+ },
405
+ {
406
+ "label": "Quick Risk Check",
407
+ "type": "shell",
408
+ "command": "node scripts/calculate-risk.js",
409
+ "problemMatcher": []
410
+ }
411
+ ]
412
+ }
413
+ ```
414
+
415
+ ## 🐛 Troubleshooting
416
+
417
+ ### AST Analysis Fails
418
+
419
+ **Problem**: `Cannot find module '@babel/parser'`
420
+
421
+ **Solution**:
422
+ ```bash
423
+ npm install --save-dev @babel/parser @babel/traverse
424
+ ```
425
+
426
+ ### Dependency Analysis Returns Empty
427
+
428
+ **Problem**: No dependencies found
429
+
430
+ **Solutions**:
431
+ 1. Check source directory: `SRC_DIR=app ./scripts/find-dependencies.sh file.ts`
432
+ 2. Ensure file exists and has correct path
433
+ 3. Check if files use ES6 imports (not CommonJS require)
434
+
435
+ ### Risk Calculation Shows Wrong Level
436
+
437
+ **Problem**: Risk level doesn't match expectations
438
+
439
+ **Solution**: Customize risk weights in `calculate-risk.js`:
440
+ ```javascript
441
+ const RISK_CONFIG = {
442
+ fileTypes: {
443
+ // Increase weight for critical files
444
+ auth: 10, // was 5
445
+ payment: 10 // was 5
446
+ }
447
+ };
448
+ ```
449
+
450
+ ### Git Commands Fail
451
+
452
+ **Problem**: `fatal: not a git repository`
453
+
454
+ **Solution**: Ensure you're in a git repository or use `--files` flag:
455
+ ```bash
456
+ ./scripts/run-analysis.sh --files "file1.ts,file2.ts"
457
+ ```
458
+
459
+ ## 📚 Further Reading
460
+
461
+ - `references/industry-techniques.md` - Industry techniques overview
462
+ - `references/practical-techniques-guide.md` - Detailed usage guide
463
+ - `references/project-detection.md` - Project-specific customization
464
+
465
+ ## 🤝 Contributing
466
+
467
+ To add new analysis techniques:
468
+
469
+ 1. Create new script in `scripts/`
470
+ 2. Add to `run-analysis.sh`
471
+ 3. Update this README
472
+ 4. Add tests if applicable
473
+
474
+ ## 📝 License
475
+
476
+ Part of CafeKit Impact Analysis skill.