Skip to content

Commit

Permalink
feat: resolve only throw error when rely on cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
DuanPengfei committed Jun 22, 2017
1 parent acfdb47 commit 8d604e1
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 149 deletions.
80 changes: 74 additions & 6 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ const win32 = {
path = arguments[i];
} else if (!resolvedDevice) {
path = process.cwd();

// If you use the current working directory,
// it is necessary to check whether the current platform support.
assertWindowsPlatform(process.platform);
} else {
// Windows has the concept of drive-specific current working
// directories. If we've resolved a drive letter but not yet an
Expand All @@ -213,11 +217,14 @@ const win32 = {
path.slice(0, 3).toLowerCase() !==
resolvedDevice.toLowerCase() + '\\') {
path = resolvedDevice + '\\';
} else {
// If you use the current working directory,
// it is necessary to check whether the current platform support.
assertWindowsPlatform(process.platform);
}
}

assertPath(path);
assertWindowsPlatform(process.platform);

// Skip empty entries
if (path.length === 0) {
Expand Down Expand Up @@ -575,8 +582,38 @@ const win32 = {
if (from === to)
return '';

var fromOrig = win32.resolve(from);
var toOrig = win32.resolve(to);
var fromOrig;
var toOrig;
var isFromAbsolute = true;
var isToAbsolute = true;

try {
fromOrig = win32.resolve(from);
} catch (err) {
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
isFromAbsolute = false;
}
try {
toOrig = win32.resolve(to);
} catch (err) {
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
isToAbsolute = false;
}

if (process.platform !== 'win32') {
if (!isFromAbsolute && !isToAbsolute) {
from = 'c:\\fakepath\\' + from;
to = 'c:\\fakepath\\' + to;
fromOrig = win32.resolve(from);
toOrig = win32.resolve(to);
} else if (isFromAbsolute && !isToAbsolute) {
to = from + '\\' + to;
toOrig = win32.resolve(to);
} else if (!isFromAbsolute && isToAbsolute) {
from = to + '\\' + from;
fromOrig = win32.resolve(from);
}
}

if (fromOrig === toOrig)
return '';
Expand Down Expand Up @@ -1177,7 +1214,6 @@ const posix = {
}

assertPath(path);
assertPosixPlatform(process.platform);

// Skip empty entries
if (path.length === 0) {
Expand All @@ -1188,6 +1224,11 @@ const posix = {
resolvedAbsolute = path.charCodeAt(0) === 47/*/*/;
}

// If you use the current working directory,
// it is necessary to check whether the current platform support.
if (cwd)
assertPosixPlatform(process.platform);

// 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)

Expand Down Expand Up @@ -1263,8 +1304,35 @@ const posix = {
if (from === to)
return '';

from = posix.resolve(from);
to = posix.resolve(to);
var isFromAbsolute = true;
var isToAbsolute = true;
try {
from = posix.resolve(from);
} catch (err) {
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
isFromAbsolute = false;
}
try {
to = posix.resolve(to);
} catch (err) {
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
isToAbsolute = false;
}

if (process.platform === 'win32') {
if (!isFromAbsolute && !isToAbsolute) {
from = '/fakepath/' + from;
to = '/fakepath/' + to;
from = posix.resolve(from);
to = posix.resolve(to);
} else if (isFromAbsolute && !isToAbsolute) {
to = from + '/' + to;
to = posix.resolve(to);
} else if (!isFromAbsolute && isToAbsolute) {
from = to + '/' + from;
from = posix.resolve(from);
}
}

if (from === to)
return '';
Expand Down
Loading

0 comments on commit 8d604e1

Please sign in to comment.