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: 69b94baac55fec8a97e1caec1d5cff98f84f06a6 (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

// 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 mkdir = require("./utils/mkdir-p")
  , npm = require("../npm")
  , fs = require("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
  , shimIfExists = require("./utils/write-shim").ifExists
  , semver = require("./utils/semver")

module.exports = activate

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.
  
  // TODO: remove this when it's more commonplace.
  if (args.length === 2 && !semver.valid(args[0]) && semver.valid(args[1])) {
    log("http://github.com/isaacs/npm/issues/issue/91", "See:")
    return cb(new Error(
      "Usage: npm activate <pkg>@<version> [pkg@<version> ...]"))
  }
  
  chain(args.map(function (arg) {
    return [activate_, arg]
  }).concat(cb))
}

function activate_ (arg, cb) {
  var args = arg.split("@")
    , pkg = args.shift()
    , version = args.join("@")
    , from = path.join(npm.dir, pkg, version)
    , to = path.join(npm.dir, pkg, "active")
    , fromMain = path.join(npm.dir, pkg, "active", "main.js")
    , toMain = path.join(npm.root, pkg+".js")
    , fromLib = path.join(npm.root, pkg + "-" + version)
    , toLib = path.join(npm.root, pkg)
    , 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) cb(er)
        readJson(jsonFile, function (er, data) {
          if (er) cb(er)
          data.version = version
          data._id = data.name + "-" + data.version
          npm.set(data)
          chain
            ( [lifecycle, data, "preactivate"]
            , [link, from, to]
            , function (cb) {
                fs.stat(toMain, function (er) {
                  if (!er) return cb()
                  shimIfExists(fromMain, toMain, cb)
                })
              }
            , function (cb) {
                fs.stat(toLib, function (er) {
                  if (!er) return cb()
                  linkIfExists(fromLib, toLib, cb)
                })
              }
            , [linkBins, data]
            , [lifecycle, data, "activate"]
            , [lifecycle, data, "postactivate"]
            , cb
            )
        })
      }
    )
}

function linkBins (pkg, cb) {
  var binroot = npm.config.get('binroot')
  if (!pkg.bin || !binroot) return cb()
  chain(Object.keys(pkg.bin).map(function (i) {
    var to = path.join(binroot, i)
      , from = to+"-"+pkg.version
    return function (cb) {
      fs.stat(to, function (er) {
        if (!er) cb()
        linkIfExists(from, to, cb)
      })
    }
  }).concat(cb))
}