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

recursive-readdir

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recursive-readdir - npm Package Compare versions

Comparing version 2.1.1 to 2.2.1

88

index.js

@@ -1,17 +0,17 @@

var fs = require('fs')
var p = require('path')
var minimatch = require('minimatch')
var fs = require("fs");
var p = require("path");
var minimatch = require("minimatch");
function patternMatcher(pattern) {
return function(path, stats) {
var minimatcher = new minimatch.Minimatch(pattern, {matchBase: true})
return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path)
}
var minimatcher = new minimatch.Minimatch(pattern, { matchBase: true });
return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path);
};
}
function toMatcherFunction(ignoreEntry) {
if (typeof ignoreEntry == 'function') {
return ignoreEntry
if (typeof ignoreEntry == "function") {
return ignoreEntry;
} else {
return patternMatcher(ignoreEntry)
return patternMatcher(ignoreEntry);
}

@@ -21,34 +21,51 @@ }

function readdir(path, ignores, callback) {
if (typeof ignores == 'function') {
callback = ignores
ignores = []
if (typeof ignores == "function") {
callback = ignores;
ignores = [];
}
ignores = ignores.map(toMatcherFunction)
var list = []
if (!callback) {
return new Promise(function(resolve, reject) {
readdir(path, ignores || [], function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
ignores = ignores.map(toMatcherFunction);
var list = [];
fs.readdir(path, function(err, files) {
if (err) {
return callback(err)
return callback(err);
}
var pending = files.length
var pending = files.length;
if (!pending) {
// we are done, woop woop
return callback(null, list)
return callback(null, list);
}
files.forEach(function(file) {
var filePath = p.join(path, file)
var filePath = p.join(path, file);
fs.stat(filePath, function(_err, stats) {
if (_err) {
return callback(_err)
return callback(_err);
}
if (ignores.some(function(matcher) { return matcher(filePath, stats) })) {
pending -= 1
if (
ignores.some(function(matcher) {
return matcher(filePath, stats);
})
) {
pending -= 1;
if (!pending) {
return callback(null, list)
return callback(null, list);
}
return null
return null;
}

@@ -59,24 +76,23 @@

if (__err) {
return callback(__err)
return callback(__err);
}
list = list.concat(res)
pending -= 1
list = list.concat(res);
pending -= 1;
if (!pending) {
return callback(null, list)
return callback(null, list);
}
})
});
} else {
list.push(filePath)
pending -= 1
list.push(filePath);
pending -= 1;
if (!pending) {
return callback(null, list)
return callback(null, list);
}
}
})
})
})
});
});
});
}
module.exports = readdir
module.exports = readdir;

@@ -6,3 +6,3 @@ {

"license": "MIT",
"version": "2.1.1",
"version": "2.2.1",
"repository": {

@@ -9,0 +9,0 @@ "type": "git",

@@ -5,7 +5,4 @@ # recursive-readdir

A simple Node module for recursively listing all files in a directory,
or in any subdirectories.
Recursively list all files in a directory and its subdirectories. It does not list the directories themselves.
It does not list directories themselves.
Because it uses fs.readdir, which calls [readdir](http://linux.die.net/man/3/readdir) under the hood

@@ -20,8 +17,7 @@ on OS X and Linux, the order of files inside directories is [not guaranteed](http://stackoverflow.com/questions/8977441/does-readdir-guarantee-an-order).

```javascript
var recursive = require('recursive-readdir');
var recursive = require("recursive-readdir");
recursive('some/path', function (err, files) {
// Files is an array of filename
recursive("some/path", function (err, files) {
// `files` is an array of absolute file paths
console.log(files);

@@ -34,7 +30,6 @@ });

```javascript
var recursive = require('recursive-readdir');
var recursive = require("recursive-readdir");
// ignore files named 'foo.cs' or files that end in '.html'.
recursive('some/path', ['foo.cs', '*.html'], function (err, files) {
// Files is an array of filename
// ignore files named "foo.cs" or files that end in ".html".
recursive("some/path", ["foo.cs", "*.html"], function (err, files) {
console.log(files);

@@ -48,3 +43,3 @@ });

```javascript
var recursive = require('recursive-readdir');
var recursive = require("recursive-readdir");

@@ -57,5 +52,4 @@ function ignoreFunc(file, stats) {

// Ignore files named 'foo.cs' and descendants of directories named test
recursive('some/path', ['foo.cs', ignoreFunc], function (err, files) {
// Files is an array of filename
// Ignore files named "foo.cs" and descendants of directories named test
recursive("some/path", ["foo.cs", ignoreFunc], function (err, files) {
console.log(files);

@@ -65,3 +59,17 @@ });

## Promises
You can omit the callback and return a promise instead.
```javascript
readdir("some/path").then(
function(files) {
console.log("files are", files);
},
function(error) {
console.error("something exploded", error);
}
);
```
The ignore strings support Glob syntax via
[minimatch](https://github.com/isaacs/minimatch).
/* eslint-env mocha */
var assert = require('assert')
var p = require('path')
var readdir = require('../index')
var assert = require("assert");
var p = require("path");
var readdir = require("../index");
function getAbsolutePath(file) {
return p.join(__dirname, file)
return p.join(__dirname, file);
}
function getAbsolutePaths(files) {
return files.map(getAbsolutePath)
return files.map(getAbsolutePath);
}
describe('readdir', function() {
it('correctly lists all files in nested directories', function(done) {
describe("readdir", function() {
it("correctly lists all files in nested directories", function(done) {
var expectedFiles = getAbsolutePaths([
'/testdir/a/a', '/testdir/a/beans',
'/testdir/b/123', '/testdir/b/b/hurp-durp',
'/testdir/c.txt', '/testdir/d.txt'
])
"/testdir/a/a",
"/testdir/a/beans",
"/testdir/b/123",
"/testdir/b/b/hurp-durp",
"/testdir/c.txt",
"/testdir/d.txt"
]);
readdir(p.join(__dirname, 'testdir'), function(err, list) {
assert.ifError(err)
assert.deepEqual(list.sort(), expectedFiles.sort())
done()
})
})
readdir(p.join(__dirname, "testdir"), function(err, list) {
assert.ifError(err);
assert.deepEqual(list.sort(), expectedFiles.sort());
done();
});
});
it('ignores the files listed in the ignores array', function(done) {
it("ignores the files listed in the ignores array", function(done) {
var notExpectedFiles = getAbsolutePaths([
'/testdir/d.txt', '/testdir/a/beans'
])
"/testdir/d.txt",
"/testdir/a/beans"
]);
readdir(p.join(__dirname, 'testdir'), ['d.txt', 'beans'], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), ["d.txt", "beans"], function(
err,
list
) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(notExpectedFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
assert.equal(
notExpectedFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
});
});
it('ignores the directories listed in the ignores array', function(done) {
it("ignores the directories listed in the ignores array", function(done) {
var notExpectedFiles = getAbsolutePaths([
'/testdir/a/a', '/testdir/a/beans'
])
"/testdir/a/a",
"/testdir/a/beans"
]);
readdir(p.join(__dirname, 'testdir'), ['**/testdir/a'], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), ["**/testdir/a"], function(
err,
list
) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(notExpectedFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
assert.equal(
notExpectedFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
});
});
it('ignores symlinked files and directories listed in the ignores array', function(done) {
it("ignores symlinked files and directories listed in the ignores array", function(
done
) {
var notExpectedFiles = getAbsolutePaths([
'/testsymlinks/testdir/linkeddir/hi.docx', '/testsymlinks/testdir/linkedfile.wmf'
])
readdir(p.join(__dirname, 'testsymlinks/testdir'), ['linkeddir', 'linkedfile.wmf'], function(err, list) {
assert.ifError(err)
list.forEach(function(file) {
assert.equal(notExpectedFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
"/testsymlinks/testdir/linkeddir/hi.docx",
"/testsymlinks/testdir/linkedfile.wmf"
]);
readdir(
p.join(__dirname, "testsymlinks/testdir"),
["linkeddir", "linkedfile.wmf"],
function(err, list) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(
notExpectedFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
}
);
});
it('supports ignoring files with just basename globbing', function(done) {
it("supports ignoring files with just basename globbing", function(done) {
var notExpectedFiles = getAbsolutePaths([
'/testdir/d.txt', '/testdir/a/beans'
])
"/testdir/d.txt",
"/testdir/a/beans"
]);
readdir(p.join(__dirname, 'testdir'), ['*.txt', 'beans'], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), ["*.txt", "beans"], function(
err,
list
) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(notExpectedFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
assert.equal(
notExpectedFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
});
});
it('supports ignoring files with the globstar syntax', function(done) {
it("supports ignoring files with the globstar syntax", function(done) {
var notExpectedFiles = getAbsolutePaths([
'/testdir/d.txt', '/testdir/a/beans'
])
"/testdir/d.txt",
"/testdir/a/beans"
]);
var ignores = ['**/*.txt', '**/a/beans']
var ignores = ["**/*.txt", "**/a/beans"];
readdir(p.join(__dirname, 'testdir'), ignores, function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), ignores, function(err, list) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(notExpectedFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
assert.equal(
notExpectedFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
});
});
context('when there is a function in the ignores array', function() {
it('passes each file and directory path to the function', function(done) {
context("when there is a function in the ignores array", function() {
it("passes each file and directory path to the function", function(done) {
var expectedPaths = getAbsolutePaths([
'/testdir/a',
'/testdir/a/a',
'/testdir/a/beans',
'/testdir/b',
'/testdir/b/123',
'/testdir/b/b',
'/testdir/b/b/hurp-durp',
'/testdir/c.txt',
'/testdir/d.txt'
])
var paths = []
"/testdir/a",
"/testdir/a/a",
"/testdir/a/beans",
"/testdir/b",
"/testdir/b/123",
"/testdir/b/b",
"/testdir/b/b/hurp-durp",
"/testdir/c.txt",
"/testdir/d.txt"
]);
var paths = [];
function ignoreFunction(path) {
paths.push(path)
return false
paths.push(path);
return false;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
assert.deepEqual(paths.sort(), expectedPaths.sort())
done()
})
})
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
assert.deepEqual(paths.sort(), expectedPaths.sort());
done();
});
});
it('passes the stat object of each file to the function as its second argument', function(done) {
var paths = {}
it("passes the stat object of each file to the function as its second argument", function(
done
) {
var paths = {};
function ignoreFunction(path, stats) {
paths[path] = stats
return false
paths[path] = stats;
return false;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
assert(paths[getAbsolutePath('/testdir/a')].isDirectory())
assert(paths[getAbsolutePath('/testdir/c.txt')].isFile())
done()
})
})
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
assert(paths[getAbsolutePath("/testdir/a")].isDirectory());
assert(paths[getAbsolutePath("/testdir/c.txt")].isFile());
done();
});
});
it('ignores files that the function returns true for', function(done) {
it("ignores files that the function returns true for", function(done) {
var ignoredFiles = getAbsolutePaths([
'/testdir/d.txt',
'/testdir/a/beans'
])
"/testdir/d.txt",
"/testdir/a/beans"
]);
function ignoreFunction(path) {
return ignoredFiles.indexOf(path) != -1
return ignoredFiles.indexOf(path) != -1;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(ignoredFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
assert.equal(
ignoredFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
});
});
it('does not ignore files that the function returns false for', function(done) {
it("does not ignore files that the function returns false for", function(
done
) {
var notIgnoredFiles = getAbsolutePaths([
'/testdir/d.txt',
'/testdir/a/beans'
])
"/testdir/d.txt",
"/testdir/a/beans"
]);
function ignoreFunction(path) {
return notIgnoredFiles.indexOf(path) == -1
return notIgnoredFiles.indexOf(path) == -1;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
notIgnoredFiles.forEach(function(file) {
assert.notEqual(notIgnoredFiles.indexOf(file), -1,
'Incorrectly ignored file "' + file + '".')
})
done()
})
})
assert.notEqual(
notIgnoredFiles.indexOf(file),
-1,
'Incorrectly ignored file "' + file + '".'
);
});
done();
});
});
it('ignores directories that the function returns true for', function(done) {
var ignoredDirectory = getAbsolutePath('/testdir/a')
var ignoredFiles = getAbsolutePaths([
'/testdir/a/a',
'/testdir/a/beans'
])
it("ignores directories that the function returns true for", function(
done
) {
var ignoredDirectory = getAbsolutePath("/testdir/a");
var ignoredFiles = getAbsolutePaths(["/testdir/a/a", "/testdir/a/beans"]);
function ignoreFunction(path) {
return ignoredDirectory == path
return ignoredDirectory == path;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
list.forEach(function(file) {
assert.equal(ignoredFiles.indexOf(file), -1,
'Failed to ignore file "' + file + '".')
})
done()
})
})
assert.equal(
ignoredFiles.indexOf(file),
-1,
'Failed to ignore file "' + file + '".'
);
});
done();
});
});
it('does not ignore directories that the function returns false for', function(done) {
var ignoredDirectory = getAbsolutePath('/testdir/a')
it("does not ignore directories that the function returns false for", function(
done
) {
var ignoredDirectory = getAbsolutePath("/testdir/a");
var notIgnoredFiles = getAbsolutePaths([
'/testdir/b/123',
'/testdir/b/b/hurp-durp'
])
"/testdir/b/123",
"/testdir/b/b/hurp-durp"
]);
function ignoreFunction(path) {
return ignoredDirectory == path
return ignoredDirectory == path;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
notIgnoredFiles.forEach(function(file) {
assert.notEqual(notIgnoredFiles.indexOf(file), -1,
'Incorrectly ignored file "' + file + '".')
})
done()
})
})
assert.notEqual(
notIgnoredFiles.indexOf(file),
-1,
'Incorrectly ignored file "' + file + '".'
);
});
done();
});
});
it('does not descend into directories that the function returns true for', function(done) {
var ignoredDirectory = getAbsolutePath('/testdir/a')
var ignoredFiles = getAbsolutePaths([
'/testdir/a/a',
'/testdir/a/beans'
])
var paths = []
it("does not descend into directories that the function returns true for", function(
done
) {
var ignoredDirectory = getAbsolutePath("/testdir/a");
var ignoredFiles = getAbsolutePaths(["/testdir/a/a", "/testdir/a/beans"]);
var paths = [];
function ignoreFunction(path) {
paths.push(path)
return ignoredDirectory == path
paths.push(path);
return ignoredDirectory == path;
}
readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
assert.ifError(err)
readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
err,
list
) {
assert.ifError(err);
paths.forEach(function(file) {
assert.equal(ignoredFiles.indexOf(file), -1,
'Transversed file in ignored directory "' + file + '".')
})
done()
})
})
})
assert.equal(
ignoredFiles.indexOf(file),
-1,
'Transversed file in ignored directory "' + file + '".'
);
});
done();
});
});
});
it('works when there are no files to report except ignored files', function(done) {
readdir(p.join(__dirname, 'testdirBeta'), ['*'], function(err, list) {
assert.ifError(err)
assert.equal(list.length, 0, 'expect to report 0 files')
done()
})
})
it("works when there are no files to report except ignored files", function(
done
) {
readdir(p.join(__dirname, "testdirBeta"), ["*"], function(err, list) {
assert.ifError(err);
assert.equal(list.length, 0, "expect to report 0 files");
done();
});
});
it('works when negated ignore list is given', function(done) {
var expectedFiles = getAbsolutePaths([
'/testdirBeta/ignore.txt'
])
it("works when negated ignore list is given", function(done) {
var expectedFiles = getAbsolutePaths(["/testdirBeta/ignore.txt"]);
readdir(p.join(__dirname, 'testdirBeta'), ['!*.txt'], function(err, list) {
assert.ifError(err)
assert.deepEqual(list.sort(), expectedFiles,
'Failed to find expected files.')
done()
})
})
readdir(p.join(__dirname, "testdirBeta"), ["!*.txt"], function(err, list) {
assert.ifError(err);
assert.deepEqual(
list.sort(),
expectedFiles,
"Failed to find expected files."
);
done();
});
});
it('traverses directory and file symbolic links', function(done) {
it("traverses directory and file symbolic links", function(done) {
var expectedFiles = getAbsolutePaths([
'/testsymlinks/testdir/linkeddir/hi.docx',
'/testsymlinks/testdir/linkedfile.wmf'
])
"/testsymlinks/testdir/linkeddir/hi.docx",
"/testsymlinks/testdir/linkedfile.wmf"
]);
readdir(p.join(__dirname,'testsymlinks','testdir'), function(err, list) {
assert.ifError(err)
assert.deepEqual(list.sort(), expectedFiles,
'Failed to find expected files.')
done()
})
})
})
readdir(p.join(__dirname, "testsymlinks", "testdir"), function(err, list) {
assert.ifError(err);
assert.deepEqual(
list.sort(),
expectedFiles,
"Failed to find expected files."
);
done();
});
});
if (!global.Promise) {
console.log("Native Promise not supported - skipping tests");
} else {
it("works with promises", function(done) {
var expectedFiles = getAbsolutePaths([
"/testdir/a/a",
"/testdir/a/beans",
"/testdir/b/123",
"/testdir/b/b/hurp-durp",
"/testdir/c.txt",
"/testdir/d.txt"
]);
readdir(p.join(__dirname, "testdir"))
.then(function(list) {
assert.deepEqual(list.sort(), expectedFiles.sort());
done();
})
.catch(done);
});
it("correctly ignores when using promises", function(done) {
var expectedFiles = getAbsolutePaths([
"/testdir/a/a",
"/testdir/a/beans",
"/testdir/b/123",
"/testdir/b/b/hurp-durp"
]);
readdir(p.join(__dirname, "testdir"), ["*.txt"])
.then(function(list) {
assert.deepEqual(list.sort(), expectedFiles.sort());
done();
})
.catch(done);
});
}
});
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