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