codeflow-hook 1.0.0

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.
package/README.md ADDED
@@ -0,0 +1,248 @@
1
+ # Codeflow Hook - AI-Powered Git Hooks
2
+
3
+ An interactive CI/CD simulator and lightweight pre-push code reviewer that uses Google Gemini AI to analyze your code changes before commits and pushes.
4
+
5
+ ## 🚀 Features
6
+
7
+ - **AI Code Review**: Get intelligent code analysis powered by Gemini AI
8
+ - **Automated Git Hooks**: Automatic pre-commit and pre-push checks
9
+ - **CI/CD Simulation**: Simulates full pipeline including tests and security checks
10
+ - **Easy Installation**: Simple CLI setup for any project
11
+ - **Developer-Friendly**: Clear feedback with actionable suggestions
12
+
13
+ ## 📦 Installation
14
+
15
+ ### Global Installation
16
+
17
+ ```bash
18
+ npm install -g codeflow-hook
19
+ ```
20
+
21
+ ### Local Installation (for specific projects)
22
+
23
+ ```bash
24
+ npm install --save-dev codeflow-hook
25
+ ```
26
+
27
+ ## ⚙️ Setup
28
+
29
+ ### 1. Configure AI Provider
30
+
31
+ Choose your AI provider and configure with your API key:
32
+
33
+ **Gemini (default):**
34
+ ```bash
35
+ codeflow-hook config -p gemini -k YOUR_GEMINI_API_KEY
36
+ ```
37
+
38
+ **OpenAI:**
39
+ ```bash
40
+ codeflow-hook config -p openai -k YOUR_OPENAI_API_KEY
41
+ ```
42
+
43
+ **Claude/Anthropic:**
44
+ ```bash
45
+ codeflow-hook config -p claude -k YOUR_CLAUDE_API_KEY
46
+ ```
47
+
48
+ **Custom model/URL:**
49
+ ```bash
50
+ codeflow-hook config -p openai -k YOUR_API_KEY -m gpt-3.5-turbo -u https://your-custom-endpoint.com
51
+ ```
52
+
53
+ ### 2. Install Git Hooks
54
+
55
+ In your project directory:
56
+
57
+ ```bash
58
+ codeflow-hook install
59
+ ```
60
+
61
+ This creates:
62
+ - `pre-commit`: AI analysis of staged changes
63
+ - `pre-push`: Full CI/CD simulation (tests + AI review)
64
+
65
+ ### 3. Check Status
66
+
67
+ ```bash
68
+ codeflow-hook status
69
+ ```
70
+
71
+ ## 🛠️ Commands
72
+
73
+ ### Analyze Specific Changes
74
+
75
+ Manually analyze a git diff:
76
+
77
+ ```bash
78
+ git diff --staged | codeflow-hook analyze-diff
79
+ ```
80
+
81
+ ### Reinstall Hooks
82
+
83
+ ```bash
84
+ codeflow-hook install --hooks-dir .custom-hooks
85
+ ```
86
+
87
+ ### View Help
88
+
89
+ ```bash
90
+ codeflow-hook --help
91
+ ```
92
+
93
+ ## 🔄 How It Works
94
+
95
+ ### Pre-commit Hook
96
+ - Analyzes staged changes only
97
+ - Provides quick feedback on code quality
98
+ - Prevents problematic commits
99
+
100
+ ### Pre-push Hook
101
+ - Runs full test suite
102
+ - Performs comprehensive AI code review
103
+ - Simulates deployment pipeline
104
+ - Blocks pushes with failing checks
105
+
106
+ ### AI Analysis Features
107
+ - Code quality assessment
108
+ - Security vulnerability detection
109
+ - Performance optimization suggestions
110
+ - Best practice recommendations
111
+ - Maintainability evaluation
112
+
113
+ ## 💡 Usage Examples
114
+
115
+ ### Standard Development Workflow
116
+
117
+ ```bash
118
+ # Stage your changes
119
+ git add .
120
+
121
+ # Pre-commit hook automatically runs AI analysis
122
+ git commit -m "feat: add new authentication"
123
+
124
+ # Pre-push hook runs tests and full AI review
125
+ git push origin main
126
+ ```
127
+
128
+ ### Manual Analysis
129
+
130
+ ```bash
131
+ # Analyze uncommitted changes
132
+ git diff | codeflow-hook analyze-diff
133
+
134
+ # Analyze specific files
135
+ git diff path/to/file.js | codeflow-hook analyze-diff
136
+
137
+ # Analyze between commits
138
+ git diff HEAD~1 HEAD | codeflow-hook analyze-diff
139
+ ```
140
+
141
+ ## 🎯 AI Analysis Output
142
+
143
+ The tool provides:
144
+
145
+ - **Rating**: 1-10 quality score with color coding
146
+ - **Summary**: Brief assessment of changes
147
+ - **Issues**: Specific problems with solutions
148
+ - **Recommendations**: Improvement suggestions
149
+
150
+ Example output:
151
+ ```
152
+ ⭐ **Rating:** 9/10
153
+ 📝 **Summary:** Clean implementation with good separation of concerns
154
+
155
+ ⚠️ **Issues:**
156
+ - Consider adding input validation for edge cases
157
+
158
+ 💡 **Recommendations:**
159
+ - Add comprehensive error handling
160
+ - Consider extracting common logic to a utility function
161
+ ```
162
+
163
+ ## 🔧 Configuration
164
+
165
+ Configuration is stored in `~/.codeflow-hook/config.json`:
166
+
167
+ ```json
168
+ {
169
+ "provider": "gemini",
170
+ "apiKey": "your-api-key",
171
+ "apiUrl": "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent",
172
+ "model": "gemini-pro"
173
+ }
174
+ ```
175
+
176
+ ### Supported AI Providers
177
+
178
+ - **Gemini**: `provider: "gemini"` - Default, uses Google AI
179
+ - **OpenAI**: `provider: "openai"` - GPT models
180
+ - **Claude**: `provider: "claude"` - Anthropic models
181
+
182
+ Each provider has optimized prompts and supports custom endpoints.
183
+
184
+ ## 📋 Requirements
185
+
186
+ - Node.js 16+
187
+ - Git repository
188
+ - Gemini API key
189
+
190
+ ## 🔒 Security
191
+
192
+ - API keys stored locally (not in your repo)
193
+ - Have to make an .env file for the credentials
194
+ - No data sent to third parties except Google Gemini
195
+ - Code diffs analyzed locally before sending
196
+
197
+ ## 🐛 Troubleshooting
198
+
199
+ ### Common Issues
200
+
201
+ **"No configuration found"**
202
+ ```bash
203
+ codeflow-hook config -k YOUR_API_KEY
204
+ ```
205
+
206
+ **Hooks not running**
207
+ ```bash
208
+ codeflow-hook install
209
+ # Ensure scripts are executable
210
+ chmod +x .git/hooks/pre-commit .git/hooks/pre-push
211
+ ```
212
+
213
+ **API errors**
214
+ - Verify your API key is valid
215
+ - Check Gemini API service status
216
+ - Ensure you have quota remaining
217
+
218
+ ### Manual Hook Setup
219
+
220
+ If automatic installation fails:
221
+
222
+ 1. Create `.git/hooks/pre-commit`
223
+ 2. Add executable permissions: `chmod +x .git/hooks/pre-commit`
224
+ 3. Call the CLI: `npx codeflow-hook analyze-diff "$(git diff --cached)"`
225
+
226
+ ## 🤝 Contributing
227
+
228
+ 1. Fork the repository
229
+ 2. Create a feature branch
230
+ 3. Make your changes
231
+ 4. Run tests: `npm test`
232
+ 5. Submit a pull request
233
+
234
+ ## 📄 License
235
+
236
+ MIT License - see LICENSE file for details
237
+
238
+ ## 🎉 Acknowledgments
239
+
240
+ Built with ❤️ using:
241
+ - Google Gemini AI
242
+ - Commander.js for CLI
243
+ - Chalk for terminal colors
244
+ - Ora for loading spinners
245
+
246
+ ---
247
+
248
+ **Ready to supercharge your development workflow? Install Codeflow Hook today!**
@@ -0,0 +1,352 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander';
4
+ import chalk from 'chalk';
5
+ import ora from 'ora';
6
+ import axios from 'axios';
7
+ import fs from 'fs';
8
+ import path from 'path';
9
+ import { execSync } from 'child_process';
10
+
11
+ const program = new Command();
12
+
13
+ program
14
+ .name('codeflow-hook')
15
+ .description('Interactive CI/CD simulator and AI-powered code reviewer')
16
+ .version('1.0.0');
17
+
18
+ // Configure AI provider settings
19
+ program
20
+ .command('config')
21
+ .description('Configure AI provider settings')
22
+ .option('-p, --provider <provider>', 'AI provider (gemini, openai, claude)', 'gemini')
23
+ .option('-k, --key <key>', 'API key for the chosen provider')
24
+ .option('-u, --url <url>', 'Custom API URL (optional)')
25
+ .option('-m, --model <model>', 'AI model name (optional - uses provider default)')
26
+ .action((options) => {
27
+ const configDir = path.join(process.env.HOME, '.codeflow-hook');
28
+ if (!fs.existsSync(configDir)) {
29
+ fs.mkdirSync(configDir, { recursive: true });
30
+ }
31
+
32
+ const configPath = path.join(configDir, 'config.json');
33
+ const existingConfig = fs.existsSync(configPath) ? JSON.parse(fs.readFileSync(configPath, 'utf8')) : {};
34
+
35
+ const config = {
36
+ ...existingConfig,
37
+ provider: options.provider || existingConfig.provider || 'gemini',
38
+ apiKey: options.key || existingConfig.apiKey,
39
+ apiUrl: options.url || existingConfig.apiUrl,
40
+ model: options.model || existingConfig.model
41
+ };
42
+
43
+ // Set defaults based on provider
44
+ if (!config.apiUrl) {
45
+ switch (config.provider) {
46
+ case 'openai':
47
+ config.apiUrl = 'https://api.openai.com/v1/chat/completions';
48
+ config.model = config.model || 'gpt-4';
49
+ break;
50
+ case 'claude':
51
+ config.apiUrl = 'https://api.anthropic.com/v1/messages';
52
+ config.model = config.model || 'claude-3-sonnet-20240229';
53
+ break;
54
+ case 'gemini':
55
+ default:
56
+ config.apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent';
57
+ config.model = config.model || 'gemini-pro';
58
+ break;
59
+ }
60
+ }
61
+
62
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
63
+ console.log(chalk.green(`✅ Configuration saved for ${config.provider} provider`));
64
+ });
65
+
66
+ // Install git hooks
67
+ program
68
+ .command('install')
69
+ .description('Install git hooks (pre-commit and pre-push)')
70
+ .option('--hooks-dir <dir>', 'Custom hooks directory', '.git/hooks')
71
+ .action(async (options) => {
72
+ const spinner = ora('Installing git hooks...').start();
73
+
74
+ try {
75
+ const hooksDir = path.resolve(options.hooksDir);
76
+ if (!fs.existsSync(hooksDir)) {
77
+ fs.mkdirSync(hooksDir, { recursive: true });
78
+ }
79
+
80
+ // Create pre-commit hook
81
+ const preCommitHook = `#!/usr/bin/env bash
82
+ # Codeflow pre-commit hook
83
+ # Auto-generated by codeflow-hook CLI
84
+
85
+ set -e
86
+
87
+ echo "🔬 Running Codeflow AI Code Analysis..."
88
+
89
+ # Get staged changes
90
+ STAGED_DIFF=$(git diff --cached --no-color)
91
+
92
+ if [ -z "$STAGED_DIFF" ]; then
93
+ echo "ℹ️ No staged changes to analyze"
94
+ exit 0
95
+ fi
96
+
97
+ # Run AI analysis
98
+ npx codeflow-hook analyze-diff "$STAGED_DIFF"
99
+ `;
100
+
101
+ fs.writeFileSync(path.join(hooksDir, 'pre-commit'), preCommitHook, { mode: 0o755 });
102
+
103
+ // Create pre-push hook (enhanced version)
104
+ const prePushHook = `#!/usr/bin/env bash
105
+ # Codeflow pre-push hook
106
+ # Auto-generated by codeflow-hook CLI
107
+
108
+ set -e
109
+
110
+ echo "🚀 Running Codeflow CI/CD simulation..."
111
+
112
+ # Run tests if available
113
+ if [ -f "package.json" ]; then
114
+ echo "🧪 Running tests..."
115
+ npm test || (echo "❌ Tests failed" && exit 1)
116
+ fi
117
+
118
+ # Get staged changes for AI analysis
119
+ STAGED_DIFF=$(git diff --cached --no-color)
120
+
121
+ if [ -n "$STAGED_DIFF" ]; then
122
+ echo "🔬 Running AI Code Review..."
123
+ npx codeflow-hook analyze-diff "$STAGED_DIFF" || exit 1
124
+ fi
125
+
126
+ echo "✅ All checks passed!"
127
+ exit 0
128
+ `;
129
+
130
+ fs.writeFileSync(path.join(hooksDir, 'pre-push'), prePushHook, { mode: 0o755 });
131
+
132
+ spinner.succeed('Git hooks installed successfully');
133
+ console.log(chalk.blue('📋 Installed hooks:'));
134
+ console.log(chalk.gray(' - pre-commit: AI analysis on staged changes'));
135
+ console.log(chalk.gray(' - pre-push: CI/CD simulation with AI review + tests'));
136
+
137
+ } catch (error) {
138
+ spinner.fail('Failed to install hooks');
139
+ console.error(chalk.red(error.message));
140
+ process.exit(1);
141
+ }
142
+ });
143
+
144
+ // Analyze diff with configured AI provider
145
+ program
146
+ .command('analyze-diff')
147
+ .description('Analyze git diff with configured AI provider')
148
+ .argument('<diff>', 'Git diff content')
149
+ .action(async (diff) => {
150
+ try {
151
+ const configPath = path.join(process.env.HOME, '.codeflow-hook', 'config.json');
152
+
153
+ if (!fs.existsSync(configPath)) {
154
+ console.log(chalk.red('No configuration found. Run: codeflow-hook config -k <api-key>'));
155
+ process.exit(1);
156
+ }
157
+
158
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
159
+
160
+ if (diff.trim() === '') {
161
+ console.log(chalk.gray('ℹ️ No changes to analyze'));
162
+ return;
163
+ }
164
+
165
+ const spinner = ora(`Analyzing code with ${config.provider}...`).start();
166
+ const prompt = generateCodeReviewPrompt(diff);
167
+
168
+ let result;
169
+ try {
170
+ result = await callAIProvider(config, prompt);
171
+ } catch (error) {
172
+ spinner.fail('Analysis failed');
173
+ console.error(chalk.red(`AI API Error: ${error.message}`));
174
+ process.exit(1);
175
+ }
176
+
177
+ spinner.succeed('Analysis complete');
178
+
179
+ // Parse and display results
180
+ displayAnalysisResults(result);
181
+
182
+ } catch (error) {
183
+ console.log(chalk.red(`Configuration error: ${error.message}`));
184
+ process.exit(1);
185
+ }
186
+ });
187
+
188
+ // Show status
189
+ program
190
+ .command('status')
191
+ .description('Show installation and configuration status')
192
+ .action(() => {
193
+ console.log(chalk.blue('🔍 Codeflow Hook Status'));
194
+ console.log();
195
+
196
+ // Check configuration
197
+ const configPath = path.join(process.env.HOME, '.codeflow-hook', 'config.json');
198
+ if (fs.existsSync(configPath)) {
199
+ console.log(chalk.green('✅ Configuration: Found'));
200
+ } else {
201
+ console.log(chalk.red('❌ Configuration: Not found (run: codeflow-hook config)'));
202
+ }
203
+
204
+ // Check git hooks
205
+ const hooksDir = '.git/hooks';
206
+ const preCommitHook = path.join(hooksDir, 'pre-commit');
207
+ const prePushHook = path.join(hooksDir, 'pre-push');
208
+
209
+ if (fs.existsSync(preCommitHook)) {
210
+ console.log(chalk.green('✅ Git Hook (pre-commit): Installed'));
211
+ } else {
212
+ console.log(chalk.red('❌ Git Hook (pre-commit): Not installed'));
213
+ }
214
+
215
+ if (fs.existsSync(prePushHook)) {
216
+ console.log(chalk.green('✅ Git Hook (pre-push): Installed'));
217
+ } else {
218
+ console.log(chalk.red('❌ Git Hook (pre-push): Not installed'));
219
+ }
220
+ });
221
+
222
+ function generateCodeReviewPrompt(diff) {
223
+ return `You are "Codeflow", a world-class AI software engineering assistant acting as a Principal Engineer. Your mission is to perform a rigorous and constructive code review on the provided code changes.
224
+
225
+ **Guidelines:**
226
+ - Focus on code quality, security, performance, and best practices
227
+ - Be constructive and provide actionable suggestions
228
+ - Rate the changes on a scale of 1-10 (where 10 is excellent)
229
+ - If there are critical issues, suggest fixes
230
+
231
+ **Format your response as:**
232
+ **Rating:** [1-10]/10
233
+ **Summary:** [Brief summary]
234
+
235
+ **Issues:** (if any)
236
+ - [Issue description and suggestion]
237
+
238
+ **Recommendations:** (if any)
239
+ - [Recommendation]
240
+
241
+ **Code Changes to Review:**
242
+ \`\`\`
243
+ ${diff}
244
+ \`\`\`
245
+
246
+ Provide your analysis:`;
247
+ }
248
+
249
+ function callAIProvider(config, prompt) {
250
+ switch (config.provider) {
251
+ case 'openai':
252
+ return callOpenAI(config, prompt);
253
+ case 'claude':
254
+ return callClaude(config, prompt);
255
+ case 'gemini':
256
+ default:
257
+ return callGemini(config, prompt);
258
+ }
259
+ }
260
+
261
+ async function callGemini(config, prompt) {
262
+ const payload = {
263
+ contents: [{
264
+ parts: [{
265
+ text: prompt
266
+ }]
267
+ }],
268
+ generationConfig: {
269
+ temperature: 0.2,
270
+ topK: 40,
271
+ topP: 0.95,
272
+ maxOutputTokens: 2048,
273
+ }
274
+ };
275
+
276
+ const response = await axios.post(`${config.apiUrl}?key=${config.apiKey}`, payload, {
277
+ headers: {
278
+ 'Content-Type': 'application/json'
279
+ }
280
+ });
281
+
282
+ return response.data.candidates[0].content.parts[0].text;
283
+ }
284
+
285
+ async function callOpenAI(config, prompt) {
286
+ const payload = {
287
+ model: config.model,
288
+ messages: [{ role: 'user', content: prompt }],
289
+ temperature: 0.2,
290
+ max_tokens: 2048
291
+ };
292
+
293
+ const response = await axios.post(config.apiUrl, payload, {
294
+ headers: {
295
+ 'Content-Type': 'application/json',
296
+ 'Authorization': `Bearer ${config.apiKey}`
297
+ }
298
+ });
299
+
300
+ return response.data.choices[0].message.content;
301
+ }
302
+
303
+ async function callClaude(config, prompt) {
304
+ const payload = {
305
+ model: config.model,
306
+ max_tokens: 2048,
307
+ messages: [{ role: 'user', content: prompt }]
308
+ };
309
+
310
+ const response = await axios.post(config.apiUrl, payload, {
311
+ headers: {
312
+ 'Content-Type': 'application/json',
313
+ 'x-api-key': config.apiKey,
314
+ 'anthropic-version': '2023-06-01'
315
+ }
316
+ });
317
+
318
+ return response.data.content[0].text;
319
+ }
320
+
321
+ function displayAnalysisResults(result) {
322
+ // Parse the AI response and format it nicely
323
+ const lines = result.split('\n');
324
+
325
+ for (const line of lines) {
326
+ if (line.startsWith('**Rating:**')) {
327
+ const rating = line.match(/\*\*Rating:\*\*\s*(\d+)/);
328
+ if (rating) {
329
+ const score = parseInt(rating[1]);
330
+ if (score >= 8) {
331
+ console.log(chalk.green(`⭐ ${line}`));
332
+ } else if (score >= 5) {
333
+ console.log(chalk.yellow(`⚠️ ${line}`));
334
+ } else {
335
+ console.log(chalk.red(`❌ ${line}`));
336
+ }
337
+ }
338
+ } else if (line.includes('**Issues:**') || line.includes('**Recommendations:**')) {
339
+ console.log(chalk.blue(line));
340
+ } else if (line.startsWith('- ')) {
341
+ console.log(chalk.gray(line));
342
+ } else if (line.includes('**Summary:**')) {
343
+ console.log(chalk.cyan(line));
344
+ } else {
345
+ console.log(line);
346
+ }
347
+ }
348
+
349
+ console.log();
350
+ }
351
+
352
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "codeflow-hook",
3
+ "version": "1.0.0",
4
+ "description": "An interactive CI/CD simulator and lightweight pre-push code reviewer using Gemini AI",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "bin": {
8
+ "codeflow-hook": "./bin/codeflow-hook.js"
9
+ },
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "keywords": ["git", "hooks", "ci", "cd", "code-review", "ai", "gemini"],
14
+ "author": "Sharv619",
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "commander": "^11.1.0",
18
+ "axios": "^1.6.0",
19
+ "chalk": "^5.3.0",
20
+ "ora": "^7.0.1"
21
+ },
22
+ "engines": {
23
+ "node": ">=16.0.0"
24
+ }
25
+ }