ai-commit-reviewer 1.0.1

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,101 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const config = require("../config");
4
+
5
+ const DEFAULT_PATTERNS = {
6
+ version: 2,
7
+ total_commits_reviewed: 0,
8
+ recurring_issues: [],
9
+ team_blind_spots: [],
10
+ security_patterns_found: [],
11
+ crash_patterns_found: [],
12
+ last_reviewed: null,
13
+ };
14
+
15
+ const DEFAULT_CONTEXT = {
16
+ components: [],
17
+ custom_hooks: [],
18
+ utilities: [],
19
+ last_updated: null,
20
+ };
21
+
22
+ function bootstrap() {
23
+ if (!fs.existsSync(config.reviewerDir)) fs.mkdirSync(config.reviewerDir, { recursive: true });
24
+ if (!fs.existsSync(config.patternsFile)) writeJSON(config.patternsFile, DEFAULT_PATTERNS);
25
+ if (!fs.existsSync(config.contextFile)) writeJSON(config.contextFile, DEFAULT_CONTEXT);
26
+ }
27
+
28
+ function readJSON(filePath, fallback = {}) {
29
+ try { return JSON.parse(fs.readFileSync(filePath, "utf8")); }
30
+ catch { return fallback; }
31
+ }
32
+
33
+ function writeJSON(filePath, data) {
34
+ try { fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8"); }
35
+ catch {}
36
+ }
37
+
38
+ function appendLine(filePath, obj) {
39
+ try { fs.appendFileSync(filePath, JSON.stringify(obj) + "\n", "utf8"); }
40
+ catch {}
41
+ }
42
+
43
+ function loadPatterns() {
44
+ return readJSON(config.patternsFile, DEFAULT_PATTERNS);
45
+ }
46
+
47
+ function updateMemory(metadata, stagedFiles) {
48
+ const patterns = loadPatterns();
49
+ patterns.total_commits_reviewed = (patterns.total_commits_reviewed || 0) + 1;
50
+ patterns.last_reviewed = new Date().toISOString();
51
+
52
+ if (Array.isArray(metadata.new_patterns_found)) {
53
+ const merged = [...new Set([
54
+ ...(patterns.recurring_issues || []),
55
+ ...metadata.new_patterns_found.filter(Boolean),
56
+ ])];
57
+ patterns.recurring_issues = merged.slice(-config.maxRecurringIssues);
58
+ patterns.team_blind_spots = merged.slice(-config.maxBlindSpots);
59
+ }
60
+
61
+ if (Array.isArray(metadata.categories_flagged)) {
62
+ if (metadata.categories_flagged.includes("SECURITY")) {
63
+ patterns.security_patterns_found = [
64
+ ...(patterns.security_patterns_found || []),
65
+ { date: new Date().toISOString(), issue: metadata.top_issue },
66
+ ].slice(-20);
67
+ }
68
+ if (metadata.categories_flagged.some(c => ["CRASH","ANR","NON-FATAL"].includes(c))) {
69
+ patterns.crash_patterns_found = [
70
+ ...(patterns.crash_patterns_found || []),
71
+ { date: new Date().toISOString(), issue: metadata.top_issue },
72
+ ].slice(-20);
73
+ }
74
+ }
75
+
76
+ writeJSON(config.patternsFile, patterns);
77
+
78
+ const ctx = readJSON(config.contextFile, DEFAULT_CONTEXT);
79
+ ctx.last_updated = new Date().toISOString();
80
+ writeJSON(config.contextFile, ctx);
81
+
82
+ appendLine(config.logFile, {
83
+ timestamp: new Date().toISOString(),
84
+ files: stagedFiles,
85
+ had_blockers: metadata.has_blockers || false,
86
+ categories: metadata.categories_flagged || [],
87
+ top_issue: metadata.top_issue || "",
88
+ commit_count: patterns.total_commits_reviewed,
89
+ });
90
+
91
+ return patterns;
92
+ }
93
+
94
+ function readLog() {
95
+ try {
96
+ return fs.readFileSync(config.logFile, "utf8")
97
+ .split("\n").filter(Boolean).map(l => JSON.parse(l));
98
+ } catch { return []; }
99
+ }
100
+
101
+ module.exports = { bootstrap, loadPatterns, updateMemory, readLog, readJSON, writeJSON };
@@ -0,0 +1,85 @@
1
+ // ── output/colors.js ──────────────────────────────────────
2
+
3
+ const C = {
4
+ red: "\x1b[31m",
5
+ yellow: "\x1b[33m",
6
+ blue: "\x1b[34m",
7
+ green: "\x1b[32m",
8
+ cyan: "\x1b[36m",
9
+ magenta: "\x1b[35m",
10
+ white: "\x1b[37m",
11
+ bold: "\x1b[1m",
12
+ dim: "\x1b[2m",
13
+ reset: "\x1b[0m",
14
+ };
15
+
16
+ function colorLine(line) {
17
+ if (line.includes("🔴 BLOCK")) return `${C.red}${C.bold}${line}${C.reset}`;
18
+ if (line.includes("🟡 WARN")) return `${C.yellow}${C.bold}${line}${C.reset}`;
19
+ if (line.includes("🔵 SUGGEST")) return `${C.blue}${line}${C.reset}`;
20
+ if (line.includes("⚪ STYLE")) return `${C.dim}${line}${C.reset}`;
21
+ if (/^(Problem|Risk|Fix):/.test(line)) return `${C.bold}${line}${C.reset}`;
22
+ if (line.startsWith("```")) return `${C.dim}${line}${C.reset}`;
23
+ if (line.startsWith("//")) return `${C.dim}${line}${C.reset}`;
24
+ if (line.startsWith("PASS ")) return `\n${C.cyan}${C.bold}${line}${C.reset}`;
25
+ return line;
26
+ }
27
+
28
+ function printDivider() {
29
+ process.stdout.write(
30
+ `${C.bold}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${C.reset}\n`
31
+ );
32
+ }
33
+
34
+ function printHeader(stagedCount, commitsReviewed, blindSpots) {
35
+ process.stdout.write(`\n${C.bold}${C.cyan}`);
36
+ process.stdout.write(` ╔══════════════════════════════════════════╗\n`);
37
+ process.stdout.write(` ║ AI Senior Dev Reviewer ║\n`);
38
+ process.stdout.write(` ╚══════════════════════════════════════════╝${C.reset}\n\n`);
39
+ process.stdout.write(` ${C.bold}Files staged:${C.reset} ${stagedCount}\n`);
40
+ process.stdout.write(` ${C.bold}Reviews done:${C.reset} ${commitsReviewed}\n`);
41
+
42
+ if (blindSpots?.length) {
43
+ process.stdout.write(
44
+ ` ${C.bold}Known blind spots:${C.reset} ${C.yellow}${blindSpots.slice(0, 2).join(" · ")}${C.reset}\n`
45
+ );
46
+ }
47
+ process.stdout.write("\n");
48
+ }
49
+
50
+ function printReview(review) {
51
+ const clean = review
52
+ .replace(/REVIEW_METADATA_START[\s\S]*?REVIEW_METADATA_END/, "")
53
+ .trim();
54
+
55
+ for (const line of clean.split("\n")) {
56
+ process.stdout.write(colorLine(line) + "\n");
57
+ }
58
+ }
59
+
60
+ function printVerdict(hasBlockers, topIssue, updatedPatterns) {
61
+ process.stdout.write("\n");
62
+ if (hasBlockers) {
63
+ process.stdout.write(`${C.red}${C.bold} ✗ Commit BLOCKED${C.reset}\n`);
64
+ process.stdout.write(`${C.red} Fix all 🔴 BLOCK issues above, then recommit.${C.reset}\n`);
65
+ if (topIssue) {
66
+ process.stdout.write(`${C.red} Most critical: ${topIssue}${C.reset}\n`);
67
+ }
68
+ process.stdout.write(
69
+ `${C.dim} To skip (use sparingly): git commit --no-verify${C.reset}\n`
70
+ );
71
+ } else {
72
+ process.stdout.write(`${C.green}${C.bold} ✓ Commit allowed${C.reset}\n`);
73
+ if (topIssue) {
74
+ process.stdout.write(`${C.yellow} Suggestion: ${topIssue}${C.reset}\n`);
75
+ }
76
+ }
77
+
78
+ const count = updatedPatterns.total_commits_reviewed || 0;
79
+ const learned = updatedPatterns.recurring_issues?.length || 0;
80
+ process.stdout.write(
81
+ `\n${C.dim} Reviews: ${count} | Patterns learned: ${learned} | run \`npm run dashboard\` to see history${C.reset}\n\n`
82
+ );
83
+ }
84
+
85
+ module.exports = { C, colorLine, printDivider, printHeader, printReview, printVerdict };