list.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const { otplease } = require('../../utils/auth.js')
  2. const npmFetch = require('npm-registry-fetch')
  3. const npa = require('npm-package-arg')
  4. const TrustCircleCI = require('./circleci.js')
  5. const TrustGithub = require('./github.js')
  6. const TrustGitlab = require('./gitlab.js')
  7. const TrustCommand = require('../../trust-cmd.js')
  8. const globalDefinitions = require('@npmcli/config/lib/definitions/definitions.js')
  9. class TrustList extends TrustCommand {
  10. static description = 'List trusted relationships for a package'
  11. static name = 'list'
  12. static positionals = 1 // expects at most 1 positional (package name)
  13. static usage = [
  14. '[package]',
  15. ]
  16. static definitions = [
  17. globalDefinitions.json,
  18. globalDefinitions.registry,
  19. ]
  20. static bodyToOptions (body) {
  21. if (body.type === 'circleci') {
  22. return TrustCircleCI.bodyToOptions(body)
  23. } else if (body.type === 'github') {
  24. return TrustGithub.bodyToOptions(body)
  25. } else if (body.type === 'gitlab') {
  26. return TrustGitlab.bodyToOptions(body)
  27. }
  28. return TrustCommand.bodyToOptions(body)
  29. }
  30. async exec (positionalArgs) {
  31. const packageName = positionalArgs[0] || (await this.optionalPkgJson()).name
  32. if (!packageName) {
  33. throw new Error('Package name must be specified either as an argument or in the package.json file')
  34. }
  35. const spec = npa(packageName)
  36. const uri = `/-/package/${spec.escapedName}/trust`
  37. const body = await otplease(this.npm, this.npm.flatOptions, opts => npmFetch.json(uri, {
  38. ...opts,
  39. method: 'GET',
  40. }))
  41. this.displayResponseBody({ body, packageName })
  42. }
  43. }
  44. module.exports = TrustList