@@ -18,6 +18,10 @@ const dirent = (path, directory) => {
18
18
} ;
19
19
} ;
20
20
21
+ const normalize = ( path ) => path . replace ( / ^ [ A - Z a - z ] : / u, "" ) . replaceAll ( "\\" , "/" ) ;
22
+
23
+ /* eslint-disable no-param-reassign */
24
+
21
25
class FsVirtual {
22
26
constructor ( files ) {
23
27
@@ -26,13 +30,15 @@ class FsVirtual {
26
30
this . promises = { } ;
27
31
28
32
this . promises . access = ( path ) => {
33
+ path = normalize ( path ) ;
29
34
if ( this . files . has ( path ) ) {
30
35
return Promise . resolve ( ) ;
31
36
}
32
37
return Promise . reject ( new Error ( `fs-virtual:promises.access(${ path } )` ) ) ;
33
38
} ;
34
39
35
40
this . promises . readFile = ( path ) => {
41
+ path = normalize ( path ) ;
36
42
const content = this . files . get ( path ) ;
37
43
if ( content ) {
38
44
return Promise . resolve ( content ) ;
@@ -41,31 +47,36 @@ class FsVirtual {
41
47
} ;
42
48
43
49
this . promises . stat = ( path ) => {
50
+ path = normalize ( path ) ;
44
51
if ( this . files . has ( path ) ) {
45
52
return Promise . resolve ( dirent ( path ) ) ;
46
53
}
47
54
return Promise . reject ( new Error ( `fs-virtual:promises.stat(${ path } )` ) ) ;
48
55
} ;
49
56
50
57
this . promises . writeFile = ( path , data ) => {
58
+ path = normalize ( path ) ;
51
59
this . files . set ( path , data ) ;
52
60
} ;
53
61
54
62
this . access = ( path , mode , callback ) => {
63
+ path = normalize ( path ) ;
55
64
if ( this . files . has ( path ) ) {
56
65
return ( callback || mode ) ( ) ;
57
66
}
58
67
return ( callback || mode ) ( new Error ( `fs-virtual:access(${ path } )` ) ) ;
59
68
} ;
60
69
61
70
this . lstat = ( path , callback ) => {
71
+ path = normalize ( path ) ;
62
72
if ( this . files . has ( path ) ) {
63
73
return callback ( null , dirent ( path , false ) ) ;
64
74
}
65
75
return callback ( null , dirent ( path , true ) ) ;
66
76
} ;
67
77
68
78
this . readdir = ( path , options , callback ) => {
79
+ path = normalize ( path ) ;
69
80
const names = [ ] ;
70
81
for ( const file of this . files . keys ( ) ) {
71
82
if ( file . startsWith ( path ) ) {
@@ -79,6 +90,7 @@ class FsVirtual {
79
90
} ;
80
91
81
92
this . readFile = ( path , options , callback ) => {
93
+ path = normalize ( path ) ;
82
94
const content = this . files . get ( path ) ;
83
95
if ( content ) {
84
96
return callback ( null , content ) ;
0 commit comments