1 |
|
2 |
|
3 | module.exports = prune
|
4 |
|
5 | prune.usage = "npm prune"
|
6 |
|
7 | var readInstalled = require("read-installed")
|
8 | , npm = require("./npm.js")
|
9 | , path = require("path")
|
10 | , readJson = require("read-package-json")
|
11 | , log = require("npmlog")
|
12 |
|
13 | prune.completion = require("./utils/completion/installed-deep.js")
|
14 |
|
15 | function prune (args, cb) {
|
16 |
|
17 | var jsonFile = path.resolve(npm.dir, "..", "package.json" )
|
18 | readJson(jsonFile, log.warn, function (er) {
|
19 | if (er) return cb(er)
|
20 | next()
|
21 | })
|
22 |
|
23 | function next() {
|
24 | var opt = {
|
25 | depth: npm.config.get("depth"),
|
26 | dev: !npm.config.get("production") || npm.config.get("dev")
|
27 | }
|
28 | readInstalled(npm.prefix, opt, function (er, data) {
|
29 | if (er) return cb(er)
|
30 | prune_(args, data, cb)
|
31 | })
|
32 | }
|
33 | }
|
34 |
|
35 | function prune_ (args, data, cb) {
|
36 | npm.commands.unbuild(prunables(args, data, []), cb)
|
37 | }
|
38 |
|
39 | function prunables (args, data, seen) {
|
40 | var deps = data.dependencies || {}
|
41 | return Object.keys(deps).map(function (d) {
|
42 | if (typeof deps[d] !== "object"
|
43 | || seen.indexOf(deps[d]) !== -1) return null
|
44 | seen.push(deps[d])
|
45 | if (deps[d].extraneous
|
46 | && (args.length === 0 || args.indexOf(d) !== -1)) {
|
47 | var extra = deps[d]
|
48 | delete deps[d]
|
49 | return extra.path
|
50 | }
|
51 | return prunables(args, deps[d], seen)
|
52 | }).filter(function (d) { return d !== null })
|
53 | .reduce(function FLAT (l, r) {
|
54 | return l.concat(Array.isArray(r) ? r.reduce(FLAT,[]) : r)
|
55 | }, [])
|
56 | }
|