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

install.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e847429dfec6c8da221b9aa8b9de0e62ac1c67ac (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

// npm install <pkg> <pkg> <pkg>
// npm install <pkg@version> <pkg@"1.0.0 - 1.99.99"> <pkg[@latest]> <pkg@tagname>

// 1. fetch the data for that package/tag into the cache
// 2. if it has any dependents, which are not yet installed,
// then add those to the list, and fetch their data.
// 3. when all the pkgs are fetched to the cache, and we have a set
// of packages that are either installed or fetched which
// will satisfy everyone's dependencies, then untar into the
// target directories for each of them.
// 4. build each of the packages that aren't already installed

module.exports = install

install.usage = "npm install <tarball file>"
              + "\nnpm install <tarball url>"
              + "\nnpm install <folder>"
              + "\nnpm install <pkg>"
              + "\nnpm install <pkg>@<tag>"
              + "\nnpm install <pkg>@<version>"
              + "\nnpm install <pkg>@<version range>"
              + "\n\nCan specify one or more: npm install ./foo.tgz bar@stable /some/folder"
              + "\nInstalls '.' if no argument supplied"

install.completion = function (args, index, cb) {
  var remotePkgs = require("./utils/completion/remote-packages")
  remotePkgs(args, index, true, true, true, cb)
}

var registry = require("./utils/registry")
  , npm = require("../npm")
  , readInstalled = require("./utils/read-installed")
  , installedPackages
  , semver = require("semver")
  , url = require("url")
  , fetch = require("./utils/fetch")
  , readJson = require("./utils/read-json")
  , log = require("./utils/log")
  , path = require("path")
  , fs = require("./utils/graceful-fs")
  , cache = require("./cache")
  , asyncMap = require("./utils/async-map")
  , chain = require("./utils/chain")

function install (pkglist, cb) {
  log.verbose(pkglist, "install")
  if (pkglist.length === 0) pkglist = ["."]
  // it's helpful to know what we have already
  if (!installedPackages) return readInstalled([], function (er, data) {
    if (er) return cb(er)
    installedPackages = data || {}
    install(pkglist, cb)
  })

  log.verbose(pkglist, "install pkglist")
  var mustInstall = npm.config.get("must-install") ? pkglist.slice(0) : []

  // three lists: "pkglist", "next", and "reg"
  // asyncMap over the "left" list: for each "it"
  //   find out what it is
  //   if it's version/range installed or on "reg" list, continue.
  //   if it's a url, fetch to cache and add the name/version to "next"
  //   if it's a tag, fetch the json, add the version to "next"
  //   if it's a version(range) not installed, then fetch the json,
  //     add url to "next"
  //   if it's a specific version in cache, then unpack, add to "reg"
  //     list, add its deps to "next"
  // if the "next" list is not empty, then pkglist=next,next=[], and repeat.
  // if it is, then build all the "reg" folders.

  var reg = Object.create(installedPackages)
    , seen = {}
    , bundles = {}
  log.verbose(mustInstall, "must install")
  asyncMap(pkglist, function (pkg, cb) {
    install_(pkg, reg, seen, mustInstall.indexOf(pkg) !== -1,
             pkglist, bundles, cb)
  }, function (er) {
    if (er) return cb(er)
    buildAll(reg, bundles, cb)
  })
}

// call the cb with the "next" thing(s) to look up for this one, or nothing
function install_ (pkg, reg, seen, mustHave, pkglist, bundles, cb) {
  log.verbose(pkg, "install_")
  if (seen[pkg]) return cb() // repeat, skip it
  seen[pkg] = true

  // it's a local thing or a url if it has a / in it.
  if (pkg.indexOf("/") !== -1 || pkg === ".") {
    log.silly(pkg, "install local")
    return cache.add(pkg, finisher(pkg, reg, pkglist, bundles, cb))
  }

  // now we know it's not a URL or file,
  // so handle it like a tag, version, or range.
  pkg = pkg.split("@")
  var name = pkg[0]
    , defTag = npm.config.get("tag")
    , ver = pkg.slice(1).join("@").trim() || defTag
    , range = semver.validRange(ver)
    , exact = semver.valid(ver)
    , tag = !exact && !range && range !== "" && ver
  log.verbose([pkg, mustHave], "must install?")
  pkg = pkg.join("@")
  seen[name+"@"+ver] = true

  // if there is a satisfying version already, then simply move on.
  if (!tag && findSatisfying(pkg, name, range, mustHave, reg)) {
    return cb()
  }

  // at this point, assume that it has to be installed.
  if (exact) {
    log.verbose("exact", pkg)
    // just pull the data out of the cache to ensure it's there
    return cache.read(name, exact, finisher(pkg, reg, pkglist, bundles, cb))
  }

  getData(name, function (er, data) {
    if (er) return cb(er)
    log.silly(data, pkg)
    if (tag) {
      log.verbose(tag, pkg+" tag")
      var tags = data["dist-tags"]
      if (!tags[tag]) cb(new Error(
        "Tag not found: "+data.name+"@"+tag
        +"\nValid install targets for "+data.name+": "
        +installTargets(data)))
      install_(data.name+"@"+tags[tag], reg, seen, mustHave
              , pkglist, bundles, cb)
    } else {
      log(pkg, "range")
      // prefer the default tag version.
      var defTag = npm.config.get("tag")
        , satis
      defTag = defTag && data["dist-tags"] && data["dist-tags"][defTag]
      if (semver.satisfies(defTag, range)) satis = defTag
      else satis = semver.maxSatisfying(Object.keys(data.versions), range)

      if (!satis) return cb(new Error(
        "No satisfying version found for '"+data.name+"'@'"+range+"'"
        +"\nValid install targets for "+data.name+": "
        +installTargets(data)))
      install_(data.name+"@"+satis, reg, seen, mustHave
              , pkglist, bundles, cb)
    }
  })
}
function installTargets (data) {
  return Object.keys(data["dist-tags"] || {})
    .concat(Object.keys(data.versions))
    .map(JSON.stringify)
    .join(", ") || "(none)"
}

function getData (name, cb) {
  var data = npm.get(name)
  if (data && data["dist-tags"]) return cb(null, data)
  registry.get(name, function (er, data) {
    if (er) return cb(er)
    if (!data._id) data._id = name
    for (var ver in data.versions) {
      try {
        readJson.processJson(data.versions[ver])
      } catch (ex) {
        log(data.name+"@"+ver, "ignoring invalid version")
        delete data.versions[ver]
      }
    }
    if (!data["dist-tags"]) return cb(new Error(
      (data._id || data.name) +
      " Invalid package data. Lacking 'dist-tags' hash."))
    filterNodeVersion(data)
    npm.set(data)
    cb(null, data)
  })
}
// see if there is a satisfying version already
function findSatisfying (pkg, name, range, mustHave, reg) {
  if (mustHave) return null
  return semver.maxSatisfying
         ( Object.keys(reg[name] || {})
           .concat(Object.keys(Object.getPrototypeOf(reg[name] || {})))
         , range
         )
}

function finisher (pkg, reg, pkglist, bundles, cb) {
    return function (er, data) {
  if (!cb) throw new Error("no cb in finisher")
  if (er) return log.er(cb, "Error installing "+pkg)(er)
  if (!data._engineSupported) return cb(
    data.name+"@"+data.version
    +" not compatible with this version of node/npm\n"
    +"Requires: "+JSON.stringify(data.engines)+"\n"
    +"You have: "+JSON.stringify({node:process.version, npm:npm.version}))

  if (!reg.hasOwnProperty(data.name)) {
    reg[data.name] = Object.create(reg[data.name] || Object.prototype)
  }
  reg[data.name][data.version] = data

  // also add the dependencies.
  // any dependencies that are URLs get added to a bundle list
  if (!pkglist) pkglist = []
  getDeps(data, function (er, deps) {
    if (er) return cb(er)
    if (!Array.isArray(pkglist)) pkglist = [pkglist]
    pkglist.push.apply(pkglist, Object.keys(deps).map(function (dep) {
      var v = deps[dep].trim()
        , d = dep.trim()
        , u = url.parse(v)
      log.silly(u, "url.parse "+v)
      if (u && u.protocol && u.host) {
        bundles[data._id] = bundles[data._id] || []
        bundles[data._id].push(u.href)
        return false
      }
      return v ? d + "@" + v : d
    }).filter(function (dep) { return dep }))
    cb()
  })
}}

function getDeps (data, cb) {
  var deps = data.dependencies || {}
    , devDeps = data.devDependencies || {}
  if (npm.config.get("dev")) {
    Object.keys(devDeps).forEach(function (d) { deps[d] = devDeps[d] })
  }
  // now see if any of them are already bundled.
  // if so, omit them from the list.
  var bundle = path.join( npm.cache, data.name, data.version
                        , "package", "node_modules" )
  fs.readdir(bundle, function (er, bundles) {
    bundles = bundles || []
    bundles.forEach(function (b) { delete deps[b] })
    cb(null, deps)
  })
}

function filterNodeVersion (data) {
  var supported = []
  Object.keys(data.versions).forEach(function (v) {
    if (!data.versions[v]._engineSupported) {
      log.verbose(data.versions[v]._id, "not supported on node@"+process.version)
      delete data.versions[v]
    } else supported.push(data.versions[v]._id)
  })
  if (!supported.length) {
    log.warn(data._id, "Not supported on node@"+process.version)
  } else log.verbose(supported, "Supported versions")
  // might have deleted the special "latest" tag.  if so, replace it.
  var v = data["dist-tags"].latest
  if (v && !data.versions[v] && supported.length) {
    data["dist-tags"].latest = Object.keys(data.versions)
      .sort(semver.compare).pop()
    log("not supported by node@"+process.version,
        "latest = "+data.name+"@"+v)
  }
}

function buildAll (installed, bundles, cb) {
  var list = []
  Object.keys(installed).forEach(function (i) {
    Object.keys(installed[i]).forEach(function (v) {
      list.push([i, v])
    })
  })
  log.verbose(list, "install list")
  cb = rollbackFailure(list, cb)
  if (!list.length) return log.info("Nothing to do", "install", cb)
  var buildList = []

  asyncMap(list, function (i, cb) {
    var target = path.join(npm.dir, i[0], i[1])
    cache.unpack( i[0], i[1], target
                , null, null // don't chmod anything here.  just as-is.
                , npm.config.get("user")
                , npm.config.get("group"), function (er) {
      if (!er) buildList.push(target)
      cb(er)
    })
  }, function (er) {
    if (er) return cb(er)
    log.verbose(list.join("\n"), "unpacked, building")
    // now everything's been unpacked.
    // install any bundles that were requested with a url dep.
    // MUST install ALL bundles before building anything
    log.silly(bundles, "installing bundles")
    chain( [installBundles, bundles]
         , [npm.commands.build, buildList]
         , cb )
  })
}

function installBundles (bundles, cb) {
  // bundles is {name:[some, pkg, urls],...}
  // bundle install each of them one at at time.
  // This global config state thing sucks a lot. Need
  // a more clean FP-style approach to the whole bundle thing.
  var bundlePkgs = Object.keys(bundles)
  if (!bundlePkgs.length) return cb()
  chain(bundlePkgs.map(function (host) { return function (cb) {
    var urls = bundles[host]
      , _ = host.split("@")
      , name = _.shift()
      , ver = _.join("@")
      , pkgDir = path.join(npm.dir, name, ver, "package")
    log.info(urls, "bundling for "+host)
    npm.commands.bundle(["install"].concat(urls), pkgDir, cb)
  }}).concat(cb))
}

function rollbackFailure (installList, cb) { return function (er) {
  if (!er) return log.verbose(installList.map(function (i) {
    return i.join("@")
  }).join("\n"), "installed", cb)
  // error happened, roll back
  installList = installList.map(function (p) {
    return p.join('@')
  })
  npm.ROLLBACK = true
  log.error(er, "install failed")
  log("rollback", "install failed")
  log(installList, "uninstall")
  return npm.commands.uninstall
    ( installList
    , function (er_) {
        if (er_) log.error(er_, "rollback failed")
        else log("rolled back", "install failed")
        cb(er)
      }
    )
}}