]> gerrit.simantics Code Review - simantics/district.git/blob - file-completion.js
6ce2f83467d012030a87ad15fa444f56886a330e
[simantics/district.git] / file-completion.js
1 module.exports = fileCompletion
2
3 var mkdir = require("mkdirp")
4   , path = require("path")
5   , glob = require("glob")
6
7 function fileCompletion (root, req, depth, cb) {
8   if (typeof cb !== "function") cb = depth, depth = Infinity
9   mkdir(root, function (er) {
10     if (er) return cb(er)
11
12     // can be either exactly the req, or a descendent
13     var pattern = root + "/{" + req + "," + req + "/**/*}"
14       , opts = { mark: true, dot: true, maxDepth: depth }
15     glob(pattern, opts, function (er, files) {
16       if (er) return cb(er)
17       return cb(null, (files || []).map(function (f) {
18         var tail = f.substr(root.length + 1).replace(/^\//, "")
19         return path.join(req, tail)
20       }))
21     })
22   })
23 }