Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit a4d5b02

Browse files
committed
Merge pull request #4 from izaakschroeder/exists
Add an `exists` and `existsSync` method.
2 parents 2d307df + 71d9401 commit a4d5b02

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/MemoryFileSystem.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,24 @@ function pathToArray(path) {
3939
function trueFn() { return true; }
4040
function falseFn() { return false; }
4141

42-
MemoryFileSystem.prototype.statSync = function(_path) {
42+
MemoryFileSystem.prototype.meta = function(_path) {
4343
var path = pathToArray(_path);
4444
var current = this.data;
4545
for(var i = 0; i < path.length - 1; i++) {
4646
if(!isDir(current[path[i]]))
47-
throw new Error("Path doesn't exist '" + _path + "'");
47+
return null;
4848
current = current[path[i]];
4949
}
50-
if(_path === "/" || isDir(current[path[i]])) {
50+
return current[path[i]];
51+
}
52+
53+
MemoryFileSystem.prototype.existsSync = function(_path) {
54+
return !!this.meta(_path);
55+
}
56+
57+
MemoryFileSystem.prototype.statSync = function(_path) {
58+
var current = this.meta(_path);
59+
if(_path === "/" || isDir(current)) {
5160
return {
5261
isFile: falseFn,
5362
isDirectory: trueFn,
@@ -57,7 +66,7 @@ MemoryFileSystem.prototype.statSync = function(_path) {
5766
isFIFO: falseFn,
5867
isSocket: falseFn
5968
};
60-
} else if(isFile(current[path[i]])) {
69+
} else if(isFile(current)) {
6170
return {
6271
isFile: trueFn,
6372
isDirectory: falseFn,
@@ -198,6 +207,10 @@ MemoryFileSystem.prototype.normalize = normalize;
198207
};
199208
});
200209

210+
MemoryFileSystem.prototype.exists = function(path, callback) {
211+
return callback(this.existsSync(path));
212+
}
213+
201214
MemoryFileSystem.prototype.readFile = function(path, optArg, callback) {
202215
if(!callback) {
203216
callback = optArg;

test/MemoryFileSystem.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ describe("directory", function() {
2828
fs.readdirSync("//test").should.be.eql(["sub2"]);
2929
fs.rmdirSync("/test/sub2");
3030
fs.rmdirSync("/test");
31+
fs.existsSync("/test").should.be.eql(false);
3132
(function() {
3233
fs.readdirSync("/test");
3334
}).should.throw();
3435
fs.readdirSync("/").should.be.eql(["root\\dir"]);
3536
fs.mkdirpSync("/a/depth/sub/dir");
37+
fs.existsSync("/a/depth/sub").should.be.eql(true);
3638
var stat = fs.statSync("/a/depth/sub");
3739
stat.isFile().should.be.eql(false);
3840
stat.isDirectory().should.be.eql(true);
@@ -50,11 +52,13 @@ describe("directory", function() {
5052
fs.readdirSync("C:\\\\test").should.be.eql(["sub2"]);
5153
fs.rmdirSync("C:\\test\\sub2");
5254
fs.rmdirSync("C:\\test");
55+
fs.existsSync("C:\\test").should.be.eql(false);
5356
(function() {
5457
fs.readdirSync("C:\\test");
5558
}).should.throw();
5659
fs.readdirSync("C:").should.be.eql(["root-dir"]);
5760
fs.mkdirpSync("D:\\a\\depth\\sub\\dir");
61+
fs.existsSync("D:\\a\\depth\\sub").should.be.eql(true);
5862
var stat = fs.statSync("D:\\a\\depth\\sub");
5963
stat.isFile().should.be.eql(false);
6064
stat.isDirectory().should.be.eql(true);
@@ -206,7 +210,10 @@ describe("async", function() {
206210
fs.readFile("/test/dir/b", function(err, content) {
207211
if(err) throw err;
208212
content.should.be.eql(new Buffer("World"));
209-
done();
213+
fs.exists("/test/dir/b", function(exists) {
214+
exists.should.be.eql(true);
215+
done();
216+
});
210217
});
211218
});
212219
});
@@ -359,4 +366,4 @@ describe("os", function() {
359366
fileSystem.statSync("C:\\a/dir\\index").isFile().should.be.eql(true);
360367
});
361368
});
362-
});
369+
});

0 commit comments

Comments
 (0)