Remove find.js, use glob instead

This commit is contained in:
isaacs
2012-06-11 20:31:31 -07:00
parent e4534d8047
commit aaf7e0565f
2 changed files with 8 additions and 64 deletions

View File

@@ -1,21 +1,19 @@
module.exports = fileCompletion
var find = require("../find.js")
, mkdir = require("mkdirp")
var mkdir = require("mkdirp")
, path = require("path")
, fs = require("graceful-fs")
, glob = require("glob")
function fileCompletion (root, req, depth, cb) {
if (typeof cb !== "function") cb = depth, depth = Infinity
mkdir(root, function (er) {
if (er) return cb(er)
function dirFilter (f, type) {
// return anything that is a file,
// or not exactly the req.
return type !== "dir" ||
( f && f !== path.join(root, req)
&& f !== path.join(root, req) + "/" )
}
find(path.join(root, req), dirFilter, depth, function (er, files) {
// can be either exactly the req, or a descendent
var pattern = root + "/{" + req + "," + req + "/**/*}"
, opts = { mark: true, dot: true, maxDepth: depth }
glob(pattern, opts, function (er, files) {
if (er) return cb(er)
return cb(null, (files || []).map(function (f) {
return path.join(req, f.substr(root.length + 1)
@@ -26,4 +24,3 @@ function fileCompletion (root, req, depth, cb) {
})
})
}

View File

@@ -1,53 +0,0 @@
// walks a set of directories recursively, and returns
// the list of files that match the filter, if one is
// provided.
module.exports = find
var fs = require("graceful-fs")
, asyncMap = require("slide").asyncMap
, path = require("path")
function find (dir, filter, depth, cb) {
if (typeof cb !== "function") cb = depth, depth = Infinity
if (typeof cb !== "function") cb = filter, filter = null
if (filter instanceof RegExp) filter = reFilter(filter)
if (typeof filter === "string") filter = strFilter(filter)
if (!Array.isArray(dir)) dir = [dir]
if (!filter) filter = nullFilter
asyncMap(dir, findDir(filter, depth), cb)
}
function findDir (filter, depth) { return function (dir, cb) {
fs.lstat(dir, function (er, stats) {
// don't include missing files, but don't abort either
if (er) return cb()
if (!stats.isDirectory()) return findFile(dir, filter, depth)("", cb)
var found = []
if (!filter || filter(dir, "dir")) found.push(dir+"/")
if (depth <= 0) return cb(null, found)
cb = (function (cb) { return function (er, f) {
cb(er, found.concat(f))
}})(cb)
fs.readdir(dir, function (er, files) {
if (er) return cb(er)
asyncMap(files, findFile(dir, filter, depth - 1), cb)
})
})
}}
function findFile (dir, filter, depth) { return function (f, cb) {
f = path.join(dir, f)
fs.lstat(f, function (er, s) {
// don't include missing files, but don't abort either
if (er) return cb(null, [])
if (s.isDirectory()) return find(f, filter, depth, cb)
if (!filter || filter(f, "file")) cb(null, f)
else cb(null, [])
})
}}
function reFilter (re) { return function (f, type) {
return nullFilter(f, type) && f.match(re)
}}
function strFilter (s) { return function (f, type) {
return nullFilter(f, type) && f.indexOf(s) === 0
}}
function nullFilter (f, type) { return type === "file" && f }