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

activate.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca0e86303b3091090e2f38485c00f3ff12209a74 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

// activate a version of a package
// this presumes that it's been installed
// no need to worry about dependencies, because they were
// installed into the child package anyhow.

var 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")
  , link = require("./utils/link")
  , linkIfExists = link.ifExists
  , semver = require("semver")
  , asyncMap = require("./utils/async-map")
  , loadPackageDefaults = require("./utils/load-package-defaults")

module.exports = activate

activate.usage =
  "npm activate <name>@<version> [<name>@<version> ...]"

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

function activate (args, cb) {
  // make sure package and version exists.
  // If there's already an active version, then deactivate it.
  // first, link .npm/{pkg}/{version} to .npm/{pkg}/{active}
  // then, link the things in the root without a version to the active one.
  if (!args.length) return cb(new Error(activate.usage))
  asyncMap(args, preActivate, function (er, data) {
    log.silly(args, "preActivate over")
    if (er) return cb(er)
    asyncMap
      ( data
      , function (d, cb) {
          var from = path.join(npm.dir, d.name, d.version)
            , to = path.join(npm.dir, d.name, "active")
          link(from, to, cb)
        }
      , function (d, cb) {
          var fromMain = path.join(npm.dir, d.name, "active", "main.js")
            , toMain = path.join(npm.root, d.name+".js")
          linkIfExists(fromMain, toMain, cb)
        }
      // todo: remove this step.  0.1.28
      // required because old deps will be linked with - rather than @
      , function (d, cb) {
          var fromLib = path.join(npm.root, d.name + "-" + d.version)
            , toLib = path.join(npm.root, d.name)
          linkIfExists(fromLib, toLib, cb)
        }
      // if both @ and - exist, then this will overwrite the kludge one.
      , function (d, cb) {
          var fromLib = path.join(npm.root, d.name + "@" + d.version)
            , toLib = path.join(npm.root, d.name)
          linkIfExists(fromLib, toLib, cb)
        }
      , linkBins
      , linkMans
      , function (er) {
          if (er) return cb(er)
          asyncMap(data, postActivate, cb)
        }
      )
  })
}

function postActivate (data, cb) {
  chain
    ( [lifecycle, data, "activate"]
    , [lifecycle, data, "postactivate"]
    , cb
    )
}

function preActivate (arg, cb) {
  var args = arg.split("@")
    , pkg = args.shift()
    , version = args.join("@")
    , to = path.join(npm.dir, pkg, "active")
    , jsonFile = path.join(npm.dir, pkg, version, "package", "package.json")

  if (!version) cb(new Error("Please specify a version to activate: "+pkg))

  chain
    ( function (cb) { npm.commands.deactivate([pkg], function () { cb() }) }
    , function (cb) {
        fs.lstat(to, function (er, stat) {
          if (!er) return fs.readlink(to, function (er, active) {
            cb(new Error("Implicit deactivate failed.\n"+
              pkg+"@"+path.dirname(active)+" still active."))
          })
          else cb()
        })
      }
    , function (er) {
        if (er) return cb(er)
        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)
            log.silly(data._id, "defaults loaded")
            lifecycle(data, "preactivate", function (er) { cb(er, data) })
          })
        })
      }
    )
}
function linkMans (pkg, cb) {
  log.verbose(pkg._id, "activate linkMans")
  log.silly(pkg.man, "activate linkMans")
  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)
      , manStem = path.join( manroot
                           , "man"+sxn
                           , (bn.indexOf(pkg.name) === 0 ? bn
                             : pkg.name + "-" + bn)
                           )
      , suff = pkg.version + "." + sxn + gz
      , manDest = manStem + "." + sxn + gz
    log.silly(manDest, "manDest")
    linkIfExists(manStem+"-"+suff, manDest, function (er) {
      if (er) return cb(er)
      linkIfExists(manStem+"@"+suff, manDest, cb)
    })
  }, cb)
}
function linkBins (pkg, cb) {
  log.verbose(pkg._id, "activate linkBins")
  log.silly(pkg.bin, "activate linkBins")
  var binroot = npm.config.get("binroot")
  if (!pkg.bin || !binroot) return cb()
  asyncMap(Object.keys(pkg.bin), function (i, cb) {
    var to = path.join(binroot, i)
      , v = pkg.version
    linkIfExists(to+"-"+v, to, function (er) {
      if (er) return cb(er)
      linkIfExists(to+"@"+v, to, cb)
    })
  }, cb)
}