2020-10-02 17:52:19 -04:00
|
|
|
const fetch = require('npm-registry-fetch')
|
2019-01-29 14:43:00 -08:00
|
|
|
const otplease = require('./utils/otplease.js')
|
2020-10-02 17:52:19 -04:00
|
|
|
const npa = require('npm-package-arg')
|
2019-01-29 14:43:00 -08:00
|
|
|
const semver = require('semver')
|
2020-12-09 10:33:11 -05:00
|
|
|
const getIdentity = require('./utils/get-identity.js')
|
|
|
|
const libaccess = require('libnpmaccess')
|
2021-03-11 17:54:23 -05:00
|
|
|
const BaseCommand = require('./base-command.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Deprecate extends BaseCommand {
|
2021-03-23 14:58:11 -04:00
|
|
|
static get description () {
|
|
|
|
return 'Deprecate a version of a package'
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get name () {
|
|
|
|
return 'deprecate'
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get usage () {
|
|
|
|
return ['<pkg>[@<version>] <message>']
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
async completion (opts) {
|
|
|
|
if (opts.conf.argv.remain.length > 1)
|
|
|
|
return []
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const username = await getIdentity(this.npm, this.npm.flatOptions)
|
|
|
|
const packages = await libaccess.lsPackages(username, this.npm.flatOptions)
|
|
|
|
return Object.keys(packages)
|
|
|
|
.filter((name) =>
|
|
|
|
packages[name] === 'write' &&
|
|
|
|
(opts.conf.argv.remain.length === 0 ||
|
|
|
|
name.startsWith(opts.conf.argv.remain[0])))
|
|
|
|
}
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
exec (args, cb) {
|
|
|
|
this.deprecate(args)
|
|
|
|
.then(() => cb())
|
|
|
|
.catch(err => cb(err.code === 'EUSAGE' ? err.message : err))
|
|
|
|
}
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
async deprecate ([pkg, msg]) {
|
|
|
|
if (!pkg || !msg)
|
|
|
|
throw this.usageError()
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
// fetch the data and make sure it exists.
|
|
|
|
const p = npa(pkg)
|
|
|
|
// npa makes the default spec "latest", but for deprecation
|
|
|
|
// "*" is the appropriate default.
|
|
|
|
const spec = p.rawSpec === '' ? '*' : p.fetchSpec
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
if (semver.validRange(spec, true) === null)
|
|
|
|
throw new Error(`invalid version range: ${spec}`)
|
2019-01-29 14:43:00 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const uri = '/' + p.escapedName
|
|
|
|
const packument = await fetch.json(uri, {
|
|
|
|
...this.npm.flatOptions,
|
|
|
|
spec: p,
|
|
|
|
query: { write: true },
|
2019-01-29 14:43:00 -08:00
|
|
|
})
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
Object.keys(packument.versions)
|
|
|
|
.filter(v => semver.satisfies(v, spec, { includePrerelease: true }))
|
|
|
|
.forEach(v => {
|
|
|
|
packument.versions[v].deprecated = msg
|
|
|
|
})
|
|
|
|
|
|
|
|
return otplease(this.npm.flatOptions, opts => fetch(uri, {
|
|
|
|
...opts,
|
|
|
|
spec: p,
|
|
|
|
method: 'PUT',
|
|
|
|
body: packument,
|
|
|
|
ignoreBody: true,
|
|
|
|
}))
|
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Deprecate
|