1 | # findit
|
2 |
|
3 | Recursively walk directory trees. Think `/usr/bin/find`.
|
4 |
|
5 | [![build status](https://secure.travis-ci.org/substack/node-findit.png)](http://travis-ci.org/substack/node-findit)
|
6 |
|
7 | # example
|
8 |
|
9 | ``` js
|
10 | var finder = require('findit')(process.argv[2] || '.');
|
11 | var path = require('path');
|
12 |
|
13 | finder.on('directory', function (dir, stat, stop) {
|
14 | var base = path.basename(dir);
|
15 | if (base === '.git' || base === 'node_modules') stop()
|
16 | else console.log(dir + '/')
|
17 | });
|
18 |
|
19 | finder.on('file', function (file, stat) {
|
20 | console.log(file);
|
21 | });
|
22 |
|
23 | finder.on('link', function (link, stat) {
|
24 | console.log(link);
|
25 | });
|
26 | ```
|
27 |
|
28 | # methods
|
29 |
|
30 | ``` js
|
31 | var find = require('findit')
|
32 | ```
|
33 |
|
34 | ## var finder = find(basedir, opts)
|
35 |
|
36 | Return an event emitter `finder` that performs a recursive walk starting at
|
37 | `basedir`.
|
38 |
|
39 | If you set `opts.followSymlinks`, symlinks will be followed. Otherwise, a
|
40 | `'link'` event will fire but symlinked directories will not be walked.
|
41 |
|
42 | If `basedir` is actually a non-directory regular file, findit emits a single
|
43 | "file" event for it then emits "end".
|
44 |
|
45 | You can optionally specify a custom
|
46 | [fs](http://nodejs.org/docs/latest/api/fs.html)
|
47 | implementation with `opts.fs`. `opts.fs` should implement:
|
48 |
|
49 | * `opts.fs.readdir(dir, cb)`
|
50 | * `opts.fs.lstat(dir, cb)`
|
51 | * `opts.fs.readlink(dir, cb)` - optional if your stat objects from
|
52 | `opts.fs.lstat` never return true for `stat.isSymbolicLink()`
|
53 |
|
54 | ## finder.stop()
|
55 |
|
56 | Stop the traversal. A `"stop"` event will fire and then no more events will
|
57 | fire.
|
58 |
|
59 | # events
|
60 |
|
61 | ## finder.on('path', function (file, stat) {})
|
62 |
|
63 | For each file, directory, and symlink `file`, this event fires.
|
64 |
|
65 | ## finder.on('file', function (file, stat) {})
|
66 |
|
67 | For each file, this event fires.
|
68 |
|
69 | ## finder.on('directory', function (dir, stat, stop) {})
|
70 |
|
71 | For each directory, this event fires with the path `dir`.
|
72 |
|
73 | Your callback may call `stop()` on the first tick to tell findit to stop walking
|
74 | the current directory.
|
75 |
|
76 | ## finder.on('link', function (file, stat) {})
|
77 |
|
78 | For each symlink, this event fires.
|
79 |
|
80 | ## finder.on('readlink', function (src, dst) {})
|
81 |
|
82 | Every time a symlink is read when `opts.followSymlinks` is on, this event fires.
|
83 |
|
84 | ## finder.on('end', function () {})
|
85 |
|
86 | When the recursive walk is complete unless `finder.stop()` was called, this
|
87 | event fires.
|
88 |
|
89 | ## finder.on('stop', function () {})
|
90 |
|
91 | When `finder.stop()` is called, this event fires.
|
92 |
|
93 | ## finder.on('error', function (err) {})
|
94 |
|
95 | Whenever there is an error, this event fires. You can choose to ignore errors or
|
96 | stop the traversal using `finder.stop()`.
|
97 |
|
98 | You can always get the source of the error by checking `err.path`.
|
99 |
|
100 | # install
|
101 |
|
102 | With [npm](https://npmjs.org) do:
|
103 |
|
104 | ```
|
105 | npm install findit
|
106 | ```
|
107 |
|
108 | # license
|
109 |
|
110 | MIT
|