diff --git a/lib/deprecate.js b/lib/deprecate.js index 9b71d1de494ad..7fe2fbed4ba55 100644 --- a/lib/deprecate.js +++ b/lib/deprecate.js @@ -1,55 +1,72 @@ -/* eslint-disable standard/no-callback-literal */ -var npm = require('./npm.js') -var mapToRegistry = require('./utils/map-to-registry.js') -var npa = require('npm-package-arg') +'use strict' + +const BB = require('bluebird') + +const npmConfig = require('./config/figgy-config.js') +const fetch = require('libnpm/fetch') +const figgyPudding = require('figgy-pudding') +const otplease = require('./utils/otplease.js') +const npa = require('libnpm/parse-arg') +const semver = require('semver') +const whoami = require('./whoami.js') + +const DeprecateConfig = figgyPudding({}) module.exports = deprecate deprecate.usage = 'npm deprecate [@] ' 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. - if (opts.conf.argv.remain.length > 2) return cb() - // get the list of packages by user - var path = '/-/by-user/' - mapToRegistry(path, npm.config, function (er, uri, c) { - if (er) return cb(er) - - if (!(c && c.username)) return cb() - - var params = { - timeout: 60000, - auth: c - } - npm.registry.get(uri + c.username, params, function (er, list) { - if (er) return cb() - console.error(list) - return cb(null, list[c.username]) + return BB.try(() => { + if (opts.conf.argv.remain.length > 2) { return } + return whoami([], true, () => {}).then(username => { + if (username) { + // first, get a list of remote packages this user owns. + // once we have a user account, then don't complete anything. + // get the list of packages by user + return fetch( + `/-/by-user/${encodeURIComponent(username)}`, + DeprecateConfig() + ).then(list => list[username]) + } }) - }) + }).nodeify(cb) } -function deprecate (args, cb) { - var pkg = args[0] - var msg = args[1] - if (msg === undefined) return cb('Usage: ' + deprecate.usage) +function deprecate ([pkg, msg], opts, cb) { + if (typeof cb !== 'function') { + cb = opts + opts = null + } + opts = DeprecateConfig(opts || npmConfig()) + return BB.try(() => { + if (msg == null) throw new Error(`Usage: ${deprecate.usage}`) + // fetch the data and make sure it exists. + const p = npa(pkg) - // fetch the data and make sure it exists. - var p = npa(pkg) + // npa makes the default spec "latest", but for deprecation + // "*" is the appropriate default. + const spec = p.rawSpec === '' ? '*' : p.fetchSpec - // npa makes the default spec "latest", but for deprecation - // "*" is the appropriate default. - var spec = p.rawSpec === '' ? '*' : p.fetchSpec - - mapToRegistry(p.name, npm.config, function (er, uri, auth) { - if (er) return cb(er) - - var params = { - version: spec, - message: msg, - auth: auth + if (semver.validRange(spec, true) === null) { + throw new Error('invalid version range: ' + spec) } - npm.registry.deprecate(uri, params, cb) - }) + + const uri = '/' + p.escapedName + return fetch.json(uri, opts.concat({ + spec: p, + query: {write: true} + })).then(packument => { + // filter all the versions that match + Object.keys(packument.versions) + .filter(v => semver.satisfies(v, spec)) + .forEach(v => { packument.versions[v].deprecated = msg }) + return otplease(opts, opts => fetch(uri, opts.concat({ + spec: p, + method: 'PUT', + body: packument, + ignoreBody: true + }))) + }) + }).nodeify(cb) }