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

deactivate.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58efc146515b51e16e4ea9fcf709996fb00aac0f (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

module.exports = deactivate

deactivate.usage = "npm deactivate <pkg>"

deactivate.completion = function (args, index, cb) {
  var installedPkgs = require("./utils/completion/installed-packages")
  installedPkgs(args, index, false, false, cb)
}

var mkdir = require("./utils/mkdir-p")
  , npm = require("../npm")
  , fs = require("./utils/graceful-fs")
  , log = require("./utils/log")
  , path = require("path")
  , rm = require("./utils/rm-rf")
  , chain = require("./utils/chain")
  , lifecycle = require("./utils/lifecycle")
  , readJson = require("./utils/read-json")
  , asyncMap = require("./utils/async-map")
  , loadPackageDefaults = require("./utils/load-package-defaults")

function deactivate (args, cb) {
  var rb = npm.ROLLBACK
  npm.ROLLBACK = true
  asyncMap(args.map(function (a) {
    return a.split("@").shift()
  }), preDeactivate, function (er, data) {
    if (er) return cb(er)
    asyncMap
      ( data
      , function (d, cb) { rm(path.join(npm.dir, d.name, "active"), cb) }
      , function (d, cb) { rm(path.join(npm.root, d.name+".js"), cb) }
      , function (d, cb) { rm(path.join(npm.root, d.name), cb) }
      , rmBins
      , rmMans
      , function (er) {
          if (er) return cb(er)
          asyncMap(data, postDeactivate, function (er) {
            npm.ROLLBACK = rb
            cb(er)
          })
        }
      )
  })
}

function rmBins (data, cb) {
  var binroot = npm.config.get("binroot")
  if (!data.bin || !binroot) return cb()
  asyncMap(Object.getOwnPropertyNames(data.bin)
            .map(function (bin) { return path.join(binroot, bin) })
          , rm
          , cb
          )
}
function rmMans (pkg, cb) {
  var manroot = npm.config.get("manroot")
  if (!pkg.man || !manroot) return cb()
  asyncMap(pkg.man, function (man, cb) {
    var parseMan = man.match(/(.*)\.([0-9]+)(\.gz)?$/)
      , stem = parseMan[1]
      , sxn = parseMan[2]
      , gz = parseMan[3] || ""
      , bn = path.basename(stem)
    rm(path.join( manroot
                , "man"+sxn
                , (bn.indexOf(pkg.name) === 0 ? bn
                  : pkg.name + "-" + bn)
                  + "." + sxn + gz
                ), cb)
  }, cb)
}

function preDeactivate (pkg, cb) {
  // run the "deactivate" lifecycle event
  // unlink the "active" folder
  // unlink the libs and main.js from the npm.root
  // run the "postdeactivate" lifecycle event
  var active = path.join(npm.dir, pkg, "active")
    , jsonFile = path.join(active, "package", "package.json")

  fs.readlink(active, function (er, p) {
    if (er) return cb()
    var version = path.basename(p)
    readJson(jsonFile, function (er, data) {
      if (er) return cb(er)
      data.version = version
      data._id = data.name+"@"+data.version
      npm.set(data)
      loadPackageDefaults(data, function (er, data) {
        if (er) return cb(er)
        chain
          ( [lifecycle, data, "predeactivate"]
          , [lifecycle, data, "deactivate"]
          , function (er) { cb(er, data) }
          )
      })
    })
  })
}

function postDeactivate (data, cb) {
  asyncMap(data, function (d, cb) { lifecycle(d, "postdeactivate", cb) }, cb)
}