• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const BB = require('bluebird')
4
5const npmConfig = require('./config/figgy-config.js')
6const fetch = require('libnpm/fetch')
7const figgyPudding = require('figgy-pudding')
8const otplease = require('./utils/otplease.js')
9const npa = require('libnpm/parse-arg')
10const semver = require('semver')
11const whoami = require('./whoami.js')
12
13const DeprecateConfig = figgyPudding({})
14
15module.exports = deprecate
16
17deprecate.usage = 'npm deprecate <pkg>[@<version>] <message>'
18
19deprecate.completion = function (opts, cb) {
20  return BB.try(() => {
21    if (opts.conf.argv.remain.length > 2) { return }
22    return whoami([], true, () => {}).then(username => {
23      if (username) {
24        // first, get a list of remote packages this user owns.
25        // once we have a user account, then don't complete anything.
26        // get the list of packages by user
27        return fetch(
28          `/-/by-user/${encodeURIComponent(username)}`,
29          DeprecateConfig()
30        ).then(list => list[username])
31      }
32    })
33  }).nodeify(cb)
34}
35
36function deprecate ([pkg, msg], opts, cb) {
37  if (typeof cb !== 'function') {
38    cb = opts
39    opts = null
40  }
41  opts = DeprecateConfig(opts || npmConfig())
42  return BB.try(() => {
43    if (msg == null) throw new Error(`Usage: ${deprecate.usage}`)
44    // fetch the data and make sure it exists.
45    const p = npa(pkg)
46
47    // npa makes the default spec "latest", but for deprecation
48    // "*" is the appropriate default.
49    const spec = p.rawSpec === '' ? '*' : p.fetchSpec
50
51    if (semver.validRange(spec, true) === null) {
52      throw new Error('invalid version range: ' + spec)
53    }
54
55    const uri = '/' + p.escapedName
56    return fetch.json(uri, opts.concat({
57      spec: p,
58      query: {write: true}
59    })).then(packument => {
60      // filter all the versions that match
61      Object.keys(packument.versions)
62        .filter(v => semver.satisfies(v, spec))
63        .forEach(v => { packument.versions[v].deprecated = msg })
64      return otplease(opts, opts => fetch(uri, opts.concat({
65        spec: p,
66        method: 'PUT',
67        body: packument,
68        ignoreBody: true
69      })))
70    })
71  }).nodeify(cb)
72}
73