path.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Slightly modified (no IE8 support, ES6) and transcribed to TypeScript
  2. // https://github.com/calvinmetcalf/rollup-plugin-node-builtins/blob/63ab8aacd013767445ca299e468d9a60a95328d7/src/es6/path.js
  3. //
  4. // Copyright Joyent, Inc.and other Node contributors.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a
  7. // copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to permit
  11. // persons to whom the Software is furnished to do so, subject to the
  12. // following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included
  15. // in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  20. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  21. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. /** JSDoc */
  25. function normalizeArray(parts, allowAboveRoot) {
  26. // if the path tries to go above the root, `up` ends up > 0
  27. let up = 0;
  28. for (let i = parts.length - 1; i >= 0; i--) {
  29. const last = parts[i];
  30. if (last === '.') {
  31. parts.splice(i, 1);
  32. } else if (last === '..') {
  33. parts.splice(i, 1);
  34. up++;
  35. } else if (up) {
  36. parts.splice(i, 1);
  37. up--;
  38. }
  39. }
  40. // if the path is allowed to go above the root, restore leading ..s
  41. if (allowAboveRoot) {
  42. for (; up--; up) {
  43. parts.unshift('..');
  44. }
  45. }
  46. return parts;
  47. }
  48. // Split a filename into [root, dir, basename, ext], unix version
  49. // 'root' is just a slash, or nothing.
  50. const splitPathRe = /^(\S+:\\|\/?)([\s\S]*?)((?:\.{1,2}|[^/\\]+?|)(\.[^./\\]*|))(?:[/\\]*)$/;
  51. /** JSDoc */
  52. function splitPath(filename) {
  53. // Truncate files names greater than 1024 characters to avoid regex dos
  54. // https://github.com/getsentry/sentry-javascript/pull/8737#discussion_r1285719172
  55. const truncated = filename.length > 1024 ? `<truncated>${filename.slice(-1024)}` : filename;
  56. const parts = splitPathRe.exec(truncated);
  57. return parts ? parts.slice(1) : [];
  58. }
  59. // path.resolve([from ...], to)
  60. // posix version
  61. /** JSDoc */
  62. function resolve(...args) {
  63. let resolvedPath = '';
  64. let resolvedAbsolute = false;
  65. for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  66. const path = i >= 0 ? args[i] : '/';
  67. // Skip empty entries
  68. if (!path) {
  69. continue;
  70. }
  71. resolvedPath = `${path}/${resolvedPath}`;
  72. resolvedAbsolute = path.charAt(0) === '/';
  73. }
  74. // At this point the path should be resolved to a full absolute path, but
  75. // handle relative paths to be safe (might happen when process.cwd() fails)
  76. // Normalize the path
  77. resolvedPath = normalizeArray(
  78. resolvedPath.split('/').filter(p => !!p),
  79. !resolvedAbsolute,
  80. ).join('/');
  81. return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';
  82. }
  83. /** JSDoc */
  84. function trim(arr) {
  85. let start = 0;
  86. for (; start < arr.length; start++) {
  87. if (arr[start] !== '') {
  88. break;
  89. }
  90. }
  91. let end = arr.length - 1;
  92. for (; end >= 0; end--) {
  93. if (arr[end] !== '') {
  94. break;
  95. }
  96. }
  97. if (start > end) {
  98. return [];
  99. }
  100. return arr.slice(start, end - start + 1);
  101. }
  102. // path.relative(from, to)
  103. // posix version
  104. /** JSDoc */
  105. function relative(from, to) {
  106. /* eslint-disable no-param-reassign */
  107. from = resolve(from).slice(1);
  108. to = resolve(to).slice(1);
  109. /* eslint-enable no-param-reassign */
  110. const fromParts = trim(from.split('/'));
  111. const toParts = trim(to.split('/'));
  112. const length = Math.min(fromParts.length, toParts.length);
  113. let samePartsLength = length;
  114. for (let i = 0; i < length; i++) {
  115. if (fromParts[i] !== toParts[i]) {
  116. samePartsLength = i;
  117. break;
  118. }
  119. }
  120. let outputParts = [];
  121. for (let i = samePartsLength; i < fromParts.length; i++) {
  122. outputParts.push('..');
  123. }
  124. outputParts = outputParts.concat(toParts.slice(samePartsLength));
  125. return outputParts.join('/');
  126. }
  127. // path.normalize(path)
  128. // posix version
  129. /** JSDoc */
  130. function normalizePath(path) {
  131. const isPathAbsolute = isAbsolute(path);
  132. const trailingSlash = path.slice(-1) === '/';
  133. // Normalize the path
  134. let normalizedPath = normalizeArray(
  135. path.split('/').filter(p => !!p),
  136. !isPathAbsolute,
  137. ).join('/');
  138. if (!normalizedPath && !isPathAbsolute) {
  139. normalizedPath = '.';
  140. }
  141. if (normalizedPath && trailingSlash) {
  142. normalizedPath += '/';
  143. }
  144. return (isPathAbsolute ? '/' : '') + normalizedPath;
  145. }
  146. // posix version
  147. /** JSDoc */
  148. function isAbsolute(path) {
  149. return path.charAt(0) === '/';
  150. }
  151. // posix version
  152. /** JSDoc */
  153. function join(...args) {
  154. return normalizePath(args.join('/'));
  155. }
  156. /** JSDoc */
  157. function dirname(path) {
  158. const result = splitPath(path);
  159. const root = result[0];
  160. let dir = result[1];
  161. if (!root && !dir) {
  162. // No dirname whatsoever
  163. return '.';
  164. }
  165. if (dir) {
  166. // It has a dirname, strip trailing slash
  167. dir = dir.slice(0, dir.length - 1);
  168. }
  169. return root + dir;
  170. }
  171. /** JSDoc */
  172. function basename(path, ext) {
  173. let f = splitPath(path)[2];
  174. if (ext && f.slice(ext.length * -1) === ext) {
  175. f = f.slice(0, f.length - ext.length);
  176. }
  177. return f;
  178. }
  179. export { basename, dirname, isAbsolute, join, normalizePath, relative, resolve };
  180. //# sourceMappingURL=path.js.map