|
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 |
|
22 // Adapted version of: |
|
23 // https://github.com/joyent/node/blob/v0.11.3/lib/path.js |
|
24 |
|
25 // Shim process global from node. |
|
26 var process = Object.create(require('../system')); |
|
27 process.cwd = process.pathFor.bind(process, 'CurProcD'); |
|
28 |
|
29 // Update original check in node `process.platform === 'win32'` since in SDK it's `winnt`. |
|
30 var isWindows = process.platform.indexOf('win') === 0; |
|
31 |
|
32 |
|
33 |
|
34 // resolves . and .. elements in a path array with directory names there |
|
35 // must be no slashes, empty elements, or device names (c:\) in the array |
|
36 // (so also no leading and trailing slashes - it does not distinguish |
|
37 // relative and absolute paths) |
|
38 function normalizeArray(parts, allowAboveRoot) { |
|
39 // if the path tries to go above the root, `up` ends up > 0 |
|
40 var up = 0; |
|
41 for (var i = parts.length - 1; i >= 0; i--) { |
|
42 var last = parts[i]; |
|
43 if (last === '.') { |
|
44 parts.splice(i, 1); |
|
45 } else if (last === '..') { |
|
46 parts.splice(i, 1); |
|
47 up++; |
|
48 } else if (up) { |
|
49 parts.splice(i, 1); |
|
50 up--; |
|
51 } |
|
52 } |
|
53 |
|
54 // if the path is allowed to go above the root, restore leading ..s |
|
55 if (allowAboveRoot) { |
|
56 for (; up--; up) { |
|
57 parts.unshift('..'); |
|
58 } |
|
59 } |
|
60 |
|
61 return parts; |
|
62 } |
|
63 |
|
64 |
|
65 if (isWindows) { |
|
66 // Regex to split a windows path into three parts: [*, device, slash, |
|
67 // tail] windows-only |
|
68 var splitDeviceRe = |
|
69 /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/; |
|
70 |
|
71 // Regex to split the tail part of the above into [*, dir, basename, ext] |
|
72 var splitTailRe = |
|
73 /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/; |
|
74 |
|
75 // Function to split a filename into [root, dir, basename, ext] |
|
76 // windows version |
|
77 var splitPath = function(filename) { |
|
78 // Separate device+slash from tail |
|
79 var result = splitDeviceRe.exec(filename), |
|
80 device = (result[1] || '') + (result[2] || ''), |
|
81 tail = result[3] || ''; |
|
82 // Split the tail into dir, basename and extension |
|
83 var result2 = splitTailRe.exec(tail), |
|
84 dir = result2[1], |
|
85 basename = result2[2], |
|
86 ext = result2[3]; |
|
87 return [device, dir, basename, ext]; |
|
88 }; |
|
89 |
|
90 var normalizeUNCRoot = function(device) { |
|
91 return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); |
|
92 }; |
|
93 |
|
94 // path.resolve([from ...], to) |
|
95 // windows version |
|
96 exports.resolve = function() { |
|
97 var resolvedDevice = '', |
|
98 resolvedTail = '', |
|
99 resolvedAbsolute = false; |
|
100 |
|
101 for (var i = arguments.length - 1; i >= -1; i--) { |
|
102 var path; |
|
103 if (i >= 0) { |
|
104 path = arguments[i]; |
|
105 } else if (!resolvedDevice) { |
|
106 path = process.cwd(); |
|
107 } else { |
|
108 // Windows has the concept of drive-specific current working |
|
109 // directories. If we've resolved a drive letter but not yet an |
|
110 // absolute path, get cwd for that drive. We're sure the device is not |
|
111 // an unc path at this points, because unc paths are always absolute. |
|
112 path = process.env['=' + resolvedDevice]; |
|
113 // Verify that a drive-local cwd was found and that it actually points |
|
114 // to our drive. If not, default to the drive's root. |
|
115 if (!path || path.substr(0, 3).toLowerCase() !== |
|
116 resolvedDevice.toLowerCase() + '\\') { |
|
117 path = resolvedDevice + '\\'; |
|
118 } |
|
119 } |
|
120 |
|
121 // Skip empty and invalid entries |
|
122 if (typeof path !== 'string') { |
|
123 throw new TypeError('Arguments to path.resolve must be strings'); |
|
124 } else if (!path) { |
|
125 continue; |
|
126 } |
|
127 |
|
128 var result = splitDeviceRe.exec(path), |
|
129 device = result[1] || '', |
|
130 isUnc = device && device.charAt(1) !== ':', |
|
131 isAbsolute = exports.isAbsolute(path), |
|
132 tail = result[3]; |
|
133 |
|
134 if (device && |
|
135 resolvedDevice && |
|
136 device.toLowerCase() !== resolvedDevice.toLowerCase()) { |
|
137 // This path points to another device so it is not applicable |
|
138 continue; |
|
139 } |
|
140 |
|
141 if (!resolvedDevice) { |
|
142 resolvedDevice = device; |
|
143 } |
|
144 if (!resolvedAbsolute) { |
|
145 resolvedTail = tail + '\\' + resolvedTail; |
|
146 resolvedAbsolute = isAbsolute; |
|
147 } |
|
148 |
|
149 if (resolvedDevice && resolvedAbsolute) { |
|
150 break; |
|
151 } |
|
152 } |
|
153 |
|
154 // Convert slashes to backslashes when `resolvedDevice` points to an UNC |
|
155 // root. Also squash multiple slashes into a single one where appropriate. |
|
156 if (isUnc) { |
|
157 resolvedDevice = normalizeUNCRoot(resolvedDevice); |
|
158 } |
|
159 |
|
160 // At this point the path should be resolved to a full absolute path, |
|
161 // but handle relative paths to be safe (might happen when process.cwd() |
|
162 // fails) |
|
163 |
|
164 // Normalize the tail path |
|
165 |
|
166 function f(p) { |
|
167 return !!p; |
|
168 } |
|
169 |
|
170 resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f), |
|
171 !resolvedAbsolute).join('\\'); |
|
172 |
|
173 return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || |
|
174 '.'; |
|
175 }; |
|
176 |
|
177 // windows version |
|
178 exports.normalize = function(path) { |
|
179 var result = splitDeviceRe.exec(path), |
|
180 device = result[1] || '', |
|
181 isUnc = device && device.charAt(1) !== ':', |
|
182 isAbsolute = exports.isAbsolute(path), |
|
183 tail = result[3], |
|
184 trailingSlash = /[\\\/]$/.test(tail); |
|
185 |
|
186 // If device is a drive letter, we'll normalize to lower case. |
|
187 if (device && device.charAt(1) === ':') { |
|
188 device = device[0].toLowerCase() + device.substr(1); |
|
189 } |
|
190 |
|
191 // Normalize the tail path |
|
192 tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) { |
|
193 return !!p; |
|
194 }), !isAbsolute).join('\\'); |
|
195 |
|
196 if (!tail && !isAbsolute) { |
|
197 tail = '.'; |
|
198 } |
|
199 if (tail && trailingSlash) { |
|
200 tail += '\\'; |
|
201 } |
|
202 |
|
203 // Convert slashes to backslashes when `device` points to an UNC root. |
|
204 // Also squash multiple slashes into a single one where appropriate. |
|
205 if (isUnc) { |
|
206 device = normalizeUNCRoot(device); |
|
207 } |
|
208 |
|
209 return device + (isAbsolute ? '\\' : '') + tail; |
|
210 }; |
|
211 |
|
212 // windows version |
|
213 exports.isAbsolute = function(path) { |
|
214 var result = splitDeviceRe.exec(path), |
|
215 device = result[1] || '', |
|
216 isUnc = device && device.charAt(1) !== ':'; |
|
217 // UNC paths are always absolute |
|
218 return !!result[2] || isUnc; |
|
219 }; |
|
220 |
|
221 // windows version |
|
222 exports.join = function() { |
|
223 function f(p) { |
|
224 if (typeof p !== 'string') { |
|
225 throw new TypeError('Arguments to path.join must be strings'); |
|
226 } |
|
227 return p; |
|
228 } |
|
229 |
|
230 var paths = Array.prototype.filter.call(arguments, f); |
|
231 var joined = paths.join('\\'); |
|
232 |
|
233 // Make sure that the joined path doesn't start with two slashes, because |
|
234 // normalize() will mistake it for an UNC path then. |
|
235 // |
|
236 // This step is skipped when it is very clear that the user actually |
|
237 // intended to point at an UNC path. This is assumed when the first |
|
238 // non-empty string arguments starts with exactly two slashes followed by |
|
239 // at least one more non-slash character. |
|
240 // |
|
241 // Note that for normalize() to treat a path as an UNC path it needs to |
|
242 // have at least 2 components, so we don't filter for that here. |
|
243 // This means that the user can use join to construct UNC paths from |
|
244 // a server name and a share name; for example: |
|
245 // path.join('//server', 'share') -> '\\\\server\\share\') |
|
246 if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) { |
|
247 joined = joined.replace(/^[\\\/]{2,}/, '\\'); |
|
248 } |
|
249 |
|
250 return exports.normalize(joined); |
|
251 }; |
|
252 |
|
253 // path.relative(from, to) |
|
254 // it will solve the relative path from 'from' to 'to', for instance: |
|
255 // from = 'C:\\orandea\\test\\aaa' |
|
256 // to = 'C:\\orandea\\impl\\bbb' |
|
257 // The output of the function should be: '..\\..\\impl\\bbb' |
|
258 // windows version |
|
259 exports.relative = function(from, to) { |
|
260 from = exports.resolve(from); |
|
261 to = exports.resolve(to); |
|
262 |
|
263 // windows is not case sensitive |
|
264 var lowerFrom = from.toLowerCase(); |
|
265 var lowerTo = to.toLowerCase(); |
|
266 |
|
267 function trim(arr) { |
|
268 var start = 0; |
|
269 for (; start < arr.length; start++) { |
|
270 if (arr[start] !== '') break; |
|
271 } |
|
272 |
|
273 var end = arr.length - 1; |
|
274 for (; end >= 0; end--) { |
|
275 if (arr[end] !== '') break; |
|
276 } |
|
277 |
|
278 if (start > end) return []; |
|
279 return arr.slice(start, end - start + 1); |
|
280 } |
|
281 |
|
282 var toParts = trim(to.split('\\')); |
|
283 |
|
284 var lowerFromParts = trim(lowerFrom.split('\\')); |
|
285 var lowerToParts = trim(lowerTo.split('\\')); |
|
286 |
|
287 var length = Math.min(lowerFromParts.length, lowerToParts.length); |
|
288 var samePartsLength = length; |
|
289 for (var i = 0; i < length; i++) { |
|
290 if (lowerFromParts[i] !== lowerToParts[i]) { |
|
291 samePartsLength = i; |
|
292 break; |
|
293 } |
|
294 } |
|
295 |
|
296 if (samePartsLength == 0) { |
|
297 return to; |
|
298 } |
|
299 |
|
300 var outputParts = []; |
|
301 for (var i = samePartsLength; i < lowerFromParts.length; i++) { |
|
302 outputParts.push('..'); |
|
303 } |
|
304 |
|
305 outputParts = outputParts.concat(toParts.slice(samePartsLength)); |
|
306 |
|
307 return outputParts.join('\\'); |
|
308 }; |
|
309 |
|
310 exports.sep = '\\'; |
|
311 exports.delimiter = ';'; |
|
312 |
|
313 } else /* posix */ { |
|
314 |
|
315 // Split a filename into [root, dir, basename, ext], unix version |
|
316 // 'root' is just a slash, or nothing. |
|
317 var splitPathRe = |
|
318 /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; |
|
319 var splitPath = function(filename) { |
|
320 return splitPathRe.exec(filename).slice(1); |
|
321 }; |
|
322 |
|
323 // path.resolve([from ...], to) |
|
324 // posix version |
|
325 exports.resolve = function() { |
|
326 var resolvedPath = '', |
|
327 resolvedAbsolute = false; |
|
328 |
|
329 for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { |
|
330 var path = (i >= 0) ? arguments[i] : process.cwd(); |
|
331 |
|
332 // Skip empty and invalid entries |
|
333 if (typeof path !== 'string') { |
|
334 throw new TypeError('Arguments to path.resolve must be strings'); |
|
335 } else if (!path) { |
|
336 continue; |
|
337 } |
|
338 |
|
339 resolvedPath = path + '/' + resolvedPath; |
|
340 resolvedAbsolute = path.charAt(0) === '/'; |
|
341 } |
|
342 |
|
343 // At this point the path should be resolved to a full absolute path, but |
|
344 // handle relative paths to be safe (might happen when process.cwd() fails) |
|
345 |
|
346 // Normalize the path |
|
347 resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) { |
|
348 return !!p; |
|
349 }), !resolvedAbsolute).join('/'); |
|
350 |
|
351 return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; |
|
352 }; |
|
353 |
|
354 // path.normalize(path) |
|
355 // posix version |
|
356 exports.normalize = function(path) { |
|
357 var isAbsolute = exports.isAbsolute(path), |
|
358 trailingSlash = path.substr(-1) === '/'; |
|
359 |
|
360 // Normalize the path |
|
361 path = normalizeArray(path.split('/').filter(function(p) { |
|
362 return !!p; |
|
363 }), !isAbsolute).join('/'); |
|
364 |
|
365 if (!path && !isAbsolute) { |
|
366 path = '.'; |
|
367 } |
|
368 if (path && trailingSlash) { |
|
369 path += '/'; |
|
370 } |
|
371 |
|
372 return (isAbsolute ? '/' : '') + path; |
|
373 }; |
|
374 |
|
375 // posix version |
|
376 exports.isAbsolute = function(path) { |
|
377 return path.charAt(0) === '/'; |
|
378 }; |
|
379 |
|
380 // posix version |
|
381 exports.join = function() { |
|
382 var paths = Array.prototype.slice.call(arguments, 0); |
|
383 return exports.normalize(paths.filter(function(p, index) { |
|
384 if (typeof p !== 'string') { |
|
385 throw new TypeError('Arguments to path.join must be strings'); |
|
386 } |
|
387 return p; |
|
388 }).join('/')); |
|
389 }; |
|
390 |
|
391 |
|
392 // path.relative(from, to) |
|
393 // posix version |
|
394 exports.relative = function(from, to) { |
|
395 from = exports.resolve(from).substr(1); |
|
396 to = exports.resolve(to).substr(1); |
|
397 |
|
398 function trim(arr) { |
|
399 var start = 0; |
|
400 for (; start < arr.length; start++) { |
|
401 if (arr[start] !== '') break; |
|
402 } |
|
403 |
|
404 var end = arr.length - 1; |
|
405 for (; end >= 0; end--) { |
|
406 if (arr[end] !== '') break; |
|
407 } |
|
408 |
|
409 if (start > end) return []; |
|
410 return arr.slice(start, end - start + 1); |
|
411 } |
|
412 |
|
413 var fromParts = trim(from.split('/')); |
|
414 var toParts = trim(to.split('/')); |
|
415 |
|
416 var length = Math.min(fromParts.length, toParts.length); |
|
417 var samePartsLength = length; |
|
418 for (var i = 0; i < length; i++) { |
|
419 if (fromParts[i] !== toParts[i]) { |
|
420 samePartsLength = i; |
|
421 break; |
|
422 } |
|
423 } |
|
424 |
|
425 var outputParts = []; |
|
426 for (var i = samePartsLength; i < fromParts.length; i++) { |
|
427 outputParts.push('..'); |
|
428 } |
|
429 |
|
430 outputParts = outputParts.concat(toParts.slice(samePartsLength)); |
|
431 |
|
432 return outputParts.join('/'); |
|
433 }; |
|
434 |
|
435 exports.sep = '/'; |
|
436 exports.delimiter = ':'; |
|
437 } |
|
438 |
|
439 exports.dirname = function(path) { |
|
440 var result = splitPath(path), |
|
441 root = result[0], |
|
442 dir = result[1]; |
|
443 |
|
444 if (!root && !dir) { |
|
445 // No dirname whatsoever |
|
446 return '.'; |
|
447 } |
|
448 |
|
449 if (dir) { |
|
450 // It has a dirname, strip trailing slash |
|
451 dir = dir.substr(0, dir.length - 1); |
|
452 } |
|
453 |
|
454 return root + dir; |
|
455 }; |
|
456 |
|
457 |
|
458 exports.basename = function(path, ext) { |
|
459 var f = splitPath(path)[2]; |
|
460 // TODO: make this comparison case-insensitive on windows? |
|
461 if (ext && f.substr(-1 * ext.length) === ext) { |
|
462 f = f.substr(0, f.length - ext.length); |
|
463 } |
|
464 return f; |
|
465 }; |
|
466 |
|
467 |
|
468 exports.extname = function(path) { |
|
469 return splitPath(path)[3]; |
|
470 }; |
|
471 |
|
472 if (isWindows) { |
|
473 exports._makeLong = function(path) { |
|
474 // Note: this will *probably* throw somewhere. |
|
475 if (typeof path !== 'string') |
|
476 return path; |
|
477 |
|
478 if (!path) { |
|
479 return ''; |
|
480 } |
|
481 |
|
482 var resolvedPath = exports.resolve(path); |
|
483 |
|
484 if (/^[a-zA-Z]\:\\/.test(resolvedPath)) { |
|
485 // path is local filesystem path, which needs to be converted |
|
486 // to long UNC path. |
|
487 return '\\\\?\\' + resolvedPath; |
|
488 } else if (/^\\\\[^?.]/.test(resolvedPath)) { |
|
489 // path is network UNC path, which needs to be converted |
|
490 // to long UNC path. |
|
491 return '\\\\?\\UNC\\' + resolvedPath.substring(2); |
|
492 } |
|
493 |
|
494 return path; |
|
495 }; |
|
496 } else { |
|
497 exports._makeLong = function(path) { |
|
498 return path; |
|
499 }; |
|
500 } |