Breaking: Use Promise internally and return it when no callback speci… · gulpjs/empty-dir@45d1e0c · GitHub
Skip to content

Commit 45d1e0c

Browse files
stephenmathiesonphated
authored andcommitted
Breaking: Use Promise internally and return it when no callback specified (closes #5)
1 parent 267f110 commit 45d1e0c

7 files changed

Lines changed: 94 additions & 34 deletions

File tree

.eslintrc

Lines changed: 4 additions & 1 deletion

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ node_js:
55
- '8'
66
- '6'
77
- '4'
8-
- '0.12'
9-
- '0.10'
108
after_script:
119
- npm run coveralls

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@ Check if a directory is empty.
1515
```js
1616
var emptyDir = require('empty-dir');
1717

18-
emptyDir('./', function (err, result) {
18+
// Using an error-back
19+
emptyDir('./', function(err, result) {
1920
if (err) {
2021
console.error(err);
2122
} else {
2223
console.log('Directory is empty:', result);
2324
}
2425
});
2526

27+
// Using a Promise
28+
emptyDir('./').then(function(result) {
29+
console.log('Directory is empty:', result);
30+
});
31+
2632
var result = emptyDir.sync('./test/empty');
2733
console.log('Directory is empty:', result);
2834
```
2935

3036
## API
3137

32-
### `emptyDir(paths, [filterFunction], callback)`
38+
### `emptyDir(paths, [filterFunction], [callback])`
3339

34-
Takes a path string or array of path strings and a callback function. Checks if the given paths are empty and passes any `error` and an `isEmpty` flag to the callback. Optionally takes a filter function before the callback to filter out files that cause false positives.
40+
Takes a path string or array of path strings and returns a Promise. Checks if the given paths are empty and resolves with a boolean indicating if the paths are empty directories. Optionally takes a filter function to filter out files that cause false positives. Also, can take a node-style callback function instead of returning a Promise.
3541

3642
### `emptyDir.sync(paths, [filterFunction])`
3743

@@ -48,7 +54,7 @@ function filter(filepath) {
4854
return !/(Thumbs\.db|\.DS_Store)$/i.test(filepath);
4955
}
5056

51-
emptyDir('./', filter, function (err, isEmpty) {
57+
emptyDir('./', filter, function(err, isEmpty) {
5258
if (err) {
5359
console.error(err);
5460
} else {
@@ -60,6 +66,20 @@ var isEmpty = emptyDir.sync('./test/empty', filter);
6066
console.log('Directory is empty:', isEmpty);
6167
```
6268

69+
#### Promises
70+
71+
Global promises are required for this module. If you are using a platform that doesn't have promise support, you'll need to polyfill Promise on the global.
72+
73+
```js
74+
global.Promise = require('insert-your-promise-polyfill-here');
75+
76+
var emptyDir = require('empty-dir');
77+
78+
emptyDir('./').then(function(result) {
79+
console.log('Directory is empty:', result);
80+
});
81+
```
82+
6383
## License
6484

6585
MIT

appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
environment:
55
matrix:
66
# node.js
7-
- nodejs_version: "0.10"
8-
- nodejs_version: "0.12"
97
- nodejs_version: "4"
108
- nodejs_version: "6"
119
- nodejs_version: "8"

index.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,42 @@ function emptyDir(dir, filter, cb) {
88
filter = null;
99
}
1010

11-
if (typeof cb !== 'function') {
11+
if (cb && typeof cb !== 'function') {
1212
throw new TypeError('expected callback to be a function');
1313
}
1414

15-
if (Array.isArray(dir)) {
16-
cb(null, isEmpty(dir, filter));
17-
return;
18-
}
19-
20-
if (typeof dir !== 'string') {
21-
cb(new TypeError('expected a directory or array of files'));
22-
return;
15+
if (!Array.isArray(dir) && typeof dir !== 'string') {
16+
throw new TypeError('expected a directory or array of files');
2317
}
2418

25-
fs.stat(dir, function(err, stat) {
26-
if (err || !stat.isDirectory()) {
27-
cb(null, false);
28-
return;
19+
var p = new Promise(function(resolve, reject) {
20+
if (Array.isArray(dir)) {
21+
return resolve(isEmpty(dir, filter));
2922
}
3023

31-
fs.readdir(dir, function(err, files) {
32-
cb(err, isEmpty(files, filter));
24+
fs.stat(dir, function(err, stat) {
25+
if (err || !stat.isDirectory()) {
26+
return resolve(false);
27+
}
28+
29+
fs.readdir(dir, function(err, files) {
30+
if (err) {
31+
return reject(err);
32+
}
33+
34+
resolve(isEmpty(files, filter));
35+
});
3336
});
3437
});
38+
39+
if (cb) {
40+
p.then(function(result) {
41+
cb(null, result);
42+
}).catch(cb);
43+
return;
44+
}
45+
46+
return p;
3547
}
3648

3749
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"repository": "gulpjs/empty-dir",
1212
"license": "MIT",
1313
"engines": {
14-
"node": ">= 0.10"
14+
"node": ">= 4"
1515
},
1616
"main": "index.js",
1717
"files": [

test/index.js

Lines changed: 38 additions & 9 deletions

0 commit comments

Comments
 (0)