-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking: Refactor and cleanup library
- Loading branch information
1 parent
977a271
commit d53784a
Showing
4 changed files
with
160 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,98 @@ | ||
const fs = require('fs'); | ||
const exists = require('fs-exists-sync'); | ||
'use strict'; | ||
|
||
function isEmpty(path, fn, callback) { | ||
var fs = require('fs'); | ||
|
||
function emptyDir(dir, filter, cb) { | ||
if (arguments.length === 2) { | ||
callback = fn; | ||
fn = null; | ||
cb = filter; | ||
filter = null; | ||
} | ||
|
||
if (typeof cb !== 'function') { | ||
throw new TypeError('expected callback to be a function'); | ||
} | ||
|
||
if (!exists(path)) { | ||
callback(null, false); | ||
if (Array.isArray(dir)) { | ||
cb(null, isEmpty(dir, filter)); | ||
return; | ||
} | ||
|
||
fs.readdir(path, function(err, files) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
if (typeof dir !== 'string') { | ||
cb(new TypeError('expected a directory or array of files')); | ||
return; | ||
} | ||
|
||
if (typeof fn === 'function') { | ||
files = files.filter(fn); | ||
fs.stat(dir, function(err, stat) { | ||
if (err || !stat.isDirectory()) { | ||
cb(null, false); | ||
return; | ||
} | ||
|
||
callback(null, files.length === 0); | ||
fs.readdir(dir, function(err, files) { | ||
cb(err, isEmpty(files, filter)); | ||
}); | ||
}); | ||
} | ||
|
||
isEmpty.sync = function(path, fn) { | ||
if (!exists(path)) { | ||
/** | ||
* Return true if the given `files` array has zero length or only | ||
* includes unwanted files. | ||
*/ | ||
|
||
function emptyDirSync(dir, filter) { | ||
if (Array.isArray(dir)) { | ||
return isEmpty(dir, filter); | ||
} | ||
|
||
if (typeof dir !== 'string') { | ||
throw new TypeError('expected a directory or array of files'); | ||
} | ||
|
||
if (!isDirectory(dir)) { | ||
return false; | ||
} | ||
try { | ||
var files = fs.readdirSync(path); | ||
if (typeof fn === 'function') { | ||
files = files.filter(fn); | ||
|
||
var files = fs.readdirSync(dir); | ||
return isEmpty(files, filter); | ||
} | ||
|
||
/** | ||
* Returns true if the given "files" array is empty or only | ||
* contains unwanted files. | ||
*/ | ||
|
||
function isEmpty(files, filter) { | ||
if (files.length === 0) { | ||
return true; | ||
} | ||
|
||
if (typeof filter !== 'function') { | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < files.length; ++i) { | ||
if (filter(files[i]) === false) { | ||
return false; | ||
} | ||
return files.length === 0; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns true if the filepath exists and is a directory | ||
*/ | ||
|
||
function isDirectory(filepath) { | ||
try { | ||
return fs.statSync(filepath).isDirectory(); | ||
} catch (err) {} | ||
return false; | ||
}; | ||
} | ||
|
||
/** | ||
* Expose `emptyDir` | ||
*/ | ||
|
||
module.exports = isEmpty; | ||
module.exports = emptyDir; | ||
module.exports.sync = emptyDirSync; | ||
module.exports.isEmpty = isEmpty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters