|
1 | | -const fs = require('fs'); |
2 | | -const exists = require('fs-exists-sync'); |
| 1 | +'use strict'; |
3 | 2 |
|
4 | | -function isEmpty(path, fn, callback) { |
| 3 | +var fs = require('fs'); |
| 4 | + |
| 5 | +function emptyDir(dir, filter, cb) { |
5 | 6 | if (arguments.length === 2) { |
6 | | - callback = fn; |
7 | | - fn = null; |
| 7 | + cb = filter; |
| 8 | + filter = null; |
| 9 | + } |
| 10 | + |
| 11 | + if (typeof cb !== 'function') { |
| 12 | + throw new TypeError('expected callback to be a function'); |
8 | 13 | } |
9 | 14 |
|
10 | | - if (!exists(path)) { |
11 | | - callback(null, false); |
| 15 | + if (Array.isArray(dir)) { |
| 16 | + cb(null, isEmpty(dir, filter)); |
12 | 17 | return; |
13 | 18 | } |
14 | 19 |
|
15 | | - fs.readdir(path, function(err, files) { |
16 | | - if (err) { |
17 | | - callback(err); |
18 | | - return; |
19 | | - } |
| 20 | + if (typeof dir !== 'string') { |
| 21 | + cb(new TypeError('expected a directory or array of files')); |
| 22 | + return; |
| 23 | + } |
20 | 24 |
|
21 | | - if (typeof fn === 'function') { |
22 | | - files = files.filter(fn); |
| 25 | + fs.stat(dir, function(err, stat) { |
| 26 | + if (err || !stat.isDirectory()) { |
| 27 | + cb(null, false); |
| 28 | + return; |
23 | 29 | } |
24 | 30 |
|
25 | | - callback(null, files.length === 0); |
| 31 | + fs.readdir(dir, function(err, files) { |
| 32 | + cb(err, isEmpty(files, filter)); |
| 33 | + }); |
26 | 34 | }); |
27 | 35 | } |
28 | 36 |
|
29 | | -isEmpty.sync = function(path, fn) { |
30 | | - if (!exists(path)) { |
| 37 | +/** |
| 38 | + * Return true if the given `files` array has zero length or only |
| 39 | + * includes unwanted files. |
| 40 | + */ |
| 41 | + |
| 42 | +function emptyDirSync(dir, filter) { |
| 43 | + if (Array.isArray(dir)) { |
| 44 | + return isEmpty(dir, filter); |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof dir !== 'string') { |
| 48 | + throw new TypeError('expected a directory or array of files'); |
| 49 | + } |
| 50 | + |
| 51 | + if (!isDirectory(dir)) { |
31 | 52 | return false; |
32 | 53 | } |
33 | | - try { |
34 | | - var files = fs.readdirSync(path); |
35 | | - if (typeof fn === 'function') { |
36 | | - files = files.filter(fn); |
| 54 | + |
| 55 | + var files = fs.readdirSync(dir); |
| 56 | + return isEmpty(files, filter); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Returns true if the given "files" array is empty or only |
| 61 | + * contains unwanted files. |
| 62 | + */ |
| 63 | + |
| 64 | +function isEmpty(files, filter) { |
| 65 | + if (files.length === 0) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + if (typeof filter !== 'function') { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + for (var i = 0; i < files.length; ++i) { |
| 74 | + if (filter(files[i]) === false) { |
| 75 | + return false; |
37 | 76 | } |
38 | | - return files.length === 0; |
| 77 | + } |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Returns true if the filepath exists and is a directory |
| 83 | + */ |
| 84 | + |
| 85 | +function isDirectory(filepath) { |
| 86 | + try { |
| 87 | + return fs.statSync(filepath).isDirectory(); |
39 | 88 | } catch (err) {} |
40 | 89 | return false; |
41 | | -}; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Expose `emptyDir` |
| 94 | + */ |
42 | 95 |
|
43 | | -module.exports = isEmpty; |
| 96 | +module.exports = emptyDir; |
| 97 | +module.exports.sync = emptyDirSync; |
| 98 | +module.exports.isEmpty = isEmpty; |
0 commit comments