deprecate.js 2.1 KB

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