11cb0ef41Sopenharmony_ciconst fetch = require('npm-registry-fetch')
21cb0ef41Sopenharmony_ciconst otplease = require('../utils/otplease.js')
31cb0ef41Sopenharmony_ciconst npa = require('npm-package-arg')
41cb0ef41Sopenharmony_ciconst semver = require('semver')
51cb0ef41Sopenharmony_ciconst getIdentity = require('../utils/get-identity.js')
61cb0ef41Sopenharmony_ciconst libaccess = require('libnpmaccess')
71cb0ef41Sopenharmony_ciconst BaseCommand = require('../base-command.js')
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciclass Deprecate extends BaseCommand {
101cb0ef41Sopenharmony_ci  static description = 'Deprecate a version of a package'
111cb0ef41Sopenharmony_ci  static name = 'deprecate'
121cb0ef41Sopenharmony_ci  static usage = ['<package-spec> <message>']
131cb0ef41Sopenharmony_ci  static params = [
141cb0ef41Sopenharmony_ci    'registry',
151cb0ef41Sopenharmony_ci    'otp',
161cb0ef41Sopenharmony_ci  ]
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  static ignoreImplicitWorkspace = true
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  static async completion (opts, npm) {
211cb0ef41Sopenharmony_ci    if (opts.conf.argv.remain.length > 1) {
221cb0ef41Sopenharmony_ci      return []
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    const username = await getIdentity(npm, npm.flatOptions)
261cb0ef41Sopenharmony_ci    const packages = await libaccess.getPackages(username, npm.flatOptions)
271cb0ef41Sopenharmony_ci    return Object.keys(packages)
281cb0ef41Sopenharmony_ci      .filter((name) =>
291cb0ef41Sopenharmony_ci        packages[name] === 'write' &&
301cb0ef41Sopenharmony_ci        (opts.conf.argv.remain.length === 0 ||
311cb0ef41Sopenharmony_ci          name.startsWith(opts.conf.argv.remain[0])))
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  async exec ([pkg, msg]) {
351cb0ef41Sopenharmony_ci    // msg == null because '' is a valid value, it indicates undeprecate
361cb0ef41Sopenharmony_ci    if (!pkg || msg == null) {
371cb0ef41Sopenharmony_ci      throw this.usageError()
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    // fetch the data and make sure it exists.
411cb0ef41Sopenharmony_ci    const p = npa(pkg)
421cb0ef41Sopenharmony_ci    const spec = p.rawSpec === '*' ? '*' : p.fetchSpec
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    if (semver.validRange(spec, true) === null) {
451cb0ef41Sopenharmony_ci      throw new Error(`invalid version range: ${spec}`)
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    const uri = '/' + p.escapedName
491cb0ef41Sopenharmony_ci    const packument = await fetch.json(uri, {
501cb0ef41Sopenharmony_ci      ...this.npm.flatOptions,
511cb0ef41Sopenharmony_ci      spec: p,
521cb0ef41Sopenharmony_ci      query: { write: true },
531cb0ef41Sopenharmony_ci    })
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    const versions = Object.keys(packument.versions)
561cb0ef41Sopenharmony_ci      .filter(v => semver.satisfies(v, spec, { includePrerelease: true }))
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    if (versions.length) {
591cb0ef41Sopenharmony_ci      for (const v of versions) {
601cb0ef41Sopenharmony_ci        packument.versions[v].deprecated = msg
611cb0ef41Sopenharmony_ci      }
621cb0ef41Sopenharmony_ci      return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, {
631cb0ef41Sopenharmony_ci        ...opts,
641cb0ef41Sopenharmony_ci        spec: p,
651cb0ef41Sopenharmony_ci        method: 'PUT',
661cb0ef41Sopenharmony_ci        body: packument,
671cb0ef41Sopenharmony_ci        ignoreBody: true,
681cb0ef41Sopenharmony_ci      }))
691cb0ef41Sopenharmony_ci    }
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_cimodule.exports = Deprecate
74