Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uninstall.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 42a9a83e0e52953c6f5dbba95951bed5a362d212 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

// remove a package.

module.exports = uninstall

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

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

var fs = require("graceful-fs")
  , log = require("npmlog")
  , readJson = require("read-package-json")
  , path = require("path")
  , npm = require("./npm.js")
  , asyncMap = require("slide").asyncMap

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

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

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

}

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

  asyncMap(args, function (arg, cb) {
    // uninstall .. should not delete /usr/local/lib/node_modules/..
    var p = path.join(path.resolve(nm), path.join("/", arg))
    if (path.resolve(p) === nm) {
      log.warn("uninstall", "invalid argument: %j", arg)
      return cb(null, [])
    }
    fs.lstat(p, function (er) {
      if (er) {
        log.warn("uninstall", "not installed in %s: %j", nm, arg)
        return cb(null, [])
      }
      cb(null, p)
    })
  }, function (er, folders) {
    if (er) return cb(er)
    asyncMap(folders, npm.commands.unbuild, cb)
  })
}

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

      var bundle
      if (npm.config.get('save-bundle')) {
        bundle = pkg.bundleDependencies || pkg.bundledDependencies
        if (!Array.isArray(bundle)) bundle = undefined
      }

      var changed = false
      args.forEach(function (a) {
        ; [ [s, 'dependencies']
          , [o, 'optionalDependencies']
          , [d, 'devDependencies'] ].forEach(function (f) {
            var flag = f[0]
              , field = f[1]
            if (!flag || !pkg[field] || !pkg[field].hasOwnProperty(a)) return
            changed = true

            if (bundle) {
              var i = bundle.indexOf(a)
              if (i !== -1) bundle.splice(i, 1)
            }

            delete pkg[field][a]
          })
      })
      if (!changed) return cb_(null, data)

      if (bundle) {
        delete pkg.bundledDependencies
        if (bundle.length) {
          pkg.bundleDependencies = bundle
        } else {
          delete pkg.bundleDependencies
        }
      }

      fs.writeFile(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) {
        return cb_(er, data)
      })
    })
  }
}