Extract rule finding logic: - project-root-finder.ts: project root detection - rule-file-finder.ts: rule file discovery - rule-file-scanner.ts: filesystem scanning for rules - rule-distance.ts: rule-to-file distance calculation
37 lines
956 B
TypeScript
37 lines
956 B
TypeScript
import { existsSync, statSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { PROJECT_MARKERS } from "./constants";
|
|
|
|
/**
|
|
* Find project root by walking up from startPath.
|
|
* Checks for PROJECT_MARKERS (.git, pyproject.toml, package.json, etc.)
|
|
*
|
|
* @param startPath - Starting path to search from (file or directory)
|
|
* @returns Project root path or null if not found
|
|
*/
|
|
export function findProjectRoot(startPath: string): string | null {
|
|
let current: string;
|
|
|
|
try {
|
|
const stat = statSync(startPath);
|
|
current = stat.isDirectory() ? startPath : dirname(startPath);
|
|
} catch {
|
|
current = dirname(startPath);
|
|
}
|
|
|
|
while (true) {
|
|
for (const marker of PROJECT_MARKERS) {
|
|
const markerPath = join(current, marker);
|
|
if (existsSync(markerPath)) {
|
|
return current;
|
|
}
|
|
}
|
|
|
|
const parent = dirname(current);
|
|
if (parent === current) {
|
|
return null;
|
|
}
|
|
current = parent;
|
|
}
|
|
}
|