Skip to content

Commit 8d604e1

Browse files
committed
feat: resolve only throw error when rely on cwd
1 parent acfdb47 commit 8d604e1

File tree

2 files changed

+234
-149
lines changed

2 files changed

+234
-149
lines changed

lib/path.js

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ const win32 = {
199199
path = arguments[i];
200200
} else if (!resolvedDevice) {
201201
path = process.cwd();
202+
203+
// If you use the current working directory,
204+
// it is necessary to check whether the current platform support.
205+
assertWindowsPlatform(process.platform);
202206
} else {
203207
// Windows has the concept of drive-specific current working
204208
// directories. If we've resolved a drive letter but not yet an
@@ -213,11 +217,14 @@ const win32 = {
213217
path.slice(0, 3).toLowerCase() !==
214218
resolvedDevice.toLowerCase() + '\\') {
215219
path = resolvedDevice + '\\';
220+
} else {
221+
// If you use the current working directory,
222+
// it is necessary to check whether the current platform support.
223+
assertWindowsPlatform(process.platform);
216224
}
217225
}
218226

219227
assertPath(path);
220-
assertWindowsPlatform(process.platform);
221228

222229
// Skip empty entries
223230
if (path.length === 0) {
@@ -575,8 +582,38 @@ const win32 = {
575582
if (from === to)
576583
return '';
577584

578-
var fromOrig = win32.resolve(from);
579-
var toOrig = win32.resolve(to);
585+
var fromOrig;
586+
var toOrig;
587+
var isFromAbsolute = true;
588+
var isToAbsolute = true;
589+
590+
try {
591+
fromOrig = win32.resolve(from);
592+
} catch (err) {
593+
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
594+
isFromAbsolute = false;
595+
}
596+
try {
597+
toOrig = win32.resolve(to);
598+
} catch (err) {
599+
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
600+
isToAbsolute = false;
601+
}
602+
603+
if (process.platform !== 'win32') {
604+
if (!isFromAbsolute && !isToAbsolute) {
605+
from = 'c:\\fakepath\\' + from;
606+
to = 'c:\\fakepath\\' + to;
607+
fromOrig = win32.resolve(from);
608+
toOrig = win32.resolve(to);
609+
} else if (isFromAbsolute && !isToAbsolute) {
610+
to = from + '\\' + to;
611+
toOrig = win32.resolve(to);
612+
} else if (!isFromAbsolute && isToAbsolute) {
613+
from = to + '\\' + from;
614+
fromOrig = win32.resolve(from);
615+
}
616+
}
580617

581618
if (fromOrig === toOrig)
582619
return '';
@@ -1177,7 +1214,6 @@ const posix = {
11771214
}
11781215

11791216
assertPath(path);
1180-
assertPosixPlatform(process.platform);
11811217

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

1227+
// If you use the current working directory,
1228+
// it is necessary to check whether the current platform support.
1229+
if (cwd)
1230+
assertPosixPlatform(process.platform);
1231+
11911232
// At this point the path should be resolved to a full absolute path, but
11921233
// handle relative paths to be safe (might happen when process.cwd() fails)
11931234

@@ -1263,8 +1304,35 @@ const posix = {
12631304
if (from === to)
12641305
return '';
12651306

1266-
from = posix.resolve(from);
1267-
to = posix.resolve(to);
1307+
var isFromAbsolute = true;
1308+
var isToAbsolute = true;
1309+
try {
1310+
from = posix.resolve(from);
1311+
} catch (err) {
1312+
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
1313+
isFromAbsolute = false;
1314+
}
1315+
try {
1316+
to = posix.resolve(to);
1317+
} catch (err) {
1318+
if (err.code === 'ERR_UNSUPPORTED_PLATFORM')
1319+
isToAbsolute = false;
1320+
}
1321+
1322+
if (process.platform === 'win32') {
1323+
if (!isFromAbsolute && !isToAbsolute) {
1324+
from = '/fakepath/' + from;
1325+
to = '/fakepath/' + to;
1326+
from = posix.resolve(from);
1327+
to = posix.resolve(to);
1328+
} else if (isFromAbsolute && !isToAbsolute) {
1329+
to = from + '/' + to;
1330+
to = posix.resolve(to);
1331+
} else if (!isFromAbsolute && isToAbsolute) {
1332+
from = to + '/' + from;
1333+
from = posix.resolve(from);
1334+
}
1335+
}
12681336

12691337
if (from === to)
12701338
return '';

0 commit comments

Comments
 (0)