Skip to content

Commit

Permalink
New: Add Promise support when no callback specified (closes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmathieson authored and phated committed Dec 21, 2018
1 parent 267f110 commit d490433
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "gulp"
"extends": "gulp",
"globals": {
"Promise": true
}
}
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ node_js:
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
after_script:
- npm run coveralls
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Check if a directory is empty.
```js
var emptyDir = require('empty-dir');

// Using an error-back
emptyDir('./', function (err, result) {
if (err) {
console.error(err);
Expand All @@ -23,6 +24,11 @@ emptyDir('./', function (err, result) {
}
});

// Using a Promise
emptyDir('./').then(function (result) {
console.log('Directory is empty:', result);
})

var result = emptyDir.sync('./test/empty');
console.log('Directory is empty:', result);
```
Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
environment:
matrix:
# node.js
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- nodejs_version: "4"
- nodejs_version: "6"
- nodejs_version: "8"
Expand Down
44 changes: 29 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,44 @@ function emptyDir(dir, filter, cb) {
filter = null;
}

if (typeof cb !== 'function') {
if (cb && typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}

if (Array.isArray(dir)) {
cb(null, isEmpty(dir, filter));
return;
}

if (typeof dir !== 'string') {
cb(new TypeError('expected a directory or array of files'));
return;
if (!Array.isArray(dir) && typeof dir !== 'string') {
throw new TypeError('expected a directory or array of files');
}

fs.stat(dir, function(err, stat) {
if (err || !stat.isDirectory()) {
cb(null, false);
return;
var p = new Promise(function(resolve, reject) {
if (Array.isArray(dir)) {
return resolve(isEmpty(dir, filter));
}

fs.readdir(dir, function(err, files) {
cb(err, isEmpty(files, filter));
fs.stat(dir, function(err, stat) {
if (err || !stat.isDirectory()) {
return resolve(false);
}

fs.readdir(dir, function(err, files) {
if (err) {
return reject(err);
}

resolve(isEmpty(files, filter));
});
});
});

if (cb) {
p.then(function(result) {
cb(null, result);
}).catch(function(err) {
cb(err);
});
return;
}

return p;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"repository": "gulpjs/empty-dir",
"license": "MIT",
"engines": {
"node": ">= 0.10"
"node": ">= 4"
},
"main": "index.js",
"files": [
Expand Down
47 changes: 38 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function isGarbageFile(filename) {
}

describe('emptyDir', function() {
it('should throw when a callback is not passed', function(done) {
it('should throw when the callback is not a function', function(done) {
expect(function() {
emptyDir('./');
emptyDir('./', 'foo', 'bar');
}).toThrow(/expected/);
done();
});
Expand All @@ -27,11 +27,10 @@ describe('emptyDir', function() {
emptyDir.sync(null);
}).toThrow();

emptyDir(null, function(err) {
expect(err).toExist();
expect(/expected/.test(err.message)).toEqual(true);
done();
});
expect(function() {
emptyDir(null);
}).toThrow();
done();
});

it('should take an array', function(done) {
Expand Down Expand Up @@ -64,8 +63,7 @@ describe('emptyDir', function() {
it('should be false if a directory does not exist', function(done) {
expect(emptyDir.sync('./foo/bar/baz')).toEqual(false);
emptyDir('./foo/bar/baz', function(err, empty) {
expect(empty).toEqual(false);
done();
done(err);
});
});

Expand All @@ -76,4 +74,35 @@ describe('emptyDir', function() {
done();
});
});

describe('without a callback argument', function() {
it('should return a Promise', function(done) {
var p = emptyDir('./test/empty');
expect(p.then).toBeA('function');
p.then(function() {
done();
});
});

it('should resolve with false if a directory does not exist', function(done) {
emptyDir('/root/super/secret').then(function(result) {
expect(result).toEqual(false);
done();
});
});

it('should resolve with true if a directory is empty', function(done) {
emptyDir('./test/empty').then(function(result) {
expect(result).toEqual(true);
done();
});
});

it('should resolve with false if a directory is not empty', function(done) {
emptyDir('./test').then(function(result) {
expect(result).toEqual(false);
done();
});
});
});
});

0 comments on commit d490433

Please sign in to comment.