|
| 1 | +// Import Third-party Dependencikes |
| 2 | +import { report } from "@nodesecure/report"; |
| 3 | +import * as scanner from "@nodesecure/scanner"; |
| 4 | + |
| 5 | +// CONSTANTS |
| 6 | +const kSupportedReporters = new Set(["html", "pdf"]); |
| 7 | +const kReportPayload = { |
| 8 | + includeTransitiveInternal: false, |
| 9 | + charts: [ |
| 10 | + { |
| 11 | + name: "Extensions", |
| 12 | + display: true, |
| 13 | + interpolation: "d3.interpolateRainbow", |
| 14 | + type: "bar" |
| 15 | + }, |
| 16 | + { |
| 17 | + name: "Licenses", |
| 18 | + display: true, |
| 19 | + interpolation: "d3.interpolateCool", |
| 20 | + type: "bar" |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "Warnings", |
| 24 | + display: true, |
| 25 | + type: "horizontalBar", |
| 26 | + interpolation: "d3.interpolateInferno" |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Flags", |
| 30 | + display: true, |
| 31 | + type: "horizontalBar", |
| 32 | + interpolation: "d3.interpolateSinebow" |
| 33 | + } |
| 34 | + ] |
| 35 | +}; |
| 36 | +export async function main(repository, options) { |
| 37 | + const { |
| 38 | + theme, |
| 39 | + includesAllDeps, |
| 40 | + title, |
| 41 | + reporters |
| 42 | + } = options; |
| 43 | + |
| 44 | + const [organizationPrefixOrRepo, repo] = repository.split("/"); |
| 45 | + |
| 46 | + const formattedReporters = new Set(Array.isArray(reporters) ? reporters : [reporters]); |
| 47 | + for (const reporter of formattedReporters) { |
| 48 | + if (!kSupportedReporters.has(reporter)) { |
| 49 | + console.error(`Reporter '${reporter}' is not supported`); |
| 50 | + process.exit(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const reportPayload = { |
| 55 | + ...kReportPayload, |
| 56 | + npm: { |
| 57 | + organizationPrefix: repo === undefined ? null : organizationPrefixOrRepo, |
| 58 | + packages: [repo === undefined ? organizationPrefixOrRepo : repo] |
| 59 | + }, |
| 60 | + theme, |
| 61 | + title, |
| 62 | + reporters: [...formattedReporters], |
| 63 | + saveOnDisk: true |
| 64 | + }; |
| 65 | + const scannerPayload = await scanner.from(repository); |
| 66 | + |
| 67 | + const reportPath = await report( |
| 68 | + includesAllDeps ? scannerPayload.dependencies : { [repository]: scannerPayload.dependencies[repository] }, |
| 69 | + reportPayload, |
| 70 | + { |
| 71 | + reportOutputLocation: process.cwd(), |
| 72 | + savePDFOnDisk: formattedReporters.has("pdf"), |
| 73 | + saveHTMLOnDisk: formattedReporters.has("html") |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + console.log("Successfully generated report", reportPath); |
| 78 | +} |
0 commit comments