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')
7 module . exports = readdir
9 function readdir (dir, cb) {
10 fs . readdir (dir, function (er, kids) {
14 debug ('dir=%j, kids=%j', dir, kids)
15 readScopes (dir, kids, function (er, data) {
19 // Sort for bonus consistency points
20 data = data . sort (function (a, b) {
24 return cb (null, data)
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) === '@'
36 kids = kids . filter (function (kid) {
37 return kid . charAt (0) !== '@'
40 debug ('scopes=%j', scopes)
42 if (scopes . length === 0)
43 dz (cb) (null, kids) // prevent maybe-sync zalgo release
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))
53 function then (scope, er, scopekids) {
57 // XXX: Not sure how old this node bug is. Maybe superstition?
58 scopekids = scopekids . filter (function (scopekid) {
59 return !(scopekid === '.' || scopekid === '..' || !scopekid)
62 kids . push . apply (kids, scopekids . map (function (scopekid) {
63 return scope + '/' + scopekid
66 debug ('scope=%j scopekids=%j kids=%j', scope, scopekids, kids)