Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove find.js, use glob instead

  • Loading branch information...
commit aaf7e0565ffded02bdd3f3c75a471989a3e3a26b 1 parent e4534d8
@isaacs isaacs authored
Showing with 8 additions and 64 deletions.
  1. +8 −11 lib/utils/completion/file-completion.js
  2. +0 −53 lib/utils/find.js
View
19 lib/utils/completion/file-completion.js
@@ -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
53 lib/utils/find.js
@@ -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 }

0 comments on commit aaf7e05

Please sign in to comment.
Something went wrong with that request. Please try again.