revoke.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const globalDefinitions = require('@npmcli/config/lib/definitions/definitions.js')
  2. const Definition = require('@npmcli/config/lib/definitions/definition.js')
  3. const { otplease } = require('../../utils/auth.js')
  4. const npmFetch = require('npm-registry-fetch')
  5. const npa = require('npm-package-arg')
  6. const TrustCommand = require('../../trust-cmd.js')
  7. class TrustRevoke extends TrustCommand {
  8. static description = 'Revoke a trusted relationship for a package'
  9. static name = 'revoke'
  10. static positionals = 1 // expects at most 1 positional (package name)
  11. static usage = [
  12. '[package] --id=<trust-id>',
  13. ]
  14. static definitions = [
  15. new Definition('id', {
  16. default: null,
  17. type: String,
  18. description: 'ID of the trusted relationship to revoke',
  19. required: true,
  20. }),
  21. globalDefinitions['dry-run'],
  22. globalDefinitions.registry,
  23. ]
  24. async exec (positionalArgs, flags) {
  25. const dryRun = this.config.get('dry-run')
  26. const pkgName = positionalArgs[0] || (await this.optionalPkgJson()).name
  27. if (!pkgName) {
  28. throw new Error('Package name must be specified either as an argument or in the package.json file')
  29. }
  30. const { id } = flags
  31. if (!id) {
  32. throw new Error('ID of the trusted relationship to revoke must be specified with the --id option')
  33. }
  34. this.dialogue`Attempting to revoke trusted configuration for package ${pkgName} with id ${id}`
  35. if (dryRun) {
  36. return
  37. }
  38. const spec = npa(pkgName)
  39. const uri = `/-/package/${spec.escapedName}/trust/${encodeURIComponent(id)}`
  40. await otplease(this.npm, this.npm.flatOptions, opts => npmFetch(uri, {
  41. ...opts,
  42. method: 'DELETE',
  43. }))
  44. this.dialogue`Revoked trusted configuration for package ${pkgName} with id ${id}`
  45. }
  46. }
  47. module.exports = TrustRevoke