2025-01-10 08:20:27 -08:00
|
|
|
const npmFetch = require('npm-registry-fetch')
|
2024-05-16 05:38:49 -07:00
|
|
|
const { otplease } = require('../utils/auth.js')
|
2020-10-02 17:52:19 -04:00
|
|
|
const npa = require('npm-package-arg')
|
2024-05-16 05:38:49 -07:00
|
|
|
const { log } = require('proc-log')
|
2019-01-29 14:43:00 -08:00
|
|
|
const semver = require('semver')
|
2021-11-04 20:42:47 +00:00
|
|
|
const getIdentity = require('../utils/get-identity.js')
|
2020-12-09 10:33:11 -05:00
|
|
|
const libaccess = require('libnpmaccess')
|
2024-04-30 23:53:22 -07:00
|
|
|
const BaseCommand = require('../base-cmd.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Deprecate extends BaseCommand {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Deprecate a version of a package'
|
|
|
|
static name = 'deprecate'
|
2022-06-24 18:21:50 -07:00
|
|
|
static usage = ['<package-spec> <message>']
|
2021-11-18 20:58:02 +00:00
|
|
|
static params = [
|
|
|
|
'registry',
|
|
|
|
'otp',
|
2025-02-02 07:09:59 -08:00
|
|
|
'dry-run',
|
2021-11-18 20:58:02 +00:00
|
|
|
]
|
2021-05-20 15:54:50 -04:00
|
|
|
|
2023-10-13 06:55:37 -07:00
|
|
|
static ignoreImplicitWorkspace = true
|
2022-03-03 21:38:08 +00:00
|
|
|
|
2023-06-08 05:24:49 -07:00
|
|
|
static async completion (opts, npm) {
|
2021-11-18 20:58:02 +00:00
|
|
|
if (opts.conf.argv.remain.length > 1) {
|
2021-03-04 17:40:28 -05:00
|
|
|
return []
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2023-06-08 05:24:49 -07:00
|
|
|
const username = await getIdentity(npm, npm.flatOptions)
|
|
|
|
const packages = await libaccess.getPackages(username, npm.flatOptions)
|
2021-03-04 17:40:28 -05:00
|
|
|
return Object.keys(packages)
|
|
|
|
.filter((name) =>
|
2022-12-06 22:18:33 -05:00
|
|
|
packages[name] === 'write' &&
|
2021-03-04 17:40:28 -05:00
|
|
|
(opts.conf.argv.remain.length === 0 ||
|
|
|
|
name.startsWith(opts.conf.argv.remain[0])))
|
|
|
|
}
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec ([pkg, msg]) {
|
2021-10-07 20:21:11 +00:00
|
|
|
// msg == null because '' is a valid value, it indicates undeprecate
|
2021-11-18 20:58:02 +00:00
|
|
|
if (!pkg || msg == null) {
|
2021-03-04 17:40:28 -05:00
|
|
|
throw this.usageError()
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
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)
|
2022-12-06 22:18:33 -05:00
|
|
|
const spec = p.rawSpec === '*' ? '*' : p.fetchSpec
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (semver.validRange(spec, true) === null) {
|
2021-03-04 17:40:28 -05:00
|
|
|
throw new Error(`invalid version range: ${spec}`)
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2019-01-29 14:43:00 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const uri = '/' + p.escapedName
|
2025-01-10 08:20:27 -08:00
|
|
|
const packument = await npmFetch.json(uri, {
|
2021-03-04 17:40:28 -05:00
|
|
|
...this.npm.flatOptions,
|
|
|
|
spec: p,
|
|
|
|
query: { write: true },
|
2019-01-29 14:43:00 -08:00
|
|
|
})
|
2020-12-09 10:33:11 -05:00
|
|
|
|
2023-01-18 09:48:47 -05:00
|
|
|
const versions = Object.keys(packument.versions)
|
2021-03-04 17:40:28 -05:00
|
|
|
.filter(v => semver.satisfies(v, spec, { includePrerelease: true }))
|
|
|
|
|
2025-02-02 07:09:59 -08:00
|
|
|
const dryRun = this.npm.config.get('dry-run')
|
|
|
|
|
2023-01-18 09:48:47 -05:00
|
|
|
if (versions.length) {
|
|
|
|
for (const v of versions) {
|
|
|
|
packument.versions[v].deprecated = msg
|
2025-02-02 07:09:59 -08:00
|
|
|
if (msg) {
|
|
|
|
log.notice(`deprecating ${packument.name}@${v} with message "${msg}"`)
|
|
|
|
} else {
|
|
|
|
log.notice(`undeprecating ${packument.name}@${v}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!dryRun) {
|
|
|
|
return otplease(this.npm, this.npm.flatOptions, opts => npmFetch(uri, {
|
|
|
|
...opts,
|
|
|
|
spec: p,
|
|
|
|
method: 'PUT',
|
|
|
|
body: packument,
|
|
|
|
ignoreBody: true,
|
|
|
|
}))
|
2023-01-18 09:48:47 -05:00
|
|
|
}
|
2024-05-16 05:38:49 -07:00
|
|
|
} else {
|
|
|
|
log.warn('deprecate', 'No version found for', p.rawSpec)
|
2023-01-18 09:48:47 -05:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
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
|