23 lines
791 B
TypeScript
23 lines
791 B
TypeScript
import { relative, resolve } from "node:path";
|
|
|
|
/** True when `candidate` resolves to a path under `rootDir` (not the root dir itself). */
|
|
export function isPathInsideRoot(rootDir: string, candidate: string): boolean {
|
|
const root = resolve(rootDir);
|
|
const target = resolve(candidate);
|
|
const rel = relative(root, target);
|
|
if (rel === "" || rel === ".") return false;
|
|
if (rel.startsWith("..")) return false;
|
|
// Cross-drive relative paths on Windows are absolute (e.g. D:\other\...)
|
|
if (/^[A-Za-z]:[\\/]/.test(rel)) return false;
|
|
return true;
|
|
}
|
|
|
|
export function isSameResolvedPath(a: string, b: string): boolean {
|
|
const left = resolve(a);
|
|
const right = resolve(b);
|
|
if (process.platform === "win32") {
|
|
return left.toLowerCase() === right.toLowerCase();
|
|
}
|
|
return left === right;
|
|
}
|