161 lines
4.5 KiB
JavaScript
161 lines
4.5 KiB
JavaScript
function normalizeWindowsPath(input = "") {
|
|
if (!input || !input.includes("\\")) {
|
|
return input;
|
|
}
|
|
return input.replace(/\\/g, "/");
|
|
}
|
|
|
|
const _UNC_REGEX = /^[/\\]{2}/;
|
|
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
const normalize = function(path) {
|
|
if (path.length === 0) {
|
|
return ".";
|
|
}
|
|
path = normalizeWindowsPath(path);
|
|
const isUNCPath = path.match(_UNC_REGEX);
|
|
const isPathAbsolute = isAbsolute(path);
|
|
const trailingSeparator = path[path.length - 1] === "/";
|
|
path = normalizeString(path, !isPathAbsolute);
|
|
if (path.length === 0) {
|
|
if (isPathAbsolute) {
|
|
return "/";
|
|
}
|
|
return trailingSeparator ? "./" : ".";
|
|
}
|
|
if (trailingSeparator) {
|
|
path += "/";
|
|
}
|
|
if (_DRIVE_LETTER_RE.test(path)) {
|
|
path += "/";
|
|
}
|
|
if (isUNCPath) {
|
|
if (!isPathAbsolute) {
|
|
return `//./${path}`;
|
|
}
|
|
return `//${path}`;
|
|
}
|
|
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
};
|
|
const join = function(...arguments_) {
|
|
if (arguments_.length === 0) {
|
|
return ".";
|
|
}
|
|
let joined;
|
|
for (const argument of arguments_) {
|
|
if (argument && argument.length > 0) {
|
|
if (joined === void 0) {
|
|
joined = argument;
|
|
} else {
|
|
joined += `/${argument}`;
|
|
}
|
|
}
|
|
}
|
|
if (joined === void 0) {
|
|
return ".";
|
|
}
|
|
return normalize(joined.replace(/\/\/+/g, "/"));
|
|
};
|
|
const resolve = function(...arguments_) {
|
|
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
let resolvedPath = "";
|
|
let resolvedAbsolute = false;
|
|
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
const path = index >= 0 ? arguments_[index] : process.cwd().replace(/\\/g, "/");
|
|
if (!path || path.length === 0) {
|
|
continue;
|
|
}
|
|
resolvedPath = `${path}/${resolvedPath}`;
|
|
resolvedAbsolute = isAbsolute(path);
|
|
}
|
|
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
|
|
return `/${resolvedPath}`;
|
|
}
|
|
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
};
|
|
function normalizeString(path, allowAboveRoot) {
|
|
let res = "";
|
|
let lastSegmentLength = 0;
|
|
let lastSlash = -1;
|
|
let dots = 0;
|
|
let char = null;
|
|
for (let index = 0; index <= path.length; ++index) {
|
|
if (index < path.length) {
|
|
char = path[index];
|
|
} else if (char === "/") {
|
|
break;
|
|
} else {
|
|
char = "/";
|
|
}
|
|
if (char === "/") {
|
|
if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) {
|
|
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
if (res.length > 2) {
|
|
const lastSlashIndex = res.lastIndexOf("/");
|
|
if (lastSlashIndex === -1) {
|
|
res = "";
|
|
lastSegmentLength = 0;
|
|
} else {
|
|
res = res.slice(0, lastSlashIndex);
|
|
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
}
|
|
lastSlash = index;
|
|
dots = 0;
|
|
continue;
|
|
} else if (res.length > 0) {
|
|
res = "";
|
|
lastSegmentLength = 0;
|
|
lastSlash = index;
|
|
dots = 0;
|
|
continue;
|
|
}
|
|
}
|
|
if (allowAboveRoot) {
|
|
res += res.length > 0 ? "/.." : "..";
|
|
lastSegmentLength = 2;
|
|
}
|
|
} else {
|
|
if (res.length > 0) {
|
|
res += `/${path.slice(lastSlash + 1, index)}`;
|
|
} else {
|
|
res = path.slice(lastSlash + 1, index);
|
|
}
|
|
lastSegmentLength = index - lastSlash - 1;
|
|
}
|
|
lastSlash = index;
|
|
dots = 0;
|
|
} else if (char === "." && dots !== -1) {
|
|
++dots;
|
|
} else {
|
|
dots = -1;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
const isAbsolute = function(p) {
|
|
return _IS_ABSOLUTE_RE.test(p);
|
|
};
|
|
const relative = function(from, to) {
|
|
const _from = resolve(from).split("/");
|
|
const _to = resolve(to).split("/");
|
|
const _fromCopy = [..._from];
|
|
for (const segment of _fromCopy) {
|
|
if (_to[0] !== segment) {
|
|
break;
|
|
}
|
|
_from.shift();
|
|
_to.shift();
|
|
}
|
|
return [..._from.map(() => ".."), ..._to].join("/");
|
|
};
|
|
const dirname = function(p) {
|
|
const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
|
|
if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) {
|
|
segments[0] += "/";
|
|
}
|
|
return segments.join("/") || (isAbsolute(p) ? "/" : ".");
|
|
};
|
|
|
|
export { relative as a, dirname as d, isAbsolute as i, join as j, normalize as n, resolve as r };
|