summaryrefslogtreecommitdiff
path: root/deps/npm/lib/deprecate.js
blob: 1ba4305cd7557a44d3e11c9701dc15f12ddfb343 (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

module.exports = deprecate

deprecate.usage = "npm deprecate <pkg>[@<version>] <message>"

deprecate.completion = function (opts, cb) {
  // first, get a list of remote packages this user owns.
  // once we have a user account, then don't complete anything.
  var un = npm.config.get("username")
  if (!npm.config.get("username")) return cb()
  if (opts.conf.argv.remain.length > 2) return cb()
  // get the list of packages by user
  var uri = "/-/by-user/"+encodeURIComponent(un)
  registry.get(uri, null, 60000, function (er, list) {
    if (er) return cb()
    console.error(list)
    return cb(null, list[un])
  })
}

var semver = require("semver")
  , npm = require("./npm.js")
  , registry = npm.registry

function deprecate (args, cb) {
  var pkg = args[0]
    , msg = args[1]
  if (msg === undefined) return cb("Usage: " + deprecate.usage)
  // fetch the data and make sure it exists.
  pkg = pkg.split(/@/)
  var name = pkg.shift()
    , ver = pkg.join("@")
  if (semver.validRange(ver) === null) {
    return cb(new Error("invalid version range: "+ver))
  }
  registry.get(name, function (er, data) {
    if (er) return cb(er)
    // filter all the versions that match
    Object.keys(data.versions).filter(function (v) {
      return semver.satisfies(v, ver)
    }).forEach(function (v) {
      data.versions[v].deprecated = msg
    })
    // now update the doc on the registry
    registry.request('PUT', data._id, data, cb)
  })
}