2011-11-22 01:48:45 +08:00
|
|
|
module.exports = fileCompletion
|
|
|
|
|
2012-06-16 01:00:30 +08:00
|
|
|
var mkdir = require("mkdirp")
|
2011-11-22 01:48:45 +08:00
|
|
|
, path = require("path")
|
2012-06-16 01:00:30 +08:00
|
|
|
, glob = require("glob")
|
2011-11-22 01:48:45 +08:00
|
|
|
|
|
|
|
function fileCompletion (root, req, depth, cb) {
|
|
|
|
if (typeof cb !== "function") cb = depth, depth = Infinity
|
|
|
|
mkdir(root, function (er) {
|
|
|
|
if (er) return cb(er)
|
2012-06-16 01:00:30 +08:00
|
|
|
|
|
|
|
// 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) {
|
2011-11-22 01:48:45 +08:00
|
|
|
if (er) return cb(er)
|
|
|
|
return cb(null, (files || []).map(function (f) {
|
2015-01-09 06:37:26 +08:00
|
|
|
var tail = f.substr(root.length + 1).replace(/^\//, "")
|
|
|
|
return path.join(req, tail)
|
2011-11-22 01:48:45 +08:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|