-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
41 lines (35 loc) · 898 Bytes
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var fs = require('fs')
, walk = require('walk')
, path = require('path')
, cwd = process.cwd()
;
var find = function (name, opts, cb) {
if (!cb) {cb = opts; opts = {}}
if (!opts.ignore) {opts.ignore = []}
if (!opts.dir) {opts.dir = cwd}
var ignoreDirs = opts.ignore
, startDir = opts.dir
, found
;
walker = walk.walk(startDir);
walker.on('directories', function (root, dirs, next) {
dirs.forEach(function (dir) {
ignoreDirs.forEach(function (ignoreDir, idx) {
if (root.indexOf(ignoreDir) > -1) {
next()
} else if (dir.name == name) {
walker.emit('found', path.join(root, name))
}
})
})
next()
})
walker.once('found', function (dir) {
found = dir
})
walker.once('end', function () {
if (!found) { console.log('Unable to find', name) }
cb(found)
})
}
exports.find = find