Skip to content

Commit

Permalink
Update to prettier, add another test, update readme, bump npm version
Browse files Browse the repository at this point in the history
  • Loading branch information
jergason committed May 15, 2017
1 parent 6ee5c98 commit 270e8df
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 300 deletions.
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/jergason/recursive-readdir.svg?branch=master)](https://travis-ci.org/jergason/recursive-readdir)

A Node module for recursively listing all files in a directory and its subdirectories. It does not list the directories themselves.
Recursively list all files in a directory and its subdirectories. It does not list the directories themselves.

Because it uses fs.readdir, which calls [readdir](http://linux.die.net/man/3/readdir) under the hood
on OS X and Linux, the order of files inside directories is [not guaranteed](http://stackoverflow.com/questions/8977441/does-readdir-guarantee-an-order).
Expand All @@ -13,24 +13,22 @@ on OS X and Linux, the order of files inside directories is [not guaranteed](htt

## Usage


```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);
});
```

It can also take a list of files to ignore.

```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);
});
```
Expand All @@ -39,20 +37,33 @@ You can also pass functions which are called to determine whether or not to
ignore a file:

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

function ignoreFunc(file, stats) {
// `file` is the absolute path to the file, and `stats` is an `fs.Stats`
// object returned from `fs.lstat()`.
return stats.isDirectory() && path.basename(file) == "test";
}

// 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);
});
```

## 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).
97 changes: 54 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,105 @@
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);
};
}

readdir("some/path").then(
function(files) {
console.log("files are", files);
},
function(error) {
console.error("something exploded", error);
}
);

function toMatcherFunction(ignoreEntry) {
if (typeof ignoreEntry == 'function') {
return ignoreEntry
if (typeof ignoreEntry == "function") {
return ignoreEntry;
} else {
return patternMatcher(ignoreEntry)
return patternMatcher(ignoreEntry);
}
}

function readdir(path, ignores, callback) {

if (typeof ignores == 'function') {
callback = ignores
ignores = []
if (typeof ignores == "function") {
callback = ignores;
ignores = [];
}

if (!callback) {
return new Promise(function (resolve, reject) {
readdir(path, ignores || [], function (err, data) {
return new Promise(function(resolve, reject) {
readdir(path, ignores || [], function(err, data) {
if (err) {
reject(err)
reject(err);
} else {
resolve(data)
resolve(data);
}
})
})
});
});
}

ignores = ignores.map(toMatcherFunction)
ignores = ignores.map(toMatcherFunction);

var list = []
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;
}

if (stats.isDirectory()) {
readdir(filePath, ignores, function(__err, res) {
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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "recursive-readdir",
"description": "Get an array of all files in a directory and subdirectories.",
"license": "MIT",
"version": "2.1.1",
"version": "2.2.0",
"repository": {
"type": "git",
"url": "git://github.com/jergason/recursive-readdir.git"
Expand Down
Loading

0 comments on commit 270e8df

Please sign in to comment.