UNPKG

auditjs

Version:

Audit dependencies to identify known vulnerabilities and maintenance problems

105 lines 3.98 kB
"use strict"; /* * Copyright 2019-Present Sonatype Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TextFormatter = void 0; const chalk = require("chalk"); class TextFormatter { constructor(quiet = false) { this.quiet = quiet; } printAuditResults(list) { const total = list.length; list = list.sort((a, b) => { return a.coordinates < b.coordinates ? -1 : 1; }); console.log(); console.group(); this.printLine('Sonabot here, beep boop beep boop, here are your Sonatype OSS Index results:'); this.suggestIncludeDevDeps(total); console.groupEnd(); console.log(); this.printLine('-'.repeat(process.stdout.columns)); list.forEach((x, i) => { if (x.vulnerabilities && x.vulnerabilities.length > 0) { this.printVulnerability(i, total, x); } else { this.printLine(chalk.keyword('green')(`[${i + 1}/${total}] - ${x.toAuditLog()}`)); } }); this.printLine('-'.repeat(process.stdout.columns)); } getColorFromMaxScore(maxScore, defaultColor = 'chartreuse') { if (maxScore > 8) { defaultColor = 'red'; } else if (maxScore > 6) { defaultColor = 'orange'; } else if (maxScore > 4) { defaultColor = 'yellow'; } return defaultColor; } printVulnerability(i, total, result) { if (!result.vulnerabilities) { return; } const maxScore = Math.max(...result.vulnerabilities.map((x) => { return +x.cvssScore; })); const printVuln = (x) => { x.forEach((y) => { const color = this.getColorFromMaxScore(+y.cvssScore); console.group(); this.printVulnField(color, `Vulnerability Title: `, y.title); this.printVulnField(color, `ID: `, y.id); this.printVulnField(color, `Description: `, y.description); this.printVulnField(color, `CVSS Score: `, y.cvssScore); this.printVulnField(color, `CVSS Vector: `, y.cvssVector); this.printVulnField(color, `CVE: `, y.cve); this.printVulnField(color, `Reference: `, y.reference); console.log(); console.groupEnd(); }); }; console.log(chalk.keyword(this.getColorFromMaxScore(maxScore)).bold(`[${i + 1}/${total}] - ${result.toAuditLog()}`)); console.log(); result.vulnerabilities && printVuln(result.vulnerabilities.sort((x, y) => { return +y.cvssScore - +x.cvssScore; })); } printLine(line) { if (!this.quiet) { console.log(line); } } printVulnField(color, title, field) { if (typeof field !== 'undefined') { console.log(chalk.keyword(color)(title), field); } } suggestIncludeDevDeps(total) { this.printLine(`Total dependencies audited: ${total}`); if (total == 0) { this.printLine(`We noticed you had 0 dependencies, we exclude devDependencies by default, try running with --dev if you want to include those as well`); } } } exports.TextFormatter = TextFormatter; //# sourceMappingURL=TextFormatter.js.map