Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

empty-dir

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

empty-dir - npm Package Compare versions

Comparing version 0.2.1 to 1.0.0

103

index.js

@@ -1,43 +0,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 (!exists(path)) {
callback(null, false);
if (typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}
if (Array.isArray(dir)) {
cb(null, isEmpty(dir, filter));
return;
}
fs.readdir(path, function(err, files) {
if (err) {
callback(err);
if (typeof dir !== 'string') {
cb(new TypeError('expected a directory or array of files'));
return;
}
fs.stat(dir, function(err, stat) {
if (err || !stat.isDirectory()) {
cb(null, false);
return;
}
if (typeof fn === 'function') {
files = files.filter(fn);
}
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;
}
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 true;
}
/**
* Returns true if the filepath exists and is a directory
*/
function isDirectory(filepath) {
try {
var files = fs.readdirSync(path);
if (typeof fn === 'function') {
files = files.filter(fn);
}
return files.length === 0;
return fs.statSync(filepath).isDirectory();
} catch (err) {}
return false;
};
}
module.exports = isEmpty;
/**
* Expose `emptyDir`
*/
module.exports = emptyDir;
module.exports.sync = emptyDirSync;
module.exports.isEmpty = isEmpty;
{
"name": "empty-dir",
"description": "Check if a directory is empty.",
"version": "0.2.1",
"version": "1.0.0",
"homepage": "https://github.com/js-cli/js-empty-dir",

@@ -22,14 +22,11 @@ "author": "Tyler Kellen <http://goingslowly.com/>",

},
"dependencies": {
"fs-exists-sync": "^0.1.0"
},
"keywords": [
"empty",
"is-empty",
"directory",
"folder"
],
"devDependencies": {
"chai": "~1.9.1",
"mocha": "~1.18.2"
},
"keywords": [
"empty directory",
"empty dir",
"empty folder"
]
"mocha": "^3.5.3"
}
}

@@ -6,7 +6,14 @@ # empty-dir [![Build Status](https://secure.travis-ci.org/js-cli/js-empty-dir.svg?branch=master)](http://travis-ci.org/js-cli/js-empty-dir)

Note that directories with `.DS_Store` on mac are considered empty.
## Install
## Example
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save empty-dir
```
## Usage
```js
const emptyDir = require('empty-dir');
var emptyDir = require('empty-dir');

@@ -25,25 +32,24 @@ emptyDir('./', function (err, result) {

**Filter function**
## Filter function
Both async and sync take a filter function as the second argument.
Both async and sync take a filter function as the second argument, to ignore files like `.DS_Store` on mac or `Thumbs.db` on windows from causing false-negatives.
_(This gives you the ability to eliminate files like `.DS_Store` on mac, or `Thumbs.db` on windows from causing the result to be "not empty" (`.DS_Store` is already filtered by default).)_
```js
const emptyDir = require('empty-dir');
var emptyDir = require('empty-dir');
function filter(filepath) {
return !/Thumbs\.db$/i.test(filepath);
return !/(Thumbs\.db|\.DS_Store)$/i.test(filepath);
}
emptyDir('./', filter, function (err, result) {
emptyDir('./', filter, function (err, isEmpty) {
if (err) {
console.error(err);
} else {
console.log('Directory is empty:', result);
console.log('Directory is empty:', isEmpty);
}
});
var result = emptyDir.sync('./test/empty', filter);
console.log('Directory is empty:', result);
var isEmpty = emptyDir.sync('./test/empty', filter);
console.log('Directory is empty:', isEmpty);
```

@@ -53,3 +59,4 @@

* 2018-03-09 - v1.0.0 - refactored "isEmpty" logic so that it returns early, as soon as a non-filtered file is encountered, instead of filtering the entire list and comparing against length. Also allows an array to be passed (this avoids having to call `fs.readdir()` multiple times).
* 2016-02-07 - v0.2.0 - add filter support
* 2014-05-08 - v0.1.0 - initial release
* 2016-02-07 - v0.2.0 - add filter support
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc