| [9af201e] | 1 | # recursive-readdir
|
|---|
| 2 |
|
|---|
| 3 | [](https://travis-ci.org/jergason/recursive-readdir)
|
|---|
| 4 |
|
|---|
| 5 | Recursively list all files in a directory and its subdirectories. It does not list the directories themselves.
|
|---|
| 6 |
|
|---|
| 7 | Because it uses fs.readdir, which calls [readdir](http://linux.die.net/man/3/readdir) under the hood
|
|---|
| 8 | on OS X and Linux, the order of files inside directories is [not guaranteed](http://stackoverflow.com/questions/8977441/does-readdir-guarantee-an-order).
|
|---|
| 9 |
|
|---|
| 10 | ## Installation
|
|---|
| 11 |
|
|---|
| 12 | npm install recursive-readdir
|
|---|
| 13 |
|
|---|
| 14 | ## Usage
|
|---|
| 15 |
|
|---|
| 16 | ```javascript
|
|---|
| 17 | var recursive = require("recursive-readdir");
|
|---|
| 18 |
|
|---|
| 19 | recursive("some/path", function (err, files) {
|
|---|
| 20 | // `files` is an array of file paths
|
|---|
| 21 | console.log(files);
|
|---|
| 22 | });
|
|---|
| 23 | ```
|
|---|
| 24 |
|
|---|
| 25 | It can also take a list of files to ignore.
|
|---|
| 26 |
|
|---|
| 27 | ```javascript
|
|---|
| 28 | var recursive = require("recursive-readdir");
|
|---|
| 29 |
|
|---|
| 30 | // ignore files named "foo.cs" or files that end in ".html".
|
|---|
| 31 | recursive("some/path", ["foo.cs", "*.html"], function (err, files) {
|
|---|
| 32 | console.log(files);
|
|---|
| 33 | });
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | You can also pass functions which are called to determine whether or not to
|
|---|
| 37 | ignore a file:
|
|---|
| 38 |
|
|---|
| 39 | ```javascript
|
|---|
| 40 | var recursive = require("recursive-readdir");
|
|---|
| 41 |
|
|---|
| 42 | function ignoreFunc(file, stats) {
|
|---|
| 43 | // `file` is the path to the file, and `stats` is an `fs.Stats`
|
|---|
| 44 | // object returned from `fs.lstat()`.
|
|---|
| 45 | return stats.isDirectory() && path.basename(file) == "test";
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | // Ignore files named "foo.cs" and descendants of directories named test
|
|---|
| 49 | recursive("some/path", ["foo.cs", ignoreFunc], function (err, files) {
|
|---|
| 50 | console.log(files);
|
|---|
| 51 | });
|
|---|
| 52 | ```
|
|---|
| 53 |
|
|---|
| 54 | ## Promises
|
|---|
| 55 | You can omit the callback and return a promise instead.
|
|---|
| 56 |
|
|---|
| 57 | ```javascript
|
|---|
| 58 | var recursive = require("recursive-readdir");
|
|---|
| 59 |
|
|---|
| 60 | recursive("some/path").then(
|
|---|
| 61 | function(files) {
|
|---|
| 62 | console.log("files are", files);
|
|---|
| 63 | },
|
|---|
| 64 | function(error) {
|
|---|
| 65 | console.error("something exploded", error);
|
|---|
| 66 | }
|
|---|
| 67 | );
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | The ignore strings support Glob syntax via
|
|---|
| 71 | [minimatch](https://github.com/isaacs/minimatch).
|
|---|