23
23
24
24
const {
25
25
FunctionPrototypeBind,
26
- RegExp,
27
26
StringPrototypeCharCodeAt,
28
27
StringPrototypeIndexOf,
29
28
StringPrototypeLastIndexOf,
@@ -48,6 +47,8 @@ const {
48
47
validateString,
49
48
} = require ( 'internal/validators' ) ;
50
49
50
+ const platformIsWin32 = ( process . platform === 'win32' ) ;
51
+
51
52
function isPathSeparator ( code ) {
52
53
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH ;
53
54
}
@@ -1011,24 +1012,29 @@ const win32 = {
1011
1012
posix : null
1012
1013
} ;
1013
1014
1015
+ const posixCwd = ( ( ) => {
1016
+ if ( platformIsWin32 ) {
1017
+ // Converts Windows' backslash path separators to POSIX forward slashes
1018
+ // and truncates any drive indicator
1019
+ const regexp = / \\ / g;
1020
+ return ( ) => {
1021
+ const cwd = StringPrototypeReplace ( process . cwd ( ) , regexp , '/' ) ;
1022
+ return StringPrototypeSlice ( cwd , StringPrototypeIndexOf ( cwd , '/' ) ) ;
1023
+ } ;
1024
+ }
1025
+
1026
+ // We're already on POSIX, no need for any transformations
1027
+ return ( ) => process . cwd ( ) ;
1028
+ } ) ( ) ;
1029
+
1014
1030
const posix = {
1015
1031
// path.resolve([from ...], to)
1016
1032
resolve ( ...args ) {
1017
1033
let resolvedPath = '' ;
1018
1034
let resolvedAbsolute = false ;
1019
1035
1020
1036
for ( let i = args . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
1021
- let path ;
1022
- if ( i >= 0 ) {
1023
- path = args [ i ] ;
1024
- } else {
1025
- const _ = StringPrototypeReplace (
1026
- process . cwd ( ) ,
1027
- new RegExp ( `\\${ module . exports . sep } ` , 'g' ) ,
1028
- posix . sep
1029
- ) ;
1030
- path = StringPrototypeSlice ( _ , StringPrototypeIndexOf ( _ , posix . sep ) ) ;
1031
- }
1037
+ const path = i >= 0 ? args [ i ] : posixCwd ( ) ;
1032
1038
1033
1039
validateString ( path , 'path' ) ;
1034
1040
@@ -1431,4 +1437,4 @@ posix.posix = win32.posix = posix;
1431
1437
win32 . _makeLong = win32 . toNamespacedPath ;
1432
1438
posix . _makeLong = posix . toNamespacedPath ;
1433
1439
1434
- module . exports = process . platform === 'win32' ? win32 : posix ;
1440
+ module . exports = platformIsWin32 ? win32 : posix ;
0 commit comments