File size: 2,605 Bytes
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { readFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";
import { execSync } from "node:child_process";
import { generateReport, formatReport } from "../../checker/engine.js";

export async function checkCode(target: string, root: string): Promise<void> {
  const absRoot = resolve(root);
  console.log(`Checking code: ${target}\n`);

  let fileAnalyses: Array<{ path: string; content: string; language?: string }> = [];

  // Check if target is a file path
  const filePath = resolve(absRoot, target);
  if (existsSync(filePath)) {
    const content = readFileSync(filePath, "utf-8");
    const lang = detectLanguage(filePath);
    fileAnalyses.push({ path: target, content, language: lang });
  } else if (target.includes("..") || target.includes("HEAD")) {
    // Git diff range
    try {
      const diffFiles = execSync(`git diff --name-only ${target}`, {
        cwd: absRoot,
        encoding: "utf-8",
      })
        .trim()
        .split("\n")
        .filter(Boolean);

      for (const file of diffFiles) {
        const fPath = resolve(absRoot, file);
        if (existsSync(fPath)) {
          const content = readFileSync(fPath, "utf-8");
          fileAnalyses.push({ path: file, content, language: detectLanguage(file) });
        }
      }
    } catch {
      console.log("  ⚠ Could not read git diff. Make sure you're in a git repo with commits.");
      console.log("  Usage: agentlens check <file> or agentlens check <git-diff-range>");
      return;
    }
  } else {
    console.log(`  ⚠ File not found: ${target}`);
    console.log("  Usage: agentlens check <file> or agentlens check HEAD~1..HEAD");
    return;
  }

  if (fileAnalyses.length === 0) {
    console.log("  No files to check.");
    return;
  }

  const report = generateReport(fileAnalyses);
  console.log(formatReport(report));

  // Exit with non-zero if critical issues found
  if (report.summary.critical > 0) {
    console.log(`  ❌ ${report.summary.critical} critical issue(s) found.\n`);
  } else if (report.summary.warning > 0) {
    console.log(`  ⚠ ${report.summary.warning} warning(s) found.\n`);
  } else {
    console.log("  ✅ All clear.\n");
  }
}

function detectLanguage(filePath: string): string | undefined {
  const ext = filePath.split(".").pop()?.toLowerCase();
  const langMap: Record<string, string> = {
    ts: "typescript", tsx: "typescript",
    js: "javascript", jsx: "javascript", mjs: "javascript",
    py: "python",
    go: "go",
    rs: "rust",
    java: "java",
    rb: "ruby",
    php: "php",
  };
  return ext ? langMap[ext] : undefined;
}