refactor(packages): rename rules-core to rules-engine
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { dirname, relative } from "node:path";
|
||||
import { GLOBAL_DISTANCE } from "./constants";
|
||||
|
||||
export function calculateDistance(rulePath: string, currentFile: string, projectRoot: string | null): number {
|
||||
if (!projectRoot) return GLOBAL_DISTANCE;
|
||||
try {
|
||||
const ruleRelative = relative(projectRoot, dirname(rulePath));
|
||||
const currentRelative = relative(projectRoot, dirname(currentFile));
|
||||
if (ruleRelative.startsWith("..") || currentRelative.startsWith("..")) return GLOBAL_DISTANCE;
|
||||
const ruleParts = toParts(ruleRelative);
|
||||
const currentParts = toParts(currentRelative);
|
||||
let shared = 0;
|
||||
for (let index = 0; index < Math.min(ruleParts.length, currentParts.length); index += 1) {
|
||||
if (ruleParts[index] !== currentParts[index]) break;
|
||||
shared += 1;
|
||||
}
|
||||
return currentParts.length - shared;
|
||||
} catch {
|
||||
return GLOBAL_DISTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
function toParts(path: string): string[] {
|
||||
return path.split(/[/\\]/).filter(Boolean);
|
||||
}
|
||||
Reference in New Issue
Block a user