pathe.1f0a373c.cjs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
  3. function normalizeWindowsPath(input = "") {
  4. if (!input) {
  5. return input;
  6. }
  7. return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
  8. }
  9. const _UNC_REGEX = /^[/\\]{2}/;
  10. const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
  11. const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
  12. const _ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/;
  13. const sep = "/";
  14. const delimiter = ":";
  15. const normalize = function(path) {
  16. if (path.length === 0) {
  17. return ".";
  18. }
  19. path = normalizeWindowsPath(path);
  20. const isUNCPath = path.match(_UNC_REGEX);
  21. const isPathAbsolute = isAbsolute(path);
  22. const trailingSeparator = path[path.length - 1] === "/";
  23. path = normalizeString(path, !isPathAbsolute);
  24. if (path.length === 0) {
  25. if (isPathAbsolute) {
  26. return "/";
  27. }
  28. return trailingSeparator ? "./" : ".";
  29. }
  30. if (trailingSeparator) {
  31. path += "/";
  32. }
  33. if (_DRIVE_LETTER_RE.test(path)) {
  34. path += "/";
  35. }
  36. if (isUNCPath) {
  37. if (!isPathAbsolute) {
  38. return `//./${path}`;
  39. }
  40. return `//${path}`;
  41. }
  42. return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
  43. };
  44. const join = function(...arguments_) {
  45. if (arguments_.length === 0) {
  46. return ".";
  47. }
  48. let joined;
  49. for (const argument of arguments_) {
  50. if (argument && argument.length > 0) {
  51. if (joined === void 0) {
  52. joined = argument;
  53. } else {
  54. joined += `/${argument}`;
  55. }
  56. }
  57. }
  58. if (joined === void 0) {
  59. return ".";
  60. }
  61. return normalize(joined.replace(/\/\/+/g, "/"));
  62. };
  63. function cwd() {
  64. if (typeof process !== "undefined" && typeof process.cwd === "function") {
  65. return process.cwd().replace(/\\/g, "/");
  66. }
  67. return "/";
  68. }
  69. const resolve = function(...arguments_) {
  70. arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
  71. let resolvedPath = "";
  72. let resolvedAbsolute = false;
  73. for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
  74. const path = index >= 0 ? arguments_[index] : cwd();
  75. if (!path || path.length === 0) {
  76. continue;
  77. }
  78. resolvedPath = `${path}/${resolvedPath}`;
  79. resolvedAbsolute = isAbsolute(path);
  80. }
  81. resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
  82. if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
  83. return `/${resolvedPath}`;
  84. }
  85. return resolvedPath.length > 0 ? resolvedPath : ".";
  86. };
  87. function normalizeString(path, allowAboveRoot) {
  88. let res = "";
  89. let lastSegmentLength = 0;
  90. let lastSlash = -1;
  91. let dots = 0;
  92. let char = null;
  93. for (let index = 0; index <= path.length; ++index) {
  94. if (index < path.length) {
  95. char = path[index];
  96. } else if (char === "/") {
  97. break;
  98. } else {
  99. char = "/";
  100. }
  101. if (char === "/") {
  102. if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) {
  103. if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
  104. if (res.length > 2) {
  105. const lastSlashIndex = res.lastIndexOf("/");
  106. if (lastSlashIndex === -1) {
  107. res = "";
  108. lastSegmentLength = 0;
  109. } else {
  110. res = res.slice(0, lastSlashIndex);
  111. lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
  112. }
  113. lastSlash = index;
  114. dots = 0;
  115. continue;
  116. } else if (res.length > 0) {
  117. res = "";
  118. lastSegmentLength = 0;
  119. lastSlash = index;
  120. dots = 0;
  121. continue;
  122. }
  123. }
  124. if (allowAboveRoot) {
  125. res += res.length > 0 ? "/.." : "..";
  126. lastSegmentLength = 2;
  127. }
  128. } else {
  129. if (res.length > 0) {
  130. res += `/${path.slice(lastSlash + 1, index)}`;
  131. } else {
  132. res = path.slice(lastSlash + 1, index);
  133. }
  134. lastSegmentLength = index - lastSlash - 1;
  135. }
  136. lastSlash = index;
  137. dots = 0;
  138. } else if (char === "." && dots !== -1) {
  139. ++dots;
  140. } else {
  141. dots = -1;
  142. }
  143. }
  144. return res;
  145. }
  146. const isAbsolute = function(p) {
  147. return _IS_ABSOLUTE_RE.test(p);
  148. };
  149. const toNamespacedPath = function(p) {
  150. return normalizeWindowsPath(p);
  151. };
  152. const _EXTNAME_RE = /.(\.[^./]+)$/;
  153. const extname = function(p) {
  154. const match = _EXTNAME_RE.exec(normalizeWindowsPath(p));
  155. return match && match[1] || "";
  156. };
  157. const relative = function(from, to) {
  158. const _from = resolve(from).replace(_ROOT_FOLDER_RE, "$1").split("/");
  159. const _to = resolve(to).replace(_ROOT_FOLDER_RE, "$1").split("/");
  160. if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) {
  161. return _to.join("/");
  162. }
  163. const _fromCopy = [..._from];
  164. for (const segment of _fromCopy) {
  165. if (_to[0] !== segment) {
  166. break;
  167. }
  168. _from.shift();
  169. _to.shift();
  170. }
  171. return [..._from.map(() => ".."), ..._to].join("/");
  172. };
  173. const dirname = function(p) {
  174. const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
  175. if (segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0])) {
  176. segments[0] += "/";
  177. }
  178. return segments.join("/") || (isAbsolute(p) ? "/" : ".");
  179. };
  180. const format = function(p) {
  181. const segments = [p.root, p.dir, p.base ?? p.name + p.ext].filter(Boolean);
  182. return normalizeWindowsPath(
  183. p.root ? resolve(...segments) : segments.join("/")
  184. );
  185. };
  186. const basename = function(p, extension) {
  187. const lastSegment = normalizeWindowsPath(p).split("/").pop();
  188. return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment;
  189. };
  190. const parse = function(p) {
  191. const root = normalizeWindowsPath(p).split("/").shift() || "/";
  192. const base = basename(p);
  193. const extension = extname(base);
  194. return {
  195. root,
  196. dir: dirname(p),
  197. base,
  198. ext: extension,
  199. name: base.slice(0, base.length - extension.length)
  200. };
  201. };
  202. const path = {
  203. __proto__: null,
  204. basename: basename,
  205. delimiter: delimiter,
  206. dirname: dirname,
  207. extname: extname,
  208. format: format,
  209. isAbsolute: isAbsolute,
  210. join: join,
  211. normalize: normalize,
  212. normalizeString: normalizeString,
  213. parse: parse,
  214. relative: relative,
  215. resolve: resolve,
  216. sep: sep,
  217. toNamespacedPath: toNamespacedPath
  218. };
  219. exports.basename = basename;
  220. exports.delimiter = delimiter;
  221. exports.dirname = dirname;
  222. exports.extname = extname;
  223. exports.format = format;
  224. exports.isAbsolute = isAbsolute;
  225. exports.join = join;
  226. exports.normalize = normalize;
  227. exports.normalizeString = normalizeString;
  228. exports.normalizeWindowsPath = normalizeWindowsPath;
  229. exports.parse = parse;
  230. exports.path = path;
  231. exports.relative = relative;
  232. exports.resolve = resolve;
  233. exports.sep = sep;
  234. exports.toNamespacedPath = toNamespacedPath;