refactor(rules-injector): split finder.ts into rule discovery modules
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
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user