• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1module.exports = fileCompletion
2
3var mkdir = require('mkdirp')
4var glob = require('glob')
5
6function fileCompletion (root, req, depth, cb) {
7  if (typeof cb !== 'function') {
8    cb = depth
9    depth = Infinity
10  }
11  mkdir(root, function (er) {
12    if (er) return cb(er)
13
14    // can be either exactly the req, or a descendent
15    var pattern = root + '/{' + req + ',' + req + '/**/*}'
16    var opts = { mark: true, dot: true, maxDepth: depth }
17    glob(pattern, opts, function (er, files) {
18      if (er) return cb(er)
19      return cb(null, (files || []).map(function (f) {
20        return f.substr(root.length + 1).replace(/^\/|\/$/g, '')
21      }))
22    })
23  })
24}
25