path.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. var process = process || {};
  2. (function () {
  3. "use strict";
  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. var isWindows = process.platform === 'win32';
  25. // resolves . and .. elements in a path array with directory names there
  26. // must be no slashes, empty elements, or device names (c:\) in the array
  27. // (so also no leading and trailing slashes - it does not distinguish
  28. // relative and absolute paths)
  29. function normalizeArray(parts, allowAboveRoot) {
  30. // if the path tries to go above the root, `up` ends up > 0
  31. var up = 0;
  32. for (var i = parts.length; i >= 0; i--) {
  33. var last = parts[i];
  34. if (last == '.') {
  35. parts.splice(i, 1);
  36. } else if (last === '..') {
  37. parts.splice(i, 1);
  38. up++;
  39. } else if (up) {
  40. parts.splice(i, 1);
  41. up--;
  42. }
  43. }
  44. // if the path is allowed to go above the root, restore leading ..s
  45. if (allowAboveRoot) {
  46. for (; up--; up) {
  47. parts.unshift('..');
  48. }
  49. }
  50. return parts;
  51. }
  52. if (isWindows) {
  53. // Regex to split a filename into [*, dir, basename, ext]
  54. // windows version
  55. var splitPathRe = /^(.+(?:[\\\/](?!$)|:)|[\\\/])?((?:.+?)?(\.[^.]*)?)$/;
  56. // Regex to split a windows path into three parts: [*, device, slash,
  57. // tail] windows-only
  58. var splitDeviceRe =
  59. /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?(.*?)$/;
  60. // path.resolve([from ...], to)
  61. // windows version
  62. exports.resolve = function() {
  63. var resolvedDevice = '',
  64. resolvedTail = '',
  65. resolvedAbsolute = false;
  66. for (var i = arguments.length; i >= -1; i--) {
  67. var path = (i >= 0)
  68. ? arguments[i]
  69. : process.cwd();
  70. // Skip empty and invalid entries
  71. if (typeof path !== 'string' || !path) {
  72. continue;
  73. }
  74. var result = splitDeviceRe.exec(path),
  75. device = result[1] || '',
  76. isUnc = device && device.charAt(1) !== ':',
  77. isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute
  78. tail = result[3];
  79. if (device &&
  80. resolvedDevice &&
  81. device.toLowerCase() !== resolvedDevice.toLowerCase()) {
  82. // This path points to another device so it is not applicable
  83. continue;
  84. }
  85. if (!resolvedDevice) {
  86. resolvedDevice = device;
  87. }
  88. if (!resolvedAbsolute) {
  89. resolvedTail = tail + '\\' + resolvedTail;
  90. resolvedAbsolute = isAbsolute;
  91. }
  92. if (resolvedDevice && resolvedAbsolute) {
  93. break;
  94. }
  95. }
  96. if (!resolvedAbsolute && resolvedDevice) {
  97. // If we still don't have an absolute path,
  98. // prepend the current path for the device found.
  99. // TODO
  100. // Windows stores the current directories for 'other' drives
  101. // as hidden environment variables like =C:=c:\windows (literally)
  102. // var deviceCwd = os.getCwdForDrive(resolvedDevice);
  103. var deviceCwd = '';
  104. // If there is no cwd set for the drive, it is at root
  105. resolvedTail = deviceCwd + '\\' + resolvedTail;
  106. resolvedAbsolute = true;
  107. }
  108. // Replace slashes (in UNC share name) by backslashes
  109. resolvedDevice = resolvedDevice.replace(/\//g, '\\');
  110. // At this point the path should be resolved to a full absolute path,
  111. // but handle relative paths to be safe (might happen when process.cwd()
  112. // fails)
  113. // Normalize the tail path
  114. function f(p) {
  115. return !!p;
  116. }
  117. resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),
  118. !resolvedAbsolute).join('\\');
  119. return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
  120. '.';
  121. };
  122. // windows version
  123. exports.normalize = function(path) {
  124. var result = splitDeviceRe.exec(path),
  125. device = result[1] || '',
  126. isUnc = device && device.charAt(1) !== ':',
  127. isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute
  128. tail = result[3],
  129. trailingSlash = /[\\\/]$/.test(tail);
  130. // Normalize the tail path
  131. tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
  132. return !!p;
  133. }), !isAbsolute).join('\\');
  134. if (!tail && !isAbsolute) {
  135. tail = '.';
  136. }
  137. if (tail && trailingSlash) {
  138. tail += '\\';
  139. }
  140. return device + (isAbsolute ? '\\' : '') + tail;
  141. };
  142. // windows version
  143. exports.join = function() {
  144. function f(p) {
  145. return p && typeof p === 'string';
  146. }
  147. var paths = Array.prototype.slice.call(arguments, 0).filter(f);
  148. var joined = paths.join('\\');
  149. // Make sure that the joined path doesn't start with two slashes
  150. // - it will be mistaken for an unc path by normalize() -
  151. // unless the paths[0] also starts with two slashes
  152. if (/^[\\\/]{2}/.test(joined) && !/^[\\\/]{2}/.test(paths[0])) {
  153. joined = joined.slice(1);
  154. }
  155. return exports.normalize(joined);
  156. };
  157. } else /* posix */ {
  158. // Regex to split a filename into [*, dir, basename, ext]
  159. // posix version
  160. var splitPathRe = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/;
  161. // path.resolve([from ...], to)
  162. // posix version
  163. exports.resolve = function() {
  164. var resolvedPath = '',
  165. resolvedAbsolute = false;
  166. for (var i = arguments.length; i >= -1 && !resolvedAbsolute; i--) {
  167. var path = (i >= 0)
  168. ? arguments[i]
  169. : process.cwd();
  170. // Skip empty and invalid entries
  171. if (typeof path !== 'string' || !path) {
  172. continue;
  173. }
  174. resolvedPath = path + '/' + resolvedPath;
  175. resolvedAbsolute = path.charAt(0) === '/';
  176. }
  177. // At this point the path should be resolved to a full absolute path, but
  178. // handle relative paths to be safe (might happen when process.cwd() fails)
  179. // Normalize the path
  180. resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
  181. return !!p;
  182. }), !resolvedAbsolute).join('/');
  183. return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
  184. };
  185. // path.normalize(path)
  186. // posix version
  187. exports.normalize = function(path) {
  188. var isAbsolute = path.charAt(0) === '/',
  189. trailingSlash = path.slice(-1) === '/';
  190. // Normalize the path
  191. path = normalizeArray(path.split('/').filter(function(p) {
  192. return !!p;
  193. }), !isAbsolute).join('/');
  194. if (!path && !isAbsolute) {
  195. path = '.';
  196. }
  197. if (path && trailingSlash) {
  198. path += '/';
  199. }
  200. return (isAbsolute ? '/' : '') + path;
  201. };
  202. // posix version
  203. exports.join = function() {
  204. var paths = Array.prototype.slice.call(arguments, 0);
  205. return exports.normalize(paths.filter(function(p, index) {
  206. return p && typeof p === 'string';
  207. }).join('/'));
  208. };
  209. }
  210. exports.dirname = function(path) {
  211. var dir = splitPathRe.exec(path)[1] || '';
  212. if (!dir) {
  213. // No dirname
  214. return '.';
  215. } else if (dir.length === 1 ||
  216. (isWindows && dir.length <= 3 && dir.charAt(1) === ':')) {
  217. // It is just a slash or a drive letter with a slash
  218. return dir;
  219. } else {
  220. // It is a full dirname, strip trailing slash
  221. return dir.substring(0, dir.length - 1);
  222. }
  223. };
  224. exports.basename = function(path, ext) {
  225. var f = splitPathRe.exec(path)[2] || '';
  226. // TODO: make this comparison case-insensitive on windows?
  227. if (ext && f.substr(-1 * ext.length) === ext) {
  228. f = f.substr(0, f.length - ext.length);
  229. }
  230. return f;
  231. };
  232. exports.extname = function(path) {
  233. return splitPathRe.exec(path)[3] || '';
  234. };
  235. exports.exists = function(path, callback) {
  236. process.binding('fs').stat(path, function(err, stats) {
  237. if (callback) callback(err ? false : true);
  238. });
  239. };
  240. exports.existsSync = function(path) {
  241. try {
  242. process.binding('fs').stat(path);
  243. return true;
  244. } catch (e) {
  245. return false;
  246. }
  247. };
  248. }());