|
| 1 | +var fs = require ('fs') |
| 2 | +var rpj = require ('read-package-json') |
| 3 | +var path = require ('path') |
| 4 | +var dz = require ('dezalgo') |
| 5 | +var once = require ('once') |
| 6 | +var readdir = require ('readdir-scoped-modules') |
| 7 | +var debug = require ('debuglog') ('rpt') |
| 8 | + |
| 9 | +function dpath (p) { |
| 10 | + if (p . indexOf (process.cwd()) === 0) |
| 11 | + p = p . substr (process.cwd().length + 1) |
| 12 | + return p |
| 13 | +} |
| 14 | + |
| 15 | +module . exports = rpt |
| 16 | + |
| 17 | +function Node (package, path) { |
| 18 | + if (! (this instanceof Node)) |
| 19 | + return new Node (package, path) |
| 20 | + |
| 21 | + debug ('Node', dpath (path), package && package . _id) |
| 22 | + this . package = package |
| 23 | + this . path = path |
| 24 | + this . children = [] |
| 25 | +} |
| 26 | +Node . prototype . package = null |
| 27 | +Node . prototype . path = '' |
| 28 | +Node . prototype . children = null |
| 29 | + |
| 30 | +function loadNode (p, cb) { |
| 31 | + debug ('loadNode', dpath (p)) |
| 32 | + fs . realpath (p, function (er, real) { |
| 33 | + if (er) |
| 34 | + return cb (er) |
| 35 | + debug ('realpath p=%j real=%j', dpath (p), dpath (real)) |
| 36 | + var pj = path . resolve (real, 'package.json') |
| 37 | + rpj (pj, function (er, package) { |
| 38 | + cb (er, new Node (package || null, real)) |
| 39 | + }) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +function loadChildren (node, cb) { |
| 44 | + debug ('loadChildren', dpath(node . path)) |
| 45 | + // don't let it be called more than once |
| 46 | + cb = once (cb) |
| 47 | + var nm = path . resolve (node . path, 'node_modules') |
| 48 | + readdir (nm, function (er, kids) { |
| 49 | + // If there are no children, that's fine, just return |
| 50 | + if (er) |
| 51 | + return cb (null, node) |
| 52 | + |
| 53 | + kids = kids . filter (function (kid) { |
| 54 | + return kid !== '.bin' |
| 55 | + }) |
| 56 | + |
| 57 | + var l = kids . length |
| 58 | + if (l === 0) |
| 59 | + return cb (null, node) |
| 60 | + |
| 61 | + kids . forEach (function (kid) { |
| 62 | + var p = path . resolve (nm, kid) |
| 63 | + loadNode (p, then) |
| 64 | + }) |
| 65 | + |
| 66 | + function then (er, kid) { |
| 67 | + if (er) |
| 68 | + return cb (er) |
| 69 | + node . children . push (kid) |
| 70 | + if (-- l === 0) { |
| 71 | + sortChildren (node) |
| 72 | + return cb (null, node) |
| 73 | + } |
| 74 | + } |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +function sortChildren (node) { |
| 79 | + node . children = node . children . sort (function (a, b) { |
| 80 | + a = a . package . name . toLowerCase () |
| 81 | + b = b . package . name . toLowerCase () |
| 82 | + return a > b ? 1 : -1 |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +function loadTree (node, cb, did) { |
| 87 | + did = did || Object . create (null) |
| 88 | + debug ('loadTree', dpath (node . path), !! did [ node . path ]) |
| 89 | + if (did [ node . path ]) |
| 90 | + return dz (cb) (null, node) |
| 91 | + |
| 92 | + did [ node . path ] = true |
| 93 | + |
| 94 | + cb = once (cb) |
| 95 | + loadChildren (node, function (er, node) { |
| 96 | + if (er) |
| 97 | + return cb (er) |
| 98 | + |
| 99 | + var kids = node . children . filter (function (kid) { |
| 100 | + return ! did [ kid . path ] |
| 101 | + }) |
| 102 | + var l = kids . length |
| 103 | + if (l === 0) |
| 104 | + return cb (null, node) |
| 105 | + |
| 106 | + kids . forEach (function (kid) { |
| 107 | + loadTree (kid, then, did) |
| 108 | + }) |
| 109 | + |
| 110 | + function then (er, kid) { |
| 111 | + if (er) |
| 112 | + return cb (er) |
| 113 | + |
| 114 | + if (--l === 0) |
| 115 | + cb (null, node) |
| 116 | + } |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +function rpt (root, cb) { |
| 121 | + debug ('rpt', dpath (root)) |
| 122 | + loadNode (root, function (er, node) { |
| 123 | + // if there's an error, it's fine, as long as we got a node |
| 124 | + if (!node) |
| 125 | + return cb (er) |
| 126 | + loadTree (node, cb) |
| 127 | + }) |
| 128 | +} |
0 commit comments