()
| 23 | ] |
| 24 | |
| 25 | async exec () { |
| 26 | const sbomFormat = this.npm.config.get('sbom-format') |
| 27 | const packageLockOnly = this.npm.config.get('package-lock-only') |
| 28 | |
| 29 | if (!sbomFormat) { |
| 30 | throw this.usageError(`Must specify --sbom-format flag with one of: ${SBOM_FORMATS.join(', ')}.`) |
| 31 | } |
| 32 | |
| 33 | const opts = { |
| 34 | ...this.npm.flatOptions, |
| 35 | path: this.npm.prefix, |
| 36 | forceActual: true, |
| 37 | } |
| 38 | const Arborist = require('@npmcli/arborist') |
| 39 | const arb = new Arborist(opts) |
| 40 | |
| 41 | const tree = packageLockOnly ? await arb.loadVirtual(opts).catch(() => { |
| 42 | throw this.usageError('A package lock or shrinkwrap file is required in package-lock-only mode') |
| 43 | }) : await arb.loadActual(opts) |
| 44 | |
| 45 | // Collect the list of selected workspaces in the project |
| 46 | const wsNodes = this.workspaceNames?.length |
| 47 | ? arb.workspaceNodes(tree, this.workspaceNames) |
| 48 | : null |
| 49 | |
| 50 | // Build the selector and query the tree for the list of nodes |
| 51 | const selector = this.#buildSelector({ wsNodes }) |
| 52 | log.info('sbom', `Using dependency selector: ${selector}`) |
| 53 | const items = await tree.querySelectorAll(selector) |
| 54 | |
| 55 | const errors = items.flatMap(node => detectErrors(node)) |
| 56 | if (errors.length) { |
| 57 | throw Object.assign(new Error([...new Set(errors)].join('\n')), { |
| 58 | code: 'ESBOMPROBLEMS', |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | // Populate the response with the list of unique nodes (sorted by location) |
| 63 | this.#buildResponse(items.sort((a, b) => localeCompare(a.location, b.location))) |
| 64 | |
| 65 | // TODO(BREAKING_CHANGE): all sbom output is in json mode but setting it before any of the errors will cause those to be thrown in json mode. |
| 66 | this.npm.config.set('json', true) |
| 67 | output.standard(JSON.stringify(this.#response, null, 2), { [META]: true, redact: false }) |
| 68 | } |
| 69 | |
| 70 | async execWorkspaces (args) { |
| 71 | await this.setWorkspaces() |
no test coverage detected