oh-my-opencode/src/hooks/rules-injector/project-root-finder.ts
YeonGyu-Kim 2d22a54b55 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
2026-02-08 16:22:33 +09:00

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;
}
}