index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. // resolves . and .. elements in a path array with directory names there
  22. // must be no slashes, empty elements, or device names (c:\) in the array
  23. // (so also no leading and trailing slashes - it does not distinguish
  24. // relative and absolute paths)
  25. function normalizeArray(parts, allowAboveRoot) {
  26. // if the path tries to go above the root, `up` ends up > 0
  27. var up = 0;
  28. for (var i = parts.length - 1; i >= 0; i--) {
  29. var 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. var splitPathRe =
  51. /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
  52. var splitPath = function(filename) {
  53. return splitPathRe.exec(filename).slice(1);
  54. };
  55. // path.resolve([from ...], to)
  56. // posix version
  57. exports.resolve = function() {
  58. var resolvedPath = '',
  59. resolvedAbsolute = false;
  60. for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  61. var path = (i >= 0) ? arguments[i] : process.cwd();
  62. // Skip empty and invalid entries
  63. if (typeof path !== 'string') {
  64. throw new TypeError('Arguments to path.resolve must be strings');
  65. } else if (!path) {
  66. continue;
  67. }
  68. resolvedPath = path + '/' + resolvedPath;
  69. resolvedAbsolute = path.charAt(0) === '/';
  70. }
  71. // At this point the path should be resolved to a full absolute path, but
  72. // handle relative paths to be safe (might happen when process.cwd() fails)
  73. // Normalize the path
  74. resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
  75. return !!p;
  76. }), !resolvedAbsolute).join('/');
  77. return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
  78. };
  79. // path.normalize(path)
  80. // posix version
  81. exports.normalize = function(path) {
  82. var isAbsolute = exports.isAbsolute(path),
  83. trailingSlash = substr(path, -1) === '/';
  84. // Normalize the path
  85. path = normalizeArray(filter(path.split('/'), function(p) {
  86. return !!p;
  87. }), !isAbsolute).join('/');
  88. if (!path && !isAbsolute) {
  89. path = '.';
  90. }
  91. if (path && trailingSlash) {
  92. path += '/';
  93. }
  94. return (isAbsolute ? '/' : '') + path;
  95. };
  96. // posix version
  97. exports.isAbsolute = function(path) {
  98. return path.charAt(0) === '/';
  99. };
  100. // posix version
  101. exports.join = function() {
  102. var paths = Array.prototype.slice.call(arguments, 0);
  103. return exports.normalize(filter(paths, function(p, index) {
  104. if (typeof p !== 'string') {
  105. throw new TypeError('Arguments to path.join must be strings');
  106. }
  107. return p;
  108. }).join('/'));
  109. };
  110. // path.relative(from, to)
  111. // posix version
  112. exports.relative = function(from, to) {
  113. from = exports.resolve(from).substr(1);
  114. to = exports.resolve(to).substr(1);
  115. function trim(arr) {
  116. var start = 0;
  117. for (; start < arr.length; start++) {
  118. if (arr[start] !== '') break;
  119. }
  120. var end = arr.length - 1;
  121. for (; end >= 0; end--) {
  122. if (arr[end] !== '') break;
  123. }
  124. if (start > end) return [];
  125. return arr.slice(start, end - start + 1);
  126. }
  127. var fromParts = trim(from.split('/'));
  128. var toParts = trim(to.split('/'));
  129. var length = Math.min(fromParts.length, toParts.length);
  130. var samePartsLength = length;
  131. for (var i = 0; i < length; i++) {
  132. if (fromParts[i] !== toParts[i]) {
  133. samePartsLength = i;
  134. break;
  135. }
  136. }
  137. var outputParts = [];
  138. for (var i = samePartsLength; i < fromParts.length; i++) {
  139. outputParts.push('..');
  140. }
  141. outputParts = outputParts.concat(toParts.slice(samePartsLength));
  142. return outputParts.join('/');
  143. };
  144. exports.sep = '/';
  145. exports.delimiter = ':';
  146. exports.dirname = function(path) {
  147. var result = splitPath(path),
  148. root = result[0],
  149. dir = result[1];
  150. if (!root && !dir) {
  151. // No dirname whatsoever
  152. return '.';
  153. }
  154. if (dir) {
  155. // It has a dirname, strip trailing slash
  156. dir = dir.substr(0, dir.length - 1);
  157. }
  158. return root + dir;
  159. };
  160. exports.basename = function(path, ext) {
  161. var f = splitPath(path)[2];
  162. // TODO: make this comparison case-insensitive on windows?
  163. if (ext && f.substr(-1 * ext.length) === ext) {
  164. f = f.substr(0, f.length - ext.length);
  165. }
  166. return f;
  167. };
  168. exports.extname = function(path) {
  169. return splitPath(path)[3];
  170. };
  171. function filter (xs, f) {
  172. if (xs.filter) return xs.filter(f);
  173. var res = [];
  174. for (var i = 0; i < xs.length; i++) {
  175. if (f(xs[i], i, xs)) res.push(xs[i]);
  176. }
  177. return res;
  178. }
  179. // String.prototype.substr - negative index don't work in IE8
  180. var substr = 'ab'.substr(-1) === 'b'
  181. ? function (str, start, len) { return str.substr(start, len) }
  182. : function (str, start, len) {
  183. if (start < 0) start = str.length + start;
  184. return str.substr(start, len);
  185. }
  186. ;