All files deprecate.js

79.49% Statements 31/39
86.67% Branches 13/15
71.43% Functions 10/14
80.56% Lines 29/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74    1x 1x 1x 1x 1x 1x 1x 1x   1x 2x       1x         1x 6x 1x   5x 4x 16x   4x     1x 3x   3x   1x 3x 2x     1x     1x   1x 1x                                               1x  
'use strict'
 
const npm = require('./npm.js')
const fetch = require('npm-registry-fetch')
const otplease = require('./utils/otplease.js')
const npa = require('npm-package-arg')
const semver = require('semver')
const getIdentity = require('./utils/get-identity.js')
const libaccess = require('libnpmaccess')
const usageUtil = require('./utils/usage.js')
 
const UsageError = () =>
  Object.assign(new Error(`\nUsage: ${usage}`), {
    code: 'EUSAGE',
  })
 
const usage = usageUtil(
  'deprecate',
  'npm deprecate <pkg>[@<version>] <message>'
)
 
const completion = (opts, cb) => {
  if (opts.conf.argv.remain.length > 1)
    return cb(null, [])
 
  return getIdentity(npm.flatOptions).then((username) => {
    return libaccess.lsPackages(username, npm.flatOptions).then((packages) => {
      return Object.keys(packages).filter((name) => packages[name] === 'write' && (opts.conf.argv.remain.length === 0 || name.startsWith(opts.conf.argv.remain[0])))
    })
  }).then((list) => cb(null, list), (err) => cb(err))
}
 
const cmd = (args, cb) =>
  deprecate(args)
    .then(() => cb())
    .catch(err => cb(err.code === 'EUSAGE' ? err.message : err))
 
const deprecate = async ([pkg, msg]) => {
  if (!pkg || !msg)
    throw UsageError()
 
  // 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
 
  Eif (semver.validRange(spec, true) === null)
    throw new Error(`invalid version range: ${spec}`)
 
  const uri = '/' + p.escapedName
  const packument = await fetch.json(uri, {
    ...npm.flatOptions,
    spec: p,
    query: { write: true }
  })
 
  Object.keys(packument.versions)
    .filter(v => semver.satisfies(v, spec))
    .forEach(v => {
      packument.versions[v].deprecated = msg
    })
 
  return otplease(npm.flatOptions, opts => fetch(uri, {
    ...opts,
    spec: p,
    method: 'PUT',
    body: packument,
    ignoreBody: true,
  }))
}
 
module.exports = Object.assign(cmd, { completion, usage })