UNPKG

2.67 kBMarkdownView Raw
1# findit
2
3Recursively 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
10var finder = require('findit')(process.argv[2] || '.');
11var path = require('path');
12
13finder.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
19finder.on('file', function (file, stat) {
20 console.log(file);
21});
22
23finder.on('link', function (link, stat) {
24 console.log(link);
25});
26```
27
28# methods
29
30``` js
31var find = require('findit')
32```
33
34## var finder = find(basedir, opts)
35
36Return an event emitter `finder` that performs a recursive walk starting at
37`basedir`.
38
39If you set `opts.followSymlinks`, symlinks will be followed. Otherwise, a
40`'link'` event will fire but symlinked directories will not be walked.
41
42If `basedir` is actually a non-directory regular file, findit emits a single
43"file" event for it then emits "end".
44
45You can optionally specify a custom
46[fs](http://nodejs.org/docs/latest/api/fs.html)
47implementation 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
56Stop the traversal. A `"stop"` event will fire and then no more events will
57fire.
58
59# events
60
61## finder.on('path', function (file, stat) {})
62
63For each file, directory, and symlink `file`, this event fires.
64
65## finder.on('file', function (file, stat) {})
66
67For each file, this event fires.
68
69## finder.on('directory', function (dir, stat, stop) {})
70
71For each directory, this event fires with the path `dir`.
72
73Your callback may call `stop()` on the first tick to tell findit to stop walking
74the current directory.
75
76## finder.on('link', function (file, stat) {})
77
78For each symlink, this event fires.
79
80## finder.on('readlink', function (src, dst) {})
81
82Every time a symlink is read when `opts.followSymlinks` is on, this event fires.
83
84## finder.on('end', function () {})
85
86When the recursive walk is complete unless `finder.stop()` was called, this
87event fires.
88
89## finder.on('stop', function () {})
90
91When `finder.stop()` is called, this event fires.
92
93## finder.on('error', function (err) {})
94
95Whenever there is an error, this event fires. You can choose to ignore errors or
96stop the traversal using `finder.stop()`.
97
98You can always get the source of the error by checking `err.path`.
99
100# install
101
102With [npm](https://npmjs.org) do:
103
104```
105npm install findit
106```
107
108# license
109
110MIT