"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/install/mutate-into-logical-tree.js" (8 Mar 2017, 4325 Bytes) of archive /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 'use strict'
    2 var union = require('lodash.union')
    3 var without = require('lodash.without')
    4 var validate = require('aproba')
    5 var flattenTree = require('./flatten-tree.js')
    6 var isExtraneous = require('./is-extraneous.js')
    7 var validateAllPeerDeps = require('./deps.js').validateAllPeerDeps
    8 var packageId = require('../utils/package-id.js')
    9 var moduleName = require('../utils/module-name.js')
   10 
   11 // Return true if tree is a part of a cycle that:
   12 //   A) Never connects to the top of the tree
   13 //   B) Has not not had a point in the cycle arbitraryly declared its top
   14 //      yet.
   15 function isDisconnectedCycle (tree, seen) {
   16   if (!seen) seen = {}
   17   if (tree.isTop || tree.cycleTop || tree.requiredBy.length === 0) {
   18     return false
   19   } else if (seen[tree.path]) {
   20     return true
   21   } else {
   22     seen[tree.path] = true
   23     return tree.requiredBy.every(function (node) {
   24       return isDisconnectedCycle(node, Object.create(seen))
   25     })
   26   }
   27 }
   28 
   29 var mutateIntoLogicalTree = module.exports = function (tree) {
   30   validate('O', arguments)
   31 
   32   validateAllPeerDeps(tree, function (tree, pkgname, version) {
   33     if (!tree.missingPeers) tree.missingPeers = {}
   34     tree.missingPeers[pkgname] = version
   35   })
   36 
   37   var flat = flattenTree(tree)
   38 
   39   Object.keys(flat).sort().forEach(function (flatname) {
   40     var node = flat[flatname]
   41     if (!node.requiredBy.length) return
   42 
   43     if (node.parent) {
   44       // If a node is a cycle that never reaches the root of the logical
   45       // tree then we'll leave it attached to the root, or else it
   46       // would go missing. Further we'll note that this is the node in the
   47       // cycle that we picked arbitrarily to be the one attached to the root.
   48       // others will fall
   49       if (isDisconnectedCycle(node)) {
   50         node.cycleTop = true
   51       // Nor do we want to disconnect non-cyclical extraneous modules from the tree.
   52       } else if (node.requiredBy.length) {
   53         // regular deps though, we do, as we're moving them into the capable
   54         // hands of the modules that require them.
   55         node.parent.children = without(node.parent.children, node)
   56       }
   57     }
   58 
   59     node.requiredBy.forEach(function (parentNode) {
   60       parentNode.children = union(parentNode.children, [node])
   61     })
   62   })
   63   return tree
   64 }
   65 
   66 module.exports.asReadInstalled = function (tree) {
   67   mutateIntoLogicalTree(tree)
   68   return translateTree(tree)
   69 }
   70 
   71 function translateTree (tree) {
   72   return translateTree_(tree, {})
   73 }
   74 
   75 function translateTree_ (tree, seen) {
   76   var pkg = tree.package
   77   if (seen[tree.path]) return pkg
   78   seen[tree.path] = pkg
   79   if (pkg._dependencies) return pkg
   80   pkg._dependencies = pkg.dependencies
   81   pkg.dependencies = {}
   82   tree.children.forEach(function (child) {
   83     pkg.dependencies[moduleName(child)] = translateTree_(child, seen)
   84   })
   85 
   86   function markMissing (name, requiredBy) {
   87     if (pkg.dependencies[name]) {
   88       if (pkg.dependencies[name].missing) return
   89       pkg.dependencies[name].invalid = true
   90       pkg.dependencies[name].realName = name
   91       pkg.dependencies[name].extraneous = false
   92     } else {
   93       pkg.dependencies[name] = {
   94         requiredBy: requiredBy,
   95         missing: true,
   96         optional: !!pkg.optionalDependencies[name]
   97       }
   98     }
   99   }
  100 
  101   Object.keys(tree.missingDeps).forEach(function (name) {
  102     markMissing(name, tree.missingDeps[name])
  103   })
  104   Object.keys(tree.missingDevDeps).forEach(function (name) {
  105     markMissing(name, tree.missingDevDeps[name])
  106   })
  107   var checkForMissingPeers = (tree.parent ? [] : [tree]).concat(tree.children)
  108   checkForMissingPeers.filter(function (child) {
  109     return child.missingPeers
  110   }).forEach(function (child) {
  111     Object.keys(child.missingPeers).forEach(function (pkgname) {
  112       var version = child.missingPeers[pkgname]
  113       var peerPkg = pkg.dependencies[pkgname]
  114       if (!peerPkg) {
  115         peerPkg = pkg.dependencies[pkgname] = {
  116           _id: pkgname + '@' + version,
  117           name: pkgname,
  118           version: version
  119         }
  120       }
  121       if (!peerPkg.peerMissing) peerPkg.peerMissing = []
  122       peerPkg.peerMissing.push({
  123         requiredBy: packageId(child),
  124         requires: pkgname + '@' + version
  125       })
  126     })
  127   })
  128   pkg.path = tree.path
  129 
  130   pkg.error = tree.error
  131   pkg.extraneous = isExtraneous(tree)
  132   if (tree.target && tree.parent && !tree.parent.target) pkg.link = tree.realpath
  133   return pkg
  134 }