1 | 'use strict'
|
2 |
|
3 | const BB = require('bluebird')
|
4 |
|
5 | const npmConfig = require('./config/figgy-config.js')
|
6 | const fetch = require('libnpm/fetch')
|
7 | const figgyPudding = require('figgy-pudding')
|
8 | const otplease = require('./utils/otplease.js')
|
9 | const npa = require('libnpm/parse-arg')
|
10 | const semver = require('semver')
|
11 | const whoami = require('./whoami.js')
|
12 |
|
13 | const DeprecateConfig = figgyPudding({})
|
14 |
|
15 | module.exports = deprecate
|
16 |
|
17 | deprecate.usage = 'npm deprecate <pkg>[@<version>] <message>'
|
18 |
|
19 | deprecate.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 |
|
25 |
|
26 |
|
27 | return fetch(
|
28 | `/-/by-user/${encodeURIComponent(username)}`,
|
29 | DeprecateConfig()
|
30 | ).then(list => list[username])
|
31 | }
|
32 | })
|
33 | }).nodeify(cb)
|
34 | }
|
35 |
|
36 | function 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 |
|
45 | const p = npa(pkg)
|
46 |
|
47 |
|
48 |
|
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 |
|
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 | }
|