1
- import { dirname , normalize } from 'path' ;
2
- import { fs as mem } from 'memfs' ;
3
1
import { FileSystem } from './FileSystem' ;
4
- // eslint-disable-next-line node/no-unsupported-features/node-builtins
5
- import { Dirent , Stats } from 'fs' ;
6
2
7
3
/**
8
4
* It's an implementation of FileSystem interface which reads from the real file system, but write to the in-memory file system.
9
5
*
10
- * @param caseSensitive
6
+ * @param memFileSystem
11
7
* @param realFileSystem
12
8
*/
13
- function createPassiveFileSystem ( caseSensitive = false , realFileSystem : FileSystem ) : FileSystem {
14
- function normalizePath ( path : string ) : string {
15
- return caseSensitive ? normalize ( path ) : normalize ( path ) . toLowerCase ( ) ;
16
- }
17
-
18
- function memExists ( path : string ) : boolean {
19
- return mem . existsSync ( normalizePath ( path ) ) ;
20
- }
21
-
22
- function memReadStats ( path : string ) : Stats | undefined {
23
- return memExists ( path ) ? mem . statSync ( normalizePath ( path ) ) : undefined ;
24
- }
25
-
26
- function memReadFile ( path : string , encoding ?: string ) : string | undefined {
27
- const stats = memReadStats ( path ) ;
28
-
29
- if ( stats && stats . isFile ( ) ) {
30
- return mem
31
- . readFileSync ( normalizePath ( path ) , { encoding : encoding as BufferEncoding } )
32
- . toString ( ) ;
33
- }
34
- }
35
-
36
- function memReadDir ( path : string ) : Dirent [ ] {
37
- const stats = memReadStats ( path ) ;
38
-
39
- if ( stats && stats . isDirectory ( ) ) {
40
- return mem . readdirSync ( normalizePath ( path ) , { withFileTypes : true } ) as Dirent [ ] ;
41
- }
42
-
43
- return [ ] ;
44
- }
45
-
9
+ function createPassiveFileSystem (
10
+ memFileSystem : FileSystem ,
11
+ realFileSystem : FileSystem
12
+ ) : FileSystem {
46
13
function exists ( path : string ) {
47
- return realFileSystem . exists ( path ) || memExists ( path ) ;
14
+ return realFileSystem . exists ( path ) || memFileSystem . exists ( path ) ;
48
15
}
49
16
50
17
function readFile ( path : string , encoding ?: string ) {
51
18
const fsStats = realFileSystem . readStats ( path ) ;
52
- const memStats = memReadStats ( path ) ;
19
+ const memStats = memFileSystem . readStats ( path ) ;
53
20
54
21
if ( fsStats && memStats ) {
55
22
return fsStats . mtimeMs > memStats . mtimeMs
56
23
? realFileSystem . readFile ( path , encoding )
57
- : memReadFile ( path , encoding ) ;
24
+ : memFileSystem . readFile ( path , encoding ) ;
58
25
} else if ( fsStats ) {
59
26
return realFileSystem . readFile ( path , encoding ) ;
60
27
} else if ( memStats ) {
61
- return memReadFile ( path , encoding ) ;
28
+ return memFileSystem . readFile ( path , encoding ) ;
62
29
}
63
30
}
64
31
65
32
function readDir ( path : string ) {
66
33
const fsDirents = realFileSystem . readDir ( path ) ;
67
- const memDirents = memReadDir ( path ) ;
34
+ const memDirents = memFileSystem . readDir ( path ) ;
68
35
69
36
// merge list of dirents from fs and mem
70
37
return fsDirents
@@ -74,7 +41,7 @@ function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSyst
74
41
75
42
function readStats ( path : string ) {
76
43
const fsStats = realFileSystem . readStats ( path ) ;
77
- const memStats = memReadStats ( path ) ;
44
+ const memStats = memFileSystem . readStats ( path ) ;
78
45
79
46
if ( fsStats && memStats ) {
80
47
return fsStats . mtimeMs > memStats . mtimeMs ? fsStats : memStats ;
@@ -85,31 +52,8 @@ function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSyst
85
52
}
86
53
}
87
54
88
- function createDir ( path : string ) {
89
- mem . mkdirSync ( normalizePath ( path ) , { recursive : true } ) ;
90
- }
91
-
92
- function writeFile ( path : string , data : string ) {
93
- if ( ! memExists ( dirname ( path ) ) ) {
94
- createDir ( dirname ( path ) ) ;
95
- }
96
-
97
- mem . writeFileSync ( normalizePath ( path ) , data ) ;
98
- }
99
-
100
- function deleteFile ( path : string ) {
101
- if ( memExists ( path ) ) {
102
- mem . unlinkSync ( normalizePath ( path ) ) ;
103
- }
104
- }
105
-
106
- function updateTimes ( path : string , atime : Date , mtime : Date ) {
107
- if ( memExists ( path ) ) {
108
- mem . utimesSync ( normalizePath ( path ) , atime , mtime ) ;
109
- }
110
- }
111
-
112
55
return {
56
+ ...memFileSystem ,
113
57
exists ( path : string ) {
114
58
return exists ( realFileSystem . realPath ( path ) ) ;
115
59
} ,
@@ -125,21 +69,6 @@ function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSyst
125
69
realPath ( path : string ) {
126
70
return realFileSystem . realPath ( path ) ;
127
71
} ,
128
- normalizePath ( path : string ) {
129
- return normalizePath ( path ) ;
130
- } ,
131
- writeFile ( path : string , data : string ) {
132
- writeFile ( realFileSystem . realPath ( path ) , data ) ;
133
- } ,
134
- deleteFile ( path : string ) {
135
- deleteFile ( realFileSystem . realPath ( path ) ) ;
136
- } ,
137
- createDir ( path : string ) {
138
- createDir ( realFileSystem . realPath ( path ) ) ;
139
- } ,
140
- updateTimes ( path : string , atime : Date , mtime : Date ) {
141
- updateTimes ( realFileSystem . realPath ( path ) , atime , mtime ) ;
142
- } ,
143
72
clearCache ( ) {
144
73
realFileSystem . clearCache ( ) ;
145
74
} ,
0 commit comments