2
2
MIT License http://www.opensource.org/licenses/mit-license.php
3
3
Author Tobias Koppers @sokra
4
4
*/
5
+
5
6
"use strict" ;
6
7
7
8
const normalize = require ( "./normalize" ) ;
8
9
const join = require ( "./join" ) ;
10
+ const MemoryFileSystemError = require ( "./MemoryFileSystemError" ) ;
9
11
const errors = require ( "errno" ) ;
10
12
const stream = require ( "readable-stream" ) ;
11
13
12
14
const ReadableStream = stream . Readable ;
13
15
const WritableStream = stream . Writable ;
14
16
15
- class MemoryFileSystemError extends Error {
16
- constructor ( err , path ) {
17
- super ( err , path ) ;
18
- if ( Error . captureStackTrace )
19
- Error . captureStackTrace ( this , this . constructor )
20
- this . code = err . code ;
21
- this . errno = err . errno ;
22
- this . message = err . description ;
23
- this . path = path ;
24
- }
25
- }
26
-
27
17
function isDir ( item ) {
28
18
if ( typeof item !== "object" ) return false ;
29
19
return item [ "" ] === true ;
@@ -102,7 +92,7 @@ class MemoryFileSystem {
102
92
isSocket : falseFn
103
93
} ;
104
94
} else {
105
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
95
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "stat" ) ;
106
96
}
107
97
}
108
98
@@ -112,14 +102,14 @@ class MemoryFileSystem {
112
102
let i = 0
113
103
for ( ; i < path . length - 1 ; i ++ ) {
114
104
if ( ! isDir ( current [ path [ i ] ] ) )
115
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
105
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "readFile" ) ;
116
106
current = current [ path [ i ] ] ;
117
107
}
118
108
if ( ! isFile ( current [ path [ i ] ] ) ) {
119
109
if ( isDir ( current [ path [ i ] ] ) )
120
- throw new MemoryFileSystemError ( errors . code . EISDIR , _path ) ;
110
+ throw new MemoryFileSystemError ( errors . code . EISDIR , _path , "readFile" ) ;
121
111
else
122
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
112
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "readFile" ) ;
123
113
}
124
114
current = current [ path [ i ] ] ;
125
115
const encoding = typeof optionsOrEncoding === "object" ? optionsOrEncoding . encoding : optionsOrEncoding ;
@@ -133,14 +123,14 @@ class MemoryFileSystem {
133
123
let i = 0 ;
134
124
for ( ; i < path . length - 1 ; i ++ ) {
135
125
if ( ! isDir ( current [ path [ i ] ] ) )
136
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
126
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "readdir" ) ;
137
127
current = current [ path [ i ] ] ;
138
128
}
139
129
if ( ! isDir ( current [ path [ i ] ] ) ) {
140
130
if ( isFile ( current [ path [ i ] ] ) )
141
- throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path ) ;
131
+ throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path , "readdir" ) ;
142
132
else
143
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
133
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "readdir" ) ;
144
134
}
145
135
return Object . keys ( current [ path [ i ] ] ) . filter ( Boolean ) ;
146
136
}
@@ -151,7 +141,7 @@ class MemoryFileSystem {
151
141
let current = this . data ;
152
142
for ( let i = 0 ; i < path . length ; i ++ ) {
153
143
if ( isFile ( current [ path [ i ] ] ) )
154
- throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path ) ;
144
+ throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path , "mkdirp" ) ;
155
145
else if ( ! isDir ( current [ path [ i ] ] ) )
156
146
current [ path [ i ] ] = { "" :true } ;
157
147
current = current [ path [ i ] ] ;
@@ -166,31 +156,32 @@ class MemoryFileSystem {
166
156
let i = 0 ;
167
157
for ( ; i < path . length - 1 ; i ++ ) {
168
158
if ( ! isDir ( current [ path [ i ] ] ) )
169
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
159
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "mkdir" ) ;
170
160
current = current [ path [ i ] ] ;
171
161
}
172
162
if ( isDir ( current [ path [ i ] ] ) )
173
- throw new MemoryFileSystemError ( errors . code . EEXIST , _path ) ;
163
+ throw new MemoryFileSystemError ( errors . code . EEXIST , _path , "mkdir" ) ;
174
164
else if ( isFile ( current [ path [ i ] ] ) )
175
- throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path ) ;
165
+ throw new MemoryFileSystemError ( errors . code . ENOTDIR , _path , "mkdir" ) ;
176
166
current [ path [ i ] ] = { "" :true } ;
177
167
return ;
178
168
}
179
169
180
170
_remove ( _path , name , testFn ) {
181
171
const path = pathToArray ( _path ) ;
172
+ const operation = name === "File" ? "unlink" : "rmdir" ;
182
173
if ( path . length === 0 ) {
183
- throw new MemoryFileSystemError ( errors . code . EPERM , _path ) ;
174
+ throw new MemoryFileSystemError ( errors . code . EPERM , _path , operation ) ;
184
175
}
185
176
let current = this . data ;
186
177
let i = 0 ;
187
178
for ( ; i < path . length - 1 ; i ++ ) {
188
179
if ( ! isDir ( current [ path [ i ] ] ) )
189
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
180
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , operation ) ;
190
181
current = current [ path [ i ] ] ;
191
182
}
192
183
if ( ! testFn ( current [ path [ i ] ] ) )
193
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
184
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , operation ) ;
194
185
delete current [ path [ i ] ] ;
195
186
return ;
196
187
}
@@ -204,24 +195,24 @@ class MemoryFileSystem {
204
195
}
205
196
206
197
readlinkSync ( _path ) {
207
- throw new MemoryFileSystemError ( errors . code . ENOSYS , _path ) ;
198
+ throw new MemoryFileSystemError ( errors . code . ENOSYS , _path , "readlink" ) ;
208
199
}
209
200
210
201
writeFileSync ( _path , content , optionsOrEncoding ) {
211
202
if ( ! content && ! optionsOrEncoding ) throw new Error ( "No content" ) ;
212
203
const path = pathToArray ( _path ) ;
213
204
if ( path . length === 0 ) {
214
- throw new MemoryFileSystemError ( errors . code . EISDIR , _path ) ;
205
+ throw new MemoryFileSystemError ( errors . code . EISDIR , _path , "writeFile" ) ;
215
206
}
216
207
let current = this . data ;
217
208
let i = 0
218
209
for ( ; i < path . length - 1 ; i ++ ) {
219
210
if ( ! isDir ( current [ path [ i ] ] ) )
220
- throw new MemoryFileSystemError ( errors . code . ENOENT , _path ) ;
211
+ throw new MemoryFileSystemError ( errors . code . ENOENT , _path , "writeFile" ) ;
221
212
current = current [ path [ i ] ] ;
222
213
}
223
214
if ( isDir ( current [ path [ i ] ] ) )
224
- throw new MemoryFileSystemError ( errors . code . EISDIR , _path ) ;
215
+ throw new MemoryFileSystemError ( errors . code . EISDIR , _path , "writeFile" ) ;
225
216
const encoding = typeof optionsOrEncoding === "object" ? optionsOrEncoding . encoding : optionsOrEncoding ;
226
217
current [ path [ i ] ] = optionsOrEncoding || typeof content === "string" ? new Buffer ( content , encoding ) : content ;
227
218
return ;
0 commit comments