Created
September 8, 2017 23:01
-
-
Save hisasann/2c258d2ccec463a100d571b9cca28c7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const log = require('electron-log-rotate'); | |
module.exports = { | |
// path.resolve([from ...], to) | |
resolve: function resolve() { | |
var resolvedDevice = ''; | |
var resolvedTail = ''; | |
var resolvedAbsolute = false; | |
for (var i = arguments.length - 1; i >= -1; i--) { | |
var path; | |
if (i >= 0) { | |
path = arguments[i]; | |
} else if (!resolvedDevice) { | |
log.log('!resolvedDevice: ' + process.cwd()); | |
path = process.cwd(); | |
} else { | |
// Windows has the concept of drive-specific current working | |
// directories. If we've resolved a drive letter but not yet an | |
// absolute path, get cwd for that drive, or the process cwd if | |
// the drive cwd is not available. We're sure the device is not | |
// a UNC path at this points, because UNC paths are always absolute. | |
path = process.env['=' + resolvedDevice] || process.cwd(); | |
// Verify that a cwd was found and that it actually points | |
// to our drive. If not, default to the drive's root. | |
if (path === undefined || | |
path.slice(0, 3).toLowerCase() !== | |
resolvedDevice.toLowerCase() + '\\') { | |
path = resolvedDevice + '\\'; | |
} | |
} | |
assertPath(path); | |
// Skip empty entries | |
if (path.length === 0) { | |
continue; | |
} | |
var len = path.length; | |
var rootEnd = 0; | |
var code = path.charCodeAt(0); | |
var device = ''; | |
var isAbsolute = false; | |
log.log('path in win32: ' + path); | |
// Try to match a root | |
if (len > 1) { | |
if (code === 47/*/*/ || code === 92/*\*/) { | |
// Possible UNC root | |
// If we started with a separator, we know we at least have an | |
// absolute path of some kind (UNC or otherwise) | |
isAbsolute = true; | |
code = path.charCodeAt(1); | |
if (code === 47/*/*/ || code === 92/*\*/) { | |
// Matched double path separator at beginning | |
var j = 2; | |
var last = j; | |
// Match 1 or more non-path separators | |
for (; j < len; ++j) { | |
code = path.charCodeAt(j); | |
if (code === 47/*/*/ || code === 92/*\*/) | |
break; | |
} | |
if (j < len && j !== last) { | |
const firstPart = path.slice(last, j); | |
// Matched! | |
last = j; | |
// Match 1 or more path separators | |
for (; j < len; ++j) { | |
code = path.charCodeAt(j); | |
if (code !== 47/*/*/ && code !== 92/*\*/) | |
break; | |
} | |
if (j < len && j !== last) { | |
// Matched! | |
last = j; | |
// Match 1 or more non-path separators | |
for (; j < len; ++j) { | |
code = path.charCodeAt(j); | |
if (code === 47/*/*/ || code === 92/*\*/) | |
break; | |
} | |
if (j === len) { | |
// We matched a UNC root only | |
device = '\\\\' + firstPart + '\\' + path.slice(last); | |
rootEnd = j; | |
} else if (j !== last) { | |
// We matched a UNC root with leftovers | |
device = '\\\\' + firstPart + '\\' + path.slice(last, j); | |
rootEnd = j; | |
} | |
} | |
} | |
} else { | |
rootEnd = 1; | |
} | |
} else if ((code >= 65/*A*/ && code <= 90/*Z*/) || | |
(code >= 97/*a*/ && code <= 122/*z*/)) { | |
// Possible device root | |
code = path.charCodeAt(1); | |
if (path.charCodeAt(1) === 58/*:*/) { | |
device = path.slice(0, 2); | |
rootEnd = 2; | |
if (len > 2) { | |
code = path.charCodeAt(2); | |
if (code === 47/*/*/ || code === 92/*\*/) { | |
// Treat separator following drive name as an absolute path | |
// indicator | |
isAbsolute = true; | |
rootEnd = 3; | |
} | |
} | |
} | |
} | |
} else if (code === 47/*/*/ || code === 92/*\*/) { | |
// `path` contains just a path separator | |
rootEnd = 1; | |
isAbsolute = true; | |
} | |
if (device.length > 0 && | |
resolvedDevice.length > 0 && | |
device.toLowerCase() !== resolvedDevice.toLowerCase()) { | |
// This path points to another device so it is not applicable | |
continue; | |
} | |
if (resolvedDevice.length === 0 && device.length > 0) { | |
resolvedDevice = device; | |
} | |
if (!resolvedAbsolute) { | |
resolvedTail = path.slice(rootEnd) + '\\' + resolvedTail; | |
resolvedAbsolute = isAbsolute; | |
} | |
if (resolvedDevice.length > 0 && resolvedAbsolute) { | |
break; | |
} | |
} | |
// At this point the path should be resolved to a full absolute path, | |
// but handle relative paths to be safe (might happen when process.cwd() | |
// fails) | |
// Normalize the tail path | |
resolvedTail = normalizeStringWin32(resolvedTail, !resolvedAbsolute); | |
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || | |
'.'; | |
} | |
}; | |
function assertPath(path) { | |
if (typeof path !== 'string') { | |
throw new Error('ERR_INVALID_ARG_TYPE'); | |
} | |
} | |
// Resolves . and .. elements in a path with directory names | |
function normalizeStringWin32(path, allowAboveRoot) { | |
var res = ''; | |
var lastSlash = -1; | |
var dots = 0; | |
var code; | |
for (var i = 0; i <= path.length; ++i) { | |
if (i < path.length) | |
code = path.charCodeAt(i); | |
else if (code === 47/*/*/ || code === 92/*\*/) | |
break; | |
else | |
code = 47/*/*/; | |
if (code === 47/*/*/ || code === 92/*\*/) { | |
if (lastSlash === i - 1 || dots === 1) { | |
// NOOP | |
} else if (lastSlash !== i - 1 && dots === 2) { | |
if (res.length < 2 || | |
res.charCodeAt(res.length - 1) !== 46/*.*/ || | |
res.charCodeAt(res.length - 2) !== 46/*.*/) { | |
if (res.length > 2) { | |
const start = res.length - 1; | |
var j = start; | |
for (; j >= 0; --j) { | |
if (res.charCodeAt(j) === 92/*\*/) | |
break; | |
} | |
if (j !== start) { | |
if (j === -1) | |
res = ''; | |
else | |
res = res.slice(0, j); | |
lastSlash = i; | |
dots = 0; | |
continue; | |
} | |
} else if (res.length === 2 || res.length === 1) { | |
res = ''; | |
lastSlash = i; | |
dots = 0; | |
continue; | |
} | |
} | |
if (allowAboveRoot) { | |
if (res.length > 0) | |
res += '\\..'; | |
else | |
res = '..'; | |
} | |
} else { | |
if (res.length > 0) | |
res += '\\' + path.slice(lastSlash + 1, i); | |
else | |
res = path.slice(lastSlash + 1, i); | |
} | |
lastSlash = i; | |
dots = 0; | |
} else if (code === 46/*.*/ && dots !== -1) { | |
++dots; | |
} else { | |
dots = -1; | |
} | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment