-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathdetector.ts
More file actions
153 lines (130 loc) · 4.47 KB
/
Copy pathdetector.ts
File metadata and controls
153 lines (130 loc) · 4.47 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
import { existsSync, readFileSync } from "node:fs";
import { basename, join } from "node:path";
import { logDebug } from "../ui/logger.ts";
interface DetectedProject {
name: string;
language: string;
framework: string;
testCmd: string;
lintCmd: string;
buildCmd: string;
}
/**
* Detect project settings from the codebase
*/
export function detectProject(workDir = process.cwd()): DetectedProject {
const result: DetectedProject = {
name: basename(workDir),
language: "",
framework: "",
testCmd: "",
lintCmd: "",
buildCmd: "",
};
// Check for package.json (Node.js/JavaScript/TypeScript)
const packageJsonPath = join(workDir, "package.json");
if (existsSync(packageJsonPath)) {
detectNodeProject(packageJsonPath, result);
return result;
}
// Check for Python projects
const pyprojectPath = join(workDir, "pyproject.toml");
const requirementsPath = join(workDir, "requirements.txt");
const setupPyPath = join(workDir, "setup.py");
if (existsSync(pyprojectPath) || existsSync(requirementsPath) || existsSync(setupPyPath)) {
detectPythonProject(workDir, result);
return result;
}
// Check for Go projects
const goModPath = join(workDir, "go.mod");
if (existsSync(goModPath)) {
detectGoProject(result);
return result;
}
// Check for Rust projects
const cargoPath = join(workDir, "Cargo.toml");
if (existsSync(cargoPath)) {
detectRustProject(result);
return result;
}
return result;
}
function detectNodeProject(packageJsonPath: string, result: DetectedProject): void {
try {
const content = readFileSync(packageJsonPath, "utf-8");
const pkg = JSON.parse(content);
// Get name from package.json
if (pkg.name) {
result.name = pkg.name;
}
// Detect TypeScript
const tsconfigPath = join(packageJsonPath, "..", "tsconfig.json");
result.language = existsSync(tsconfigPath) ? "TypeScript" : "JavaScript";
// Get all dependencies
const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
const depNames = Object.keys(deps);
// Detect frameworks (order matters - meta-frameworks first)
const frameworks: string[] = [];
if (depNames.includes("next")) frameworks.push("Next.js");
if (depNames.includes("nuxt")) frameworks.push("Nuxt");
if (depNames.includes("@remix-run/react")) frameworks.push("Remix");
if (depNames.includes("svelte")) frameworks.push("Svelte");
if (depNames.some((d) => d.startsWith("@nestjs/"))) frameworks.push("NestJS");
if (depNames.includes("hono")) frameworks.push("Hono");
if (depNames.includes("fastify")) frameworks.push("Fastify");
if (depNames.includes("express")) frameworks.push("Express");
// Only add React/Vue if no meta-framework detected
if (frameworks.length === 0) {
if (depNames.includes("react")) frameworks.push("React");
if (depNames.includes("vue")) frameworks.push("Vue");
}
result.framework = frameworks.join(", ");
// Detect commands from scripts
const scripts = pkg.scripts || {};
const hasBunLock = existsSync(join(packageJsonPath, "..", "bun.lockb"));
if (scripts.test) {
result.testCmd = hasBunLock ? "bun test" : "npm test";
}
if (scripts.lint) {
result.lintCmd = "npm run lint";
}
if (scripts.build) {
result.buildCmd = "npm run build";
}
} catch (error) {
// BUG FIX: Log parsing errors instead of silently ignoring
logDebug(`Failed to parse package.json: ${error}`);
}
}
function detectPythonProject(workDir: string, result: DetectedProject): void {
result.language = "Python";
// Read dependencies to detect frameworks
let deps = "";
const pyprojectPath = join(workDir, "pyproject.toml");
const requirementsPath = join(workDir, "requirements.txt");
if (existsSync(pyprojectPath)) {
deps += readFileSync(pyprojectPath, "utf-8");
}
if (existsSync(requirementsPath)) {
deps += readFileSync(requirementsPath, "utf-8");
}
const depsLower = deps.toLowerCase();
const frameworks: string[] = [];
if (depsLower.includes("fastapi")) frameworks.push("FastAPI");
if (depsLower.includes("django")) frameworks.push("Django");
if (depsLower.includes("flask")) frameworks.push("Flask");
result.framework = frameworks.join(", ");
result.testCmd = "pytest";
result.lintCmd = "ruff check .";
}
function detectGoProject(result: DetectedProject): void {
result.language = "Go";
result.testCmd = "go test ./...";
result.lintCmd = "golangci-lint run";
}
function detectRustProject(result: DetectedProject): void {
result.language = "Rust";
result.testCmd = "cargo test";
result.lintCmd = "cargo clippy";
result.buildCmd = "cargo build";
}