summaryrefslogtreecommitdiff
path: root/deps/npm/lib/prune.js
blob: 39d1c8ffb7571c150d96311b5673e3ba26669483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// prune extraneous packages.

module.exports = prune
module.exports.Pruner = Pruner

prune.usage = 'npm prune [[<@scope>/]<pkg>...] [--production]'

var npm = require('./npm.js')
var log = require('npmlog')
var util = require('util')
var moduleName = require('./utils/module-name.js')
var Installer = require('./install.js').Installer
var isExtraneous = require('./install/is-extraneous.js')
var isDev = require('./install/is-dev-dep.js')
var removeDeps = require('./install/deps.js').removeDeps
var loadExtraneous = require('./install/deps.js').loadExtraneous
var chain = require('slide').chain
var computeMetadata = require('./install/deps.js').computeMetadata

prune.completion = require('./utils/completion/installed-deep.js')

function prune (args, cb) {
  var dryrun = !!npm.config.get('dry-run')
  new Pruner('.', dryrun, args).run(cb)
}

function Pruner (where, dryrun, args) {
  Installer.call(this, where, dryrun, args)
}
util.inherits(Pruner, Installer)

Pruner.prototype.loadAllDepsIntoIdealTree = function (cb) {
  log.silly('uninstall', 'loadAllDepsIntoIdealTree')

  var cg = this.progress['loadIdealTree:loadAllDepsIntoIdealTree']
  var steps = []

  computeMetadata(this.idealTree)
  var self = this
  var excludeDev = npm.config.get('production') || /^prod(uction)?$/.test(npm.config.get('only'))
  function shouldPrune (child) {
    if (isExtraneous(child)) return true
    if (!excludeDev) return false
    var childName = moduleName(child)
    var isChildDev = function (parent) { return isDev(parent, childName) }
    if (child.requiredBy.every(isChildDev)) return true
  }
  function getModuleName (child) {
    // wrapping because moduleName doesn't like extra args and we're called
    // from map.
    return moduleName(child)
  }
  function matchesArg (name) {
    return self.args.length === 0 || self.args.indexOf(name) !== -1
  }
  function nameObj (name) {
    return {name: name}
  }
  var toPrune = this.idealTree.children.filter(shouldPrune).map(getModuleName).filter(matchesArg).map(nameObj)

  steps.push(
    [removeDeps, toPrune, this.idealTree, null, cg.newGroup('removeDeps')],
    [loadExtraneous, this.idealTree, cg.newGroup('loadExtraneous')])
  chain(steps, cb)
}

Pruner.prototype.runPreinstallTopLevelLifecycles = function (cb) { cb() }
Pruner.prototype.runPostinstallTopLevelLifecycles = function (cb) { cb() }