UNPKG

@visulima/path

Version:

Drop-in replacement of the Node.js path module.

382 lines (371 loc) 6.87 kB
import { createRequire as __cjs_createRequire } from "node:module"; const __cjs_require = __cjs_createRequire(import.meta.url); const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process; const __cjs_getBuiltinModule = (module) => { // Check if we're in Node.js and version supports getBuiltinModule if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) { const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number); // Node.js 20.16.0+ and 22.3.0+ if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) { return __cjs_getProcess.getBuiltinModule(module); } } // Fallback to createRequire return __cjs_require(module); }; const { fileURLToPath } = __cjs_getBuiltinModule("node:url"); import { k as normalizeWindowsPath, j as join, e as extname } from './packem_shared/path-CR6YkPXX.js'; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var binaryExtensions$1; var hasRequiredBinaryExtensions; function requireBinaryExtensions () { if (hasRequiredBinaryExtensions) return binaryExtensions$1; hasRequiredBinaryExtensions = 1; binaryExtensions$1 = [ "3dm", "3ds", "3g2", "3gp", "7z", "a", "aac", "adp", "afdesign", "afphoto", "afpub", "ai", "aif", "aiff", "alz", "ape", "apk", "appimage", "ar", "arj", "asf", "au", "avi", "bak", "baml", "bh", "bin", "bk", "bmp", "btif", "bz2", "bzip2", "cab", "caf", "cgm", "class", "cmx", "cpio", "cr2", "cr3", "cur", "dat", "dcm", "deb", "dex", "djvu", "dll", "dmg", "dng", "doc", "docm", "docx", "dot", "dotm", "dra", "DS_Store", "dsk", "dts", "dtshd", "dvb", "dwg", "dxf", "ecelp4800", "ecelp7470", "ecelp9600", "egg", "eol", "eot", "epub", "exe", "f4v", "fbs", "fh", "fla", "flac", "flatpak", "fli", "flv", "fpx", "fst", "fvt", "g3", "gh", "gif", "graffle", "gz", "gzip", "h261", "h263", "h264", "icns", "ico", "ief", "img", "ipa", "iso", "jar", "jpeg", "jpg", "jpgv", "jpm", "jxr", "key", "ktx", "lha", "lib", "lvp", "lz", "lzh", "lzma", "lzo", "m3u", "m4a", "m4v", "mar", "mdi", "mht", "mid", "midi", "mj2", "mka", "mkv", "mmr", "mng", "mobi", "mov", "movie", "mp3", "mp4", "mp4a", "mpeg", "mpg", "mpga", "mxu", "nef", "npx", "numbers", "nupkg", "o", "odp", "ods", "odt", "oga", "ogg", "ogv", "otf", "ott", "pages", "pbm", "pcx", "pdb", "pdf", "pea", "pgm", "pic", "png", "pnm", "pot", "potm", "potx", "ppa", "ppam", "ppm", "pps", "ppsm", "ppsx", "ppt", "pptm", "pptx", "psd", "pya", "pyc", "pyo", "pyv", "qt", "rar", "ras", "raw", "resources", "rgb", "rip", "rlc", "rmf", "rmvb", "rpm", "rtf", "rz", "s3m", "s7z", "scpt", "sgi", "shar", "snap", "sil", "sketch", "slk", "smv", "snk", "so", "stl", "suo", "sub", "swf", "tar", "tbz", "tbz2", "tga", "tgz", "thmx", "tif", "tiff", "tlz", "ttc", "ttf", "txz", "udf", "uvh", "uvi", "uvm", "uvp", "uvs", "uvu", "viv", "vob", "war", "wav", "wax", "wbmp", "wdp", "weba", "webm", "webp", "whl", "wim", "wm", "wma", "wmv", "wmx", "woff", "woff2", "wrm", "wvx", "xbm", "xif", "xla", "xlam", "xls", "xlsb", "xlsm", "xlsx", "xlt", "xltm", "xltx", "xm", "xmind", "xpi", "xpm", "xwd", "xz", "z", "zip", "zipx" ]; return binaryExtensions$1; } var binaryExtensionsExports = /*@__PURE__*/ requireBinaryExtensions(); const binaryExtensions = /*@__PURE__*/getDefaultExportFromCjs(binaryExtensionsExports); const extensions = new Set(binaryExtensions); const pathSeparators = /* @__PURE__ */ new Set(["/", "\\", void 0]); const normalizedAliasSymbol = Symbol.for("pathe:normalizedAlias"); const FILENAME_RE = /(^|[/\\])([^/\\]+?)(?=(?:\.[^.]+)?$)/; const compareAliases = (a, b) => b.split("/").length - a.split("/").length; const hasTrailingSlash = (path = "/") => { const lastChar = path.at(-1); return lastChar === "/" || lastChar === "\\"; }; const normalizeAliases = (_aliases) => { if (_aliases[normalizedAliasSymbol]) { return _aliases; } const aliases = Object.fromEntries(Object.entries(_aliases).toSorted(([a], [b]) => compareAliases(a, b))); for (const key in aliases) { for (const alias in aliases) { if (alias === key || key.startsWith(alias)) { continue; } if (aliases[key].startsWith(alias) && pathSeparators.has(aliases[key][alias.length])) { aliases[key] = aliases[alias] + aliases[key].slice(alias.length); } } } Object.defineProperty(aliases, normalizedAliasSymbol, { enumerable: false, value: true }); return aliases; }; const resolveAlias = (path, aliases) => { path = normalizeWindowsPath(path); aliases = normalizeAliases(aliases); for (const [alias, to] of Object.entries(aliases)) { if (!path.startsWith(alias)) { continue; } const stripedAlias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias; if (hasTrailingSlash(path[stripedAlias.length])) { return join(to, path.slice(alias.length)); } } return path; }; const filename = (path) => FILENAME_RE.exec(path)?.[2]; const reverseResolveAlias = (path, aliases) => { path = normalizeWindowsPath(path); aliases = normalizeAliases(aliases); for (const [to, alias] of Object.entries(aliases).toReversed()) { if (!path.startsWith(alias)) { continue; } const stripedAlias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias; if (hasTrailingSlash(path[stripedAlias.length])) { return join(to, path.slice(alias.length)); } } return path; }; const isRelative = (path) => /^(?:\.?\.[/\\]|\.\.\B)/.test(path) || path === ".."; const isBinaryPath = (path) => extensions.has(extname(path).slice(1).toLowerCase()); const toPath = (urlOrPath) => normalizeWindowsPath(urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath); const isWindows = () => { if (!globalThis?.process) { return false; } if (globalThis?.process?.platform === "win32" || globalThis?.process?.platform === "cygwin") { return true; } const osType = globalThis?.process?.env.OSTYPE; if (typeof osType !== "string") { return false; } return /^(?:msys|cygwin)$/.test(osType); }; export { filename, isBinaryPath, isRelative, isWindows, normalizeAliases, resolveAlias, reverseResolveAlias, toPath };