"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/read-package-tree/rpt.js" (11 Apr 2017, 5974 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 asyncForEach (items, todo, done) {
   10   var remaining = items.length
   11   if (remaining === 0) return done()
   12   var seenErr
   13   items.forEach(function (item) {
   14     todo(item, handleComplete)
   15   })
   16   function handleComplete (err) {
   17     if (seenErr) return
   18     if (err) {
   19       seenErr = true
   20       return done(err)
   21     }
   22     if (--remaining === 0) done()
   23   }
   24 }
   25 
   26 function dpath (p) {
   27   if (!p) return ''
   28   if (p.indexOf(process.cwd()) === 0) {
   29     p = p.substr(process.cwd().length + 1)
   30   }
   31   return p
   32 }
   33 
   34 module.exports = rpt
   35 
   36 rpt.Node = Node
   37 rpt.Link = Link
   38 
   39 var ID = 0
   40 function Node (pkg, logical, physical, er, cache) {
   41   if (cache[physical]) return cache[physical]
   42 
   43   if (!(this instanceof Node)) {
   44     return new Node(pkg, logical, physical, er, cache)
   45   }
   46 
   47   cache[physical] = this
   48 
   49   debug(this.constructor.name, dpath(physical), pkg && pkg._id)
   50 
   51   this.id = ID++
   52   this.package = pkg || {}
   53   this.path = logical
   54   this.realpath = physical
   55   this.parent = null
   56   this.isLink = false
   57   this.children = []
   58   this.error = er
   59 }
   60 
   61 Node.prototype.package = null
   62 Node.prototype.path = ''
   63 Node.prototype.realpath = ''
   64 Node.prototype.children = null
   65 Node.prototype.error = null
   66 
   67 function Link (pkg, logical, physical, realpath, er, cache) {
   68   if (cache[physical]) return cache[physical]
   69 
   70   if (!(this instanceof Link)) {
   71     return new Link(pkg, logical, physical, realpath, er, cache)
   72   }
   73 
   74   cache[physical] = this
   75 
   76   debug(this.constructor.name, dpath(physical), pkg && pkg._id)
   77 
   78   this.id = ID++
   79   this.path = logical
   80   this.realpath = realpath
   81   this.package = pkg || {}
   82   this.parent = null
   83   this.target = new Node(this.package, logical, realpath, er, cache)
   84   this.isLink = true
   85   this.children = this.target.children
   86   this.error = er
   87 }
   88 
   89 Link.prototype = Object.create(Node.prototype, {
   90   constructor: { value: Link }
   91 })
   92 Link.prototype.target = null
   93 Link.prototype.realpath = ''
   94 
   95 function loadNode (logical, physical, cache, cb) {
   96   debug('loadNode', dpath(logical))
   97   return fs.realpath(physical, thenReadPackageJson)
   98 
   99   var realpath
  100   function thenReadPackageJson (er, real) {
  101     if (er) {
  102       var node = new Node(null, logical, physical, er, cache)
  103       return cb(null, node)
  104     }
  105     debug('realpath l=%j p=%j real=%j', dpath(logical), dpath(physical), dpath(real))
  106     var pj = path.join(real, 'package.json')
  107     realpath = real
  108     return rpj(pj, thenCreateNode)
  109   }
  110   function thenCreateNode (er, pkg) {
  111     pkg = pkg || null
  112     var node
  113     if (physical === realpath) {
  114       node = new Node(pkg, logical, physical, er, cache)
  115     } else {
  116       node = new Link(pkg, logical, physical, realpath, er, cache)
  117     }
  118 
  119     cb(null, node)
  120   }
  121 }
  122 
  123 function loadChildren (node, cache, filterWith, cb) {
  124   debug('loadChildren', dpath(node.path))
  125   // needed 'cause we process all kids async-like and errors
  126   // short circuit, so we have to be sure that after an error
  127   // the cbs from other kids don't result in calling cb a second
  128   // (or more) time.
  129   cb = once(cb)
  130   var nm = path.join(node.path, 'node_modules')
  131   var rm
  132   return fs.realpath(path.join(node.path, 'node_modules'), thenReaddir)
  133 
  134   function thenReaddir (er, real_nm) {
  135     if (er) return cb(null, node)
  136     rm = real_nm
  137     readdir(nm, thenLoadKids)
  138   }
  139 
  140   function thenLoadKids (er, kids) {
  141     // If there are no children, that's fine, just return
  142     if (er) return cb(null, node)
  143 
  144     kids = kids.filter(function (kid) {
  145       return kid[0] !== '.' && (!filterWith || filterWith(node, kid))
  146     })
  147 
  148     asyncForEach(kids, thenLoadNode, thenSortChildren)
  149   }
  150   function thenLoadNode (kid, done) {
  151     var kidPath = path.join(nm, kid)
  152     var kidRealPath = path.join(rm, kid)
  153     loadNode(kidPath, kidRealPath, cache, andAddNode(done))
  154   }
  155   function andAddNode (done) {
  156     return function (er, kid) {
  157       if (er) return done(er)
  158       node.children.push(kid)
  159       kid.parent = node
  160       done()
  161     }
  162   }
  163   function thenSortChildren (er) {
  164     sortChildren(node)
  165     cb(er, node)
  166   }
  167 }
  168 
  169 function sortChildren (node) {
  170   node.children = node.children.sort(function (a, b) {
  171     a = a.package.name ? a.package.name.toLowerCase() : a.path
  172     b = b.package.name ? b.package.name.toLowerCase() : b.path
  173     return a > b ? 1 : -1
  174   })
  175 }
  176 
  177 function loadTree (node, did, cache, filterWith, cb) {
  178   debug('loadTree', dpath(node.path), !!cache[node.path])
  179 
  180   if (did[node.realpath]) {
  181     return dz(cb)(null, node)
  182   }
  183 
  184   did[node.realpath] = true
  185 
  186   // needed 'cause we process all kids async-like and errors
  187   // short circuit, so we have to be sure that after an error
  188   // the cbs from other kids don't result in calling cb a second
  189   // (or more) time.
  190   cb = once(cb)
  191   return loadChildren(node, cache, filterWith, thenProcessChildren)
  192 
  193   function thenProcessChildren (er, node) {
  194     if (er) return cb(er)
  195 
  196     var kids = node.children.filter(function (kid) {
  197       return !did[kid.realpath]
  198     })
  199 
  200     return asyncForEach(kids, loadTreeForKid, cb)
  201   }
  202   function loadTreeForKid (kid, done) {
  203     loadTree(kid, did, cache, filterWith, done)
  204   }
  205 }
  206 
  207 function rpt (root, filterWith, cb) {
  208   if (!cb) {
  209     cb = filterWith
  210     filterWith = null
  211   }
  212   var cache = Object.create(null)
  213   var topErr
  214   var tree
  215   return fs.realpath(root, thenLoadNode)
  216 
  217   function thenLoadNode (er, realRoot) {
  218     if (er) return cb(er)
  219     debug('rpt', dpath(realRoot))
  220     loadNode(root, realRoot, cache, thenLoadTree)
  221   }
  222   function thenLoadTree(er, node) {
  223     // even if there's an error, it's fine, as long as we got a node
  224     if (node) {
  225       topErr = er
  226       tree = node
  227       loadTree(node, {}, cache, filterWith, thenHandleErrors)
  228     } else {
  229       cb(er)
  230     }
  231   }
  232   function thenHandleErrors (er) {
  233     cb(topErr && topErr.code !== 'ENOENT' ? topErr : er, tree)
  234   }
  235 }