fix(coding-agent): disambiguate resource paths
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { realpathSync } from "node:fs";
|
||||
import { isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
|
||||
|
||||
/**
|
||||
* Resolve a path to its canonical (real) form, following symlinks.
|
||||
@@ -34,3 +35,23 @@ export function isLocalPath(value: string): boolean {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function resolveAgainstCwd(filePath: string, cwd: string): string {
|
||||
return isAbsolute(filePath) ? resolvePath(filePath) : resolvePath(cwd, filePath);
|
||||
}
|
||||
|
||||
export function getCwdRelativePath(filePath: string, cwd: string): string | undefined {
|
||||
const resolvedCwd = resolvePath(cwd);
|
||||
const resolvedPath = resolveAgainstCwd(filePath, resolvedCwd);
|
||||
const relativePath = relative(resolvedCwd, resolvedPath);
|
||||
const isInsideCwd =
|
||||
relativePath === "" ||
|
||||
(relativePath !== ".." && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath));
|
||||
|
||||
return isInsideCwd ? relativePath || "." : undefined;
|
||||
}
|
||||
|
||||
export function formatPathRelativeToCwdOrAbsolute(filePath: string, cwd: string): string {
|
||||
const absolutePath = resolveAgainstCwd(filePath, cwd);
|
||||
return (getCwdRelativePath(absolutePath, cwd) ?? absolutePath).split(sep).join("/");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user