The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / npm / lib / uninstall.js @ 5aef65a9

History | View | Annotate | Download (3.81 KB)

1

    
2
// remove a package.
3

    
4
module.exports = uninstall
5

    
6
uninstall.usage = "npm uninstall <name>[@<version> [<name>[@<version>] ...]"
7
                + "\nnpm rm <name>[@<version> [<name>[@<version>] ...]"
8

    
9
uninstall.completion = require("./utils/completion/installed-shallow.js")
10

    
11
var fs = require("graceful-fs")
12
  , log = require("npmlog")
13
  , readJson = require("read-package-json")
14
  , path = require("path")
15
  , npm = require("./npm.js")
16
  , semver = require("semver")
17
  , asyncMap = require("slide").asyncMap
18

    
19
function uninstall (args, cb) {
20
  // this is super easy
21
  // get the list of args that correspond to package names in either
22
  // the global npm.dir,
23
  // then call unbuild on all those folders to pull out their bins
24
  // and mans and whatnot, and then delete the folder.
25

    
26
  var nm = npm.dir
27
  if (args.length === 1 && args[0] === ".") args = []
28
  if (args.length) return uninstall_(args, nm, cb)
29

    
30
  // remove this package from the global space, if it's installed there
31
  if (npm.config.get("global")) return cb(uninstall.usage)
32
  readJson(path.resolve(npm.prefix, "package.json"), function (er, pkg) {
33
    if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
34
    if (er) return cb(uninstall.usage)
35
    uninstall_( [pkg.name]
36
              , npm.dir
37
              , cb )
38
  })
39

    
40
}
41

    
42
function uninstall_ (args, nm, cb) {
43
  // if we've been asked to --save or --save-dev or --save-optional,
44
  // then also remove it from the associated dependencies hash.
45
  var s = npm.config.get('save')
46
    , d = npm.config.get('save-dev')
47
    , o = npm.config.get('save-optional')
48
  if (s || d || o) {
49
    cb = saver(args, nm, cb)
50
  }
51

    
52
  asyncMap(args, function (arg, cb) {
53
    // uninstall .. should not delete /usr/local/lib/node_modules/..
54
    var p = path.join(path.resolve(nm), path.join("/", arg))
55
    if (path.resolve(p) === nm) {
56
      log.warn("uninstall", "invalid argument: %j", arg)
57
      return cb(null, [])
58
    }
59
    fs.lstat(p, function (er) {
60
      if (er) {
61
        log.warn("uninstall", "not installed in %s: %j", nm, arg)
62
        return cb(null, [])
63
      }
64
      cb(null, p)
65
    })
66
  }, function (er, folders) {
67
    if (er) return cb(er)
68
    asyncMap(folders, npm.commands.unbuild, cb)
69
  })
70
}
71

    
72
function saver (args, nm, cb_) {
73
  return cb
74
  function cb (er, data) {
75
    var s = npm.config.get('save')
76
      , d = npm.config.get('save-dev')
77
      , o = npm.config.get('save-optional')
78
    if (er || !(s || d || o)) return cb_(er, data)
79
    var pj = path.resolve(nm, '..', 'package.json')
80
    // don't use readJson here, because we don't want all the defaults
81
    // filled in, for mans and other bs.
82
    fs.readFile(pj, 'utf8', function (er, json) {
83
      try {
84
        var pkg = JSON.parse(json)
85
      } catch (_) {}
86
      if (!pkg) return cb_(null, data)
87

    
88
      var bundle
89
      if (npm.config.get('save-bundle')) {
90
        var bundle = pkg.bundleDependencies || pkg.bundledDependencies
91
        if (!Array.isArray(bundle)) bundle = undefined
92
      }
93

    
94
      var changed = false
95
      args.forEach(function (a) {
96
        ; [ [s, 'dependencies']
97
          , [o, 'optionalDependencies']
98
          , [d, 'devDependencies'] ].forEach(function (f) {
99
            var flag = f[0]
100
              , field = f[1]
101
            if (!flag || !pkg[field] || !pkg[field].hasOwnProperty(a)) return
102
            changed = true
103

    
104
            if (bundle) {
105
              var i = bundle.indexOf(a)
106
              if (i !== -1) bundle.splice(i, 1)
107
            }
108

    
109
            delete pkg[field][a]
110
          })
111
      })
112
      if (!changed) return cb_(null, data)
113

    
114
      if (bundle) {
115
        delete pkg.bundledDependencies
116
        if (bundle.length) {
117
          pkg.bundleDependencies = bundle
118
        } else {
119
          delete pkg.bundleDependencies
120
        }
121
      }
122

    
123
      fs.writeFile(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) {
124
        return cb_(er, data)
125
      })
126
    })
127
  }
128
}