Enhance RouteDetector to exclude specific directories from route detection

- Introduced a mechanism to exclude certain directories (e.g., node_modules, .venv) from route detection in the RouteDetector class.
- Added a new method, _should_include_file, to filter files based on the exclusion criteria.
- Updated route detection methods to utilize the new filtering logic for various frameworks (FastAPI, Flask, Django, Express, Go, Rust).
This commit is contained in:
AndyMik90
2025-12-16 15:19:28 +01:00
parent f00aa33cda
commit 08dc24cc99
+20 -13
View File
@@ -18,9 +18,16 @@ from .base import BaseAnalyzer
class RouteDetector(BaseAnalyzer): class RouteDetector(BaseAnalyzer):
"""Detects API routes across multiple web frameworks.""" """Detects API routes across multiple web frameworks."""
# Directories to exclude from route detection
EXCLUDED_DIRS = {'node_modules', '.venv', 'venv', '__pycache__', '.git'}
def __init__(self, path: Path): def __init__(self, path: Path):
super().__init__(path) super().__init__(path)
def _should_include_file(self, file_path: Path) -> bool:
"""Check if file should be included (not in excluded directories)."""
return not any(part in self.EXCLUDED_DIRS for part in file_path.parts)
def detect_all_routes(self) -> list[dict]: def detect_all_routes(self) -> list[dict]:
"""Detect all API routes across different frameworks.""" """Detect all API routes across different frameworks."""
routes = [] routes = []
@@ -51,7 +58,7 @@ class RouteDetector(BaseAnalyzer):
def _detect_fastapi_routes(self) -> list[dict]: def _detect_fastapi_routes(self) -> list[dict]:
"""Detect FastAPI routes.""" """Detect FastAPI routes."""
routes = [] routes = []
files_to_check = list(self.path.glob("**/*.py")) files_to_check = [f for f in self.path.glob("**/*.py") if self._should_include_file(f)]
for file_path in files_to_check: for file_path in files_to_check:
try: try:
@@ -113,7 +120,7 @@ class RouteDetector(BaseAnalyzer):
def _detect_flask_routes(self) -> list[dict]: def _detect_flask_routes(self) -> list[dict]:
"""Detect Flask routes.""" """Detect Flask routes."""
routes = [] routes = []
files_to_check = list(self.path.glob("**/*.py")) files_to_check = [f for f in self.path.glob("**/*.py") if self._should_include_file(f)]
for file_path in files_to_check: for file_path in files_to_check:
try: try:
@@ -160,7 +167,7 @@ class RouteDetector(BaseAnalyzer):
def _detect_django_routes(self) -> list[dict]: def _detect_django_routes(self) -> list[dict]:
"""Detect Django routes from urls.py files.""" """Detect Django routes from urls.py files."""
routes = [] routes = []
url_files = list(self.path.glob("**/urls.py")) url_files = [f for f in self.path.glob("**/urls.py") if self._should_include_file(f)]
for file_path in url_files: for file_path in url_files:
try: try:
@@ -194,10 +201,9 @@ class RouteDetector(BaseAnalyzer):
def _detect_express_routes(self) -> list[dict]: def _detect_express_routes(self) -> list[dict]:
"""Detect Express/Fastify/Koa routes.""" """Detect Express/Fastify/Koa routes."""
routes = [] routes = []
files_to_check = list(self.path.glob("**/*.js")) + list( js_files = [f for f in self.path.glob("**/*.js") if self._should_include_file(f)]
self.path.glob("**/*.ts") ts_files = [f for f in self.path.glob("**/*.ts") if self._should_include_file(f)]
) files_to_check = js_files + ts_files
for file_path in files_to_check: for file_path in files_to_check:
try: try:
content = file_path.read_text() content = file_path.read_text()
@@ -250,7 +256,8 @@ class RouteDetector(BaseAnalyzer):
app_dir = self.path / "app" app_dir = self.path / "app"
if app_dir.exists(): if app_dir.exists():
# Find all route.ts/js files # Find all route.ts/js files
for route_file in app_dir.glob("**/route.{ts,js,tsx,jsx}"): route_files = [f for f in app_dir.glob("**/route.{ts,js,tsx,jsx}") if self._should_include_file(f)]
for route_file in route_files:
# Convert file path to route path # Convert file path to route path
# app/api/users/[id]/route.ts -> /api/users/:id # app/api/users/[id]/route.ts -> /api/users/:id
relative_path = route_file.parent.relative_to(app_dir) relative_path = route_file.parent.relative_to(app_dir)
@@ -283,9 +290,9 @@ class RouteDetector(BaseAnalyzer):
# Next.js Pages Router (pages/api directory) # Next.js Pages Router (pages/api directory)
pages_api = self.path / "pages" / "api" pages_api = self.path / "pages" / "api"
if pages_api.exists(): if pages_api.exists():
for api_file in pages_api.glob("**/*.{ts,js,tsx,jsx}"): api_files = [f for f in pages_api.glob("**/*.{ts,js,tsx,jsx}") if self._should_include_file(f)]
if api_file.name.startswith("_"): for api_file in api_files:
continue if api_file.name.startswith('_'): continue
# Convert file path to route # Convert file path to route
relative_path = api_file.relative_to(pages_api) relative_path = api_file.relative_to(pages_api)
@@ -314,7 +321,7 @@ class RouteDetector(BaseAnalyzer):
def _detect_go_routes(self) -> list[dict]: def _detect_go_routes(self) -> list[dict]:
"""Detect Go framework routes (Gin, Echo, Chi, Fiber).""" """Detect Go framework routes (Gin, Echo, Chi, Fiber)."""
routes = [] routes = []
go_files = list(self.path.glob("**/*.go")) go_files = [f for f in self.path.glob("**/*.go") if self._should_include_file(f)]
for file_path in go_files: for file_path in go_files:
try: try:
@@ -348,7 +355,7 @@ class RouteDetector(BaseAnalyzer):
def _detect_rust_routes(self) -> list[dict]: def _detect_rust_routes(self) -> list[dict]:
"""Detect Rust framework routes (Axum, Actix).""" """Detect Rust framework routes (Axum, Actix)."""
routes = [] routes = []
rust_files = list(self.path.glob("**/*.rs")) rust_files = [f for f in self.path.glob("**/*.rs") if self._should_include_file(f)]
for file_path in rust_files: for file_path in rust_files:
try: try: