file-completion.js 661 B

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