]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/read-installed/node_modules/readdir-scoped-modules/readdir.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / read-installed / node_modules / readdir-scoped-modules / readdir.js
1 var fs = require ('graceful-fs')
2 var dz = require ('dezalgo')
3 var once = require ('once')
4 var path = require ('path')
5 var debug = require ('debuglog') ('rds')
6
7 module . exports = readdir
8
9 function readdir (dir, cb) {
10   fs . readdir (dir, function (er, kids) {
11     if (er)
12       return cb (er)
13
14     debug ('dir=%j, kids=%j', dir, kids)
15     readScopes (dir, kids, function (er, data) {
16       if (er)
17         return cb (er)
18
19       // Sort for bonus consistency points
20       data = data . sort (function (a, b) {
21         return a > b ? 1 : -1
22       })
23
24       return cb (null, data)
25     })
26   })
27 }
28
29 // Turn [ 'a', '@scope' ] into
30 // ['a', '@scope/foo', '@scope/bar']
31 function readScopes (root, kids, cb) {
32   var scopes = kids . filter (function (kid) {
33     return kid . charAt (0) === '@'
34   })
35
36   kids = kids . filter (function (kid) {
37     return kid . charAt (0) !== '@'
38   })
39
40   debug ('scopes=%j', scopes)
41
42   if (scopes . length === 0)
43     dz (cb) (null, kids) // prevent maybe-sync zalgo release
44
45   cb = once (cb)
46   var l = scopes . length
47   scopes . forEach (function (scope) {
48     var scopedir = path . resolve (root, scope)
49     debug ('root=%j scope=%j scopedir=%j', root, scope, scopedir)
50     fs . readdir (scopedir, then . bind (null, scope))
51   })
52
53   function then (scope, er, scopekids) {
54     if (er)
55       return cb (er)
56
57     // XXX: Not sure how old this node bug is. Maybe superstition?
58     scopekids = scopekids . filter (function (scopekid) {
59       return !(scopekid === '.' || scopekid === '..' || !scopekid)
60     })
61
62     kids . push . apply (kids, scopekids . map (function (scopekid) {
63       return scope + '/' + scopekid
64     }))
65
66     debug ('scope=%j scopekids=%j kids=%j', scope, scopekids, kids)
67
68     if (--l === 0)
69       cb (null, kids)
70   }
71 }