0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/deps/npm/lib/prune.js
Rebecca Turner 41923c0c07 deps: upgrade npm to 3.3.6
PR-URL: https://github.com/nodejs/node/pull/3310
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2015-10-22 13:56:09 -04:00

55 lines
1.5 KiB
JavaScript

// prune extraneous packages.
module.exports = prune
prune.usage = 'npm prune [[<@scope>/]<pkg>...] [--production]'
var readInstalled = require('read-installed')
var npm = require('./npm.js')
var path = require('path')
var readJson = require('read-package-json')
var log = require('npmlog')
prune.completion = require('./utils/completion/installed-deep.js')
function prune (args, cb) {
// check if is a valid package.json file
var jsonFile = path.resolve(npm.dir, '..', 'package.json')
readJson(jsonFile, log.warn, function (er) {
if (er) return cb(er)
next()
})
function next () {
var opt = {
depth: npm.config.get('depth'),
dev: !npm.config.get('production') || npm.config.get('dev')
}
readInstalled(npm.prefix, opt, function (er, data) {
if (er) return cb(er)
prune_(args, data, cb)
})
}
}
function prune_ (args, data, cb) {
npm.commands.unbuild(prunables(args, data, []), cb)
}
function prunables (args, data, seen) {
var deps = data.dependencies || {}
return Object.keys(deps).map(function (d) {
if (typeof deps[d] !== 'object' || seen.indexOf(deps[d]) !== -1) return null
seen.push(deps[d])
if (deps[d].extraneous && (args.length === 0 || args.indexOf(d) !== -1)) {
var extra = deps[d]
delete deps[d]
return extra.path
}
return prunables(args, deps[d], seen)
}).filter(function (d) { return d !== null })
.reduce(function FLAT (l, r) {
return l.concat(Array.isArray(r) ? r.reduce(FLAT, []) : r)
}, [])
}