Skip to content

Commit 64de4eb

Browse files
committed
deprecate: stop using npm-registry-client
1 parent 584613e commit 64de4eb

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

lib/deprecate.js

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,72 @@
1-
/* eslint-disable standard/no-callback-literal */
2-
var npm = require('./npm.js')
3-
var mapToRegistry = require('./utils/map-to-registry.js')
4-
var npa = require('npm-package-arg')
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({})
514

615
module.exports = deprecate
716

817
deprecate.usage = 'npm deprecate <pkg>[@<version>] <message>'
918

1019
deprecate.completion = function (opts, cb) {
11-
// first, get a list of remote packages this user owns.
12-
// once we have a user account, then don't complete anything.
13-
if (opts.conf.argv.remain.length > 2) return cb()
14-
// get the list of packages by user
15-
var path = '/-/by-user/'
16-
mapToRegistry(path, npm.config, function (er, uri, c) {
17-
if (er) return cb(er)
18-
19-
if (!(c && c.username)) return cb()
20-
21-
var params = {
22-
timeout: 60000,
23-
auth: c
24-
}
25-
npm.registry.get(uri + c.username, params, function (er, list) {
26-
if (er) return cb()
27-
console.error(list)
28-
return cb(null, list[c.username])
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+
}
2932
})
30-
})
33+
}).nodeify(cb)
3134
}
3235

33-
function deprecate (args, cb) {
34-
var pkg = args[0]
35-
var msg = args[1]
36-
if (msg === undefined) return cb('Usage: ' + deprecate.usage)
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+
// fetch the data and make sure it exists.
45+
const p = npa(pkg)
3746

38-
// fetch the data and make sure it exists.
39-
var p = npa(pkg)
47+
// npa makes the default spec "latest", but for deprecation
48+
// "*" is the appropriate default.
49+
const spec = p.rawSpec === '' ? '*' : p.fetchSpec
4050

41-
// npa makes the default spec "latest", but for deprecation
42-
// "*" is the appropriate default.
43-
var spec = p.rawSpec === '' ? '*' : p.fetchSpec
44-
45-
mapToRegistry(p.name, npm.config, function (er, uri, auth) {
46-
if (er) return cb(er)
47-
48-
var params = {
49-
version: spec,
50-
message: msg,
51-
auth: auth
51+
if (semver.validRange(spec, true) === null) {
52+
throw new Error('invalid version range: ' + spec)
5253
}
53-
npm.registry.deprecate(uri, params, cb)
54-
})
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)
5572
}

0 commit comments

Comments
 (0)