| 1 |
'use strict'; |
| 2 |
|
| 3 |
var isAbsolute = require('./is-absolute') |
| 4 |
, normalizeUNCRoot = require('./_normalize-unc-root') |
| 5 |
, splitDeviceRe = require('./_split-device-re') |
| 6 |
, cwd = require('./_cwd') |
| 7 |
, normalizeArray = require('../_normalize-array'); |
| 8 |
|
| 9 |
module.exports = function () { |
| 10 |
var resolvedDevice = '', resolvedTail = '', resolvedAbsolute = false, i, path |
| 11 |
, result, device, isUnc, isAbs, tail; |
| 12 |
|
| 13 |
for (i = arguments.length - 1; i >= -1; i--) { |
| 14 |
if (i >= 0) { |
| 15 |
path = arguments[i]; |
| 16 |
} else if (!resolvedDevice) { |
| 17 |
path = cwd; |
| 18 |
} else { |
| 19 |
// Windows has the concept of drive-specific current working |
| 20 |
// directories. If we've resolved a drive letter but not yet an |
| 21 |
// absolute path, get cwd for that drive. We're sure the device is not |
| 22 |
// an unc path at this points, because unc paths are always absolute. |
| 23 |
path = process.env['=' + resolvedDevice]; |
| 24 |
// Verify that a drive-local cwd was found and that it actually points |
| 25 |
// to our drive. If not, default to the drive's root. |
| 26 |
if (!path || (path.substr(0, 3).toLowerCase() !== |
| 27 |
resolvedDevice.toLowerCase() + '\\')) { |
| 28 |
path = resolvedDevice + '\\'; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// Skip empty and invalid entries |
| 33 |
path = String(path); |
| 34 |
if (!path) continue; |
| 35 |
|
| 36 |
result = splitDeviceRe.exec(path); |
| 37 |
device = result[1] || ''; |
| 38 |
isUnc = device && device.charAt(1) !== ':'; |
| 39 |
isAbs = isAbsolute(path); |
| 40 |
tail = result[3]; |
| 41 |
|
| 42 |
if (device && resolvedDevice && |
| 43 |
(device.toLowerCase() !== resolvedDevice.toLowerCase())) { |
| 44 |
// This path points to another device so it is not applicable |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
if (!resolvedDevice) { |
| 49 |
resolvedDevice = device; |
| 50 |
} |
| 51 |
if (!resolvedAbsolute) { |
| 52 |
resolvedTail = tail + '\\' + resolvedTail; |
| 53 |
resolvedAbsolute = isAbs; |
| 54 |
} |
| 55 |
|
| 56 |
if (resolvedDevice && resolvedAbsolute) { |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Convert slashes to backslashes when `resolvedDevice` points to an UNC |
| 62 |
// root. Also squash multiple slashes into a single one where appropriate. |
| 63 |
if (isUnc) { |
| 64 |
resolvedDevice = normalizeUNCRoot(resolvedDevice); |
| 65 |
} |
| 66 |
|
| 67 |
// At this point the path should be resolved to a full absolute path, |
| 68 |
// but handle relative paths to be safe (might happen when process.cwd() |
| 69 |
// fails) |
| 70 |
|
| 71 |
// Normalize the tail path |
| 72 |
|
| 73 |
function f(p) { |
| 74 |
return !!p; |
| 75 |
} |
| 76 |
|
| 77 |
resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f), |
| 78 |
!resolvedAbsolute).join('\\'); |
| 79 |
|
| 80 |
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || |
| 81 |
'.'; |
| 82 |
}; |