mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-04 17:19:29 +08:00
207 lines
5.7 KiB
Text
207 lines
5.7 KiB
Text
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
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 sep = "/";
|
|
const delimiter = ":";
|
|
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(...args) {
|
|
if (args.length === 0) {
|
|
return ".";
|
|
}
|
|
let joined;
|
|
for (let i = 0; i < args.length; ++i) {
|
|
const arg = args[i];
|
|
if (arg && arg.length > 0) {
|
|
if (joined === void 0) {
|
|
joined = arg;
|
|
} else {
|
|
joined += `/${arg}`;
|
|
}
|
|
}
|
|
}
|
|
if (joined === void 0) {
|
|
return ".";
|
|
}
|
|
return normalize(joined.replace(/\/\/+/g, "/"));
|
|
};
|
|
const resolve = function(...args) {
|
|
args = args.map((arg) => normalizeWindowsPath(arg));
|
|
let resolvedPath = "";
|
|
let resolvedAbsolute = false;
|
|
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
|
const path = i >= 0 ? args[i] : 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 i = 0; i <= path.length; ++i) {
|
|
if (i < path.length) {
|
|
char = path[i];
|
|
} else if (char === "/") {
|
|
break;
|
|
} else {
|
|
char = "/";
|
|
}
|
|
if (char === "/") {
|
|
if (lastSlash === i - 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 = i;
|
|
dots = 0;
|
|
continue;
|
|
} else if (res.length !== 0) {
|
|
res = "";
|
|
lastSegmentLength = 0;
|
|
lastSlash = i;
|
|
dots = 0;
|
|
continue;
|
|
}
|
|
}
|
|
if (allowAboveRoot) {
|
|
res += res.length > 0 ? "/.." : "..";
|
|
lastSegmentLength = 2;
|
|
}
|
|
} else {
|
|
if (res.length > 0) {
|
|
res += `/${path.slice(lastSlash + 1, i)}`;
|
|
} else {
|
|
res = path.slice(lastSlash + 1, i);
|
|
}
|
|
lastSegmentLength = i - lastSlash - 1;
|
|
}
|
|
lastSlash = i;
|
|
dots = 0;
|
|
} else if (char === "." && dots !== -1) {
|
|
++dots;
|
|
} else {
|
|
dots = -1;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
const isAbsolute = function(p) {
|
|
return _IS_ABSOLUTE_RE.test(p);
|
|
};
|
|
const toNamespacedPath = function(p) {
|
|
return normalizeWindowsPath(p);
|
|
};
|
|
const _EXTNAME_RE = /(?<!^)\.[^/.]+$/;
|
|
const extname = function(p) {
|
|
const match = _EXTNAME_RE.exec(normalizeWindowsPath(p));
|
|
return match && match[0] || "";
|
|
};
|
|
const relative = function(from, to) {
|
|
const _from = resolve(from).split("/");
|
|
const _to = resolve(to).split("/");
|
|
for (const segment of [..._from]) {
|
|
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) ? "/" : ".");
|
|
};
|
|
const format = function(p) {
|
|
const segments = [p.root, p.dir, p.base ?? p.name + p.ext].filter(Boolean);
|
|
return normalizeWindowsPath(p.root ? resolve(...segments) : segments.join("/"));
|
|
};
|
|
const basename = function(p, ext) {
|
|
const lastSegment = normalizeWindowsPath(p).split("/").pop();
|
|
return lastSegment.endsWith(ext) ? lastSegment.slice(0, -ext.length) : lastSegment;
|
|
};
|
|
const parse = function(p) {
|
|
const root = normalizeWindowsPath(p).split("/").shift() || "/";
|
|
const base = basename(p);
|
|
const ext = extname(base);
|
|
return {
|
|
root,
|
|
dir: dirname(p),
|
|
base,
|
|
ext,
|
|
name: base.slice(0, base.length - ext.length)
|
|
};
|
|
};
|
|
|
|
exports.basename = basename;
|
|
exports.delimiter = delimiter;
|
|
exports.dirname = dirname;
|
|
exports.extname = extname;
|
|
exports.format = format;
|
|
exports.isAbsolute = isAbsolute;
|
|
exports.join = join;
|
|
exports.normalize = normalize;
|
|
exports.normalizeString = normalizeString;
|
|
exports.parse = parse;
|
|
exports.relative = relative;
|
|
exports.resolve = resolve;
|
|
exports.sep = sep;
|
|
exports.toNamespacedPath = toNamespacedPath;
|