"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/dedupe.js" (7 Feb 2017, 5398 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 var util = require('util')
    2 var path = require('path')
    3 var validate = require('aproba')
    4 var without = require('lodash.without')
    5 var asyncMap = require('slide').asyncMap
    6 var chain = require('slide').chain
    7 var npa = require('npm-package-arg')
    8 var log = require('npmlog')
    9 var npm = require('./npm.js')
   10 var Installer = require('./install.js').Installer
   11 var findRequirement = require('./install/deps.js').findRequirement
   12 var earliestInstallable = require('./install/deps.js').earliestInstallable
   13 var checkPermissions = require('./install/check-permissions.js')
   14 var decomposeActions = require('./install/decompose-actions.js')
   15 var loadExtraneous = require('./install/deps.js').loadExtraneous
   16 var filterInvalidActions = require('./install/filter-invalid-actions.js')
   17 var recalculateMetadata = require('./install/deps.js').recalculateMetadata
   18 var sortActions = require('./install/diff-trees.js').sortActions
   19 var moduleName = require('./utils/module-name.js')
   20 var packageId = require('./utils/package-id.js')
   21 var childPath = require('./utils/child-path.js')
   22 var usage = require('./utils/usage')
   23 
   24 module.exports = dedupe
   25 module.exports.Deduper = Deduper
   26 
   27 dedupe.usage = usage(
   28   'dedupe',
   29   'npm dedupe'
   30 )
   31 
   32 function dedupe (args, cb) {
   33   validate('AF', arguments)
   34   // the /path/to/node_modules/..
   35   var where = path.resolve(npm.dir, '..')
   36   var dryrun = false
   37   if (npm.command.match(/^find/)) dryrun = true
   38   if (npm.config.get('dry-run')) dryrun = true
   39 
   40   new Deduper(where, dryrun).run(cb)
   41 }
   42 
   43 function Deduper (where, dryrun) {
   44   validate('SB', arguments)
   45   Installer.call(this, where, dryrun, [])
   46   this.noPackageJsonOk = true
   47   this.topLevelLifecycles = false
   48 }
   49 util.inherits(Deduper, Installer)
   50 
   51 Deduper.prototype.normalizeTree = function (log, cb) {
   52   validate('OF', arguments)
   53   log.silly('dedupe', 'normalizeTree')
   54   // If we're looking globally only look at the one package we're operating on
   55   if (npm.config.get('global')) {
   56     var args = this.args
   57     this.currentTree.children = this.currentTree.children.filter(function (child) {
   58       return args.filter(function (arg) { return arg === moduleName(child) }).length
   59     })
   60   }
   61   Installer.prototype.normalizeTree.call(this, log, cb)
   62 }
   63 
   64 Deduper.prototype.loadIdealTree = function (cb) {
   65   validate('F', arguments)
   66   log.silly('install', 'loadIdealTree')
   67 
   68   var self = this
   69   chain([
   70     [this.newTracker(this.progress.loadIdealTree, 'cloneCurrentTree')],
   71     [this, this.cloneCurrentTreeToIdealTree],
   72     [this, this.finishTracker, 'cloneCurrentTree'],
   73 
   74     [this.newTracker(this.progress.loadIdealTree, 'loadAllDepsIntoIdealTree', 10)],
   75     [ function (next) {
   76       loadExtraneous(self.idealTree, self.progress.loadAllDepsIntoIdealTree, next)
   77     } ],
   78     [this, this.finishTracker, 'loadAllDepsIntoIdealTree'],
   79 
   80     [this, function (next) { recalculateMetadata(this.idealTree, log, next) }]
   81   ], cb)
   82 }
   83 
   84 Deduper.prototype.generateActionsToTake = function (cb) {
   85   validate('F', arguments)
   86   log.silly('dedupe', 'generateActionsToTake')
   87   chain([
   88     [this.newTracker(log, 'hoist', 1)],
   89     [hoistChildren, this.idealTree, this.differences],
   90     [this, this.finishTracker, 'hoist'],
   91     [this.newTracker(log, 'sort-actions', 1)],
   92     [this, function (next) {
   93       this.differences = sortActions(this.differences)
   94       next()
   95     }],
   96     [this, this.finishTracker, 'sort-actions'],
   97     [filterInvalidActions, this.where, this.differences],
   98     [checkPermissions, this.differences],
   99     [decomposeActions, this.differences, this.todo]
  100   ], cb)
  101 }
  102 
  103 function move (node, hoistTo, diff) {
  104   node.parent.children = without(node.parent.children, node)
  105   hoistTo.children.push(node)
  106   node.fromPath = node.path
  107   node.path = childPath(hoistTo.path, node)
  108   node.parent = hoistTo
  109   if (!diff.filter(function (action) { return action[0] === 'move' && action[1] === node }).length) {
  110     diff.push(['move', node])
  111   }
  112 }
  113 
  114 function moveRemainingChildren (node, diff) {
  115   node.children.forEach(function (child) {
  116     move(child, node, diff)
  117     moveRemainingChildren(child, diff)
  118   })
  119 }
  120 
  121 function remove (child, diff, done) {
  122   remove_(child, diff, {}, done)
  123 }
  124 
  125 function remove_ (child, diff, seen, done) {
  126   if (seen[child.path]) return done()
  127   seen[child.path] = true
  128   diff.push(['remove', child])
  129   child.parent.children = without(child.parent.children, child)
  130   asyncMap(child.children, function (child, next) {
  131     remove_(child, diff, seen, next)
  132   }, done)
  133 }
  134 
  135 function hoistChildren (tree, diff, next) {
  136   hoistChildren_(tree, diff, {}, next)
  137 }
  138 
  139 function hoistChildren_ (tree, diff, seen, next) {
  140   validate('OAOF', arguments)
  141   if (seen[tree.path]) return next()
  142   seen[tree.path] = true
  143   asyncMap(tree.children, function (child, done) {
  144     if (!tree.parent) return hoistChildren_(child, diff, seen, done)
  145     var better = findRequirement(tree.parent, moduleName(child), child.package._requested || npa(packageId(child)))
  146     if (better) {
  147       return chain([
  148         [remove, child, diff],
  149         [recalculateMetadata, tree, log]
  150       ], done)
  151     }
  152     var hoistTo = earliestInstallable(tree, tree.parent, child.package)
  153     if (hoistTo) {
  154       move(child, hoistTo, diff)
  155       chain([
  156         [recalculateMetadata, hoistTo, log],
  157         [hoistChildren_, child, diff, seen],
  158         [ function (next) {
  159           moveRemainingChildren(child, diff)
  160           next()
  161         } ]
  162       ], done)
  163     } else {
  164       done()
  165     }
  166   }, next)
  167 }