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

Commit 8d0f163

Browse files
Add an exists and existsSync method.
Sometimes you'd rather prefer not having an error thrown (à la `.stat`). Because both functions share the code for fetching the internal file object, it has been refactored out into a common function called `meta`. Specs updated to match.
1 parent b907853 commit 8d0f163

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/MemoryFileSystem.js

Lines changed: 14 additions & 5 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,
@@ -187,7 +196,7 @@ MemoryFileSystem.prototype.normalize = normalize;
187196

188197
// async functions
189198

190-
["stat", "readdir", "mkdirp", "mkdir", "rmdir", "unlink", "readlink"].forEach(function(fn) {
199+
["exists", "stat", "readdir", "mkdirp", "mkdir", "rmdir", "unlink", "readlink"].forEach(function(fn) {
191200
MemoryFileSystem.prototype[fn] = function(path, callback) {
192201
try {
193202
var result = this[fn + "Sync"](path);

test/MemoryFileSystem.js

Lines changed: 5 additions & 1 deletion
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);
@@ -359,4 +363,4 @@ describe("os", function() {
359363
fileSystem.statSync("C:\\a/dir\\index").isFile().should.be.eql(true);
360364
});
361365
});
362-
});
366+
});

0 commit comments

Comments
 (0)