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