6870dcbb31c8e5a6709eae7bb44f6b046b8368820db839ea6fc80ec2c160fcbe5f24481f4aab4a66d105ccd96b627d0b80f923a7cdecb555da3006a96a03ff 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import path from 'path';
  2. function normalizeWindowsPath(input = "") {
  3. if (!input.includes("\\")) {
  4. return input;
  5. }
  6. return input.replace(/\\/g, "/");
  7. }
  8. const _UNC_REGEX = /^[/][/]/;
  9. const _UNC_DRIVE_REGEX = /^[/][/]([.]{1,2}[/])?([a-zA-Z]):[/]/;
  10. const _IS_ABSOLUTE_RE = /^\/|^\\|^[a-zA-Z]:[/\\]/;
  11. const sep = "/";
  12. const delimiter = ":";
  13. const normalize = function(path2) {
  14. if (path2.length === 0) {
  15. return ".";
  16. }
  17. path2 = normalizeWindowsPath(path2);
  18. const isUNCPath = path2.match(_UNC_REGEX);
  19. const hasUNCDrive = isUNCPath && path2.match(_UNC_DRIVE_REGEX);
  20. const isPathAbsolute = isAbsolute(path2);
  21. const trailingSeparator = path2[path2.length - 1] === "/";
  22. path2 = normalizeString(path2, !isPathAbsolute);
  23. if (path2.length === 0) {
  24. if (isPathAbsolute) {
  25. return "/";
  26. }
  27. return trailingSeparator ? "./" : ".";
  28. }
  29. if (trailingSeparator) {
  30. path2 += "/";
  31. }
  32. if (isUNCPath) {
  33. if (hasUNCDrive) {
  34. return `//./${path2}`;
  35. }
  36. return `//${path2}`;
  37. }
  38. return isPathAbsolute && !isAbsolute(path2) ? `/${path2}` : path2;
  39. };
  40. const join = function(...args) {
  41. if (args.length === 0) {
  42. return ".";
  43. }
  44. let joined;
  45. for (let i = 0; i < args.length; ++i) {
  46. const arg = args[i];
  47. if (arg.length > 0) {
  48. if (joined === void 0) {
  49. joined = arg;
  50. } else {
  51. joined += `/${arg}`;
  52. }
  53. }
  54. }
  55. if (joined === void 0) {
  56. return ".";
  57. }
  58. return normalize(joined);
  59. };
  60. const resolve = function(...args) {
  61. args = args.map((arg) => normalizeWindowsPath(arg));
  62. let resolvedPath = "";
  63. let resolvedAbsolute = false;
  64. for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  65. const path2 = i >= 0 ? args[i] : process.cwd();
  66. if (path2.length === 0) {
  67. continue;
  68. }
  69. resolvedPath = `${path2}/${resolvedPath}`;
  70. resolvedAbsolute = isAbsolute(path2);
  71. }
  72. resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
  73. if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
  74. return `/${resolvedPath}`;
  75. }
  76. return resolvedPath.length > 0 ? resolvedPath : ".";
  77. };
  78. function normalizeString(path2, allowAboveRoot) {
  79. let res = "";
  80. let lastSegmentLength = 0;
  81. let lastSlash = -1;
  82. let dots = 0;
  83. let char = null;
  84. for (let i = 0; i <= path2.length; ++i) {
  85. if (i < path2.length) {
  86. char = path2[i];
  87. } else if (char === "/") {
  88. break;
  89. } else {
  90. char = "/";
  91. }
  92. if (char === "/") {
  93. if (lastSlash === i - 1 || dots === 1) ; else if (dots === 2) {
  94. if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
  95. if (res.length > 2) {
  96. const lastSlashIndex = res.lastIndexOf("/");
  97. if (lastSlashIndex === -1) {
  98. res = "";
  99. lastSegmentLength = 0;
  100. } else {
  101. res = res.slice(0, lastSlashIndex);
  102. lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
  103. }
  104. lastSlash = i;
  105. dots = 0;
  106. continue;
  107. } else if (res.length !== 0) {
  108. res = "";
  109. lastSegmentLength = 0;
  110. lastSlash = i;
  111. dots = 0;
  112. continue;
  113. }
  114. }
  115. if (allowAboveRoot) {
  116. res += res.length > 0 ? "/.." : "..";
  117. lastSegmentLength = 2;
  118. }
  119. } else {
  120. if (res.length > 0) {
  121. res += `/${path2.slice(lastSlash + 1, i)}`;
  122. } else {
  123. res = path2.slice(lastSlash + 1, i);
  124. }
  125. lastSegmentLength = i - lastSlash - 1;
  126. }
  127. lastSlash = i;
  128. dots = 0;
  129. } else if (char === "." && dots !== -1) {
  130. ++dots;
  131. } else {
  132. dots = -1;
  133. }
  134. }
  135. return res;
  136. }
  137. const isAbsolute = function(p) {
  138. return _IS_ABSOLUTE_RE.test(p);
  139. };
  140. const toNamespacedPath = function(p) {
  141. return normalizeWindowsPath(p);
  142. };
  143. const extname = function(p) {
  144. return path.posix.extname(normalizeWindowsPath(p));
  145. };
  146. const relative = function(from, to) {
  147. return path.posix.relative(normalizeWindowsPath(from), normalizeWindowsPath(to));
  148. };
  149. const dirname = function(p) {
  150. return path.posix.dirname(normalizeWindowsPath(p));
  151. };
  152. const format = function(p) {
  153. return normalizeWindowsPath(path.posix.format(p));
  154. };
  155. const basename = function(p, ext) {
  156. return path.posix.basename(normalizeWindowsPath(p), ext);
  157. };
  158. const parse = function(p) {
  159. return path.posix.parse(normalizeWindowsPath(p));
  160. };
  161. const _path = /*#__PURE__*/Object.freeze({
  162. __proto__: null,
  163. sep: sep,
  164. delimiter: delimiter,
  165. normalize: normalize,
  166. join: join,
  167. resolve: resolve,
  168. normalizeString: normalizeString,
  169. isAbsolute: isAbsolute,
  170. toNamespacedPath: toNamespacedPath,
  171. extname: extname,
  172. relative: relative,
  173. dirname: dirname,
  174. format: format,
  175. basename: basename,
  176. parse: parse
  177. });
  178. const index = {
  179. ..._path
  180. };
  181. export { basename, index as default, delimiter, dirname, extname, format, isAbsolute, join, normalize, normalizeString, parse, relative, resolve, sep, toNamespacedPath };