Skip to content

Commit

Permalink
chore: Started work on #43
Browse files Browse the repository at this point in the history
  • Loading branch information
nprail committed Feb 24, 2021
1 parent 05b0c54 commit 5be1b64
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from 'fs-extra'
import open from 'open'
import path from 'path'

import { reporter } from './lib/reporter'
import { generateReport } from './lib/reporter'
import pkg from '../package.json'

updateNotifier({ pkg }).notify()
Expand Down Expand Up @@ -57,7 +57,7 @@ program

const genReport = async (
data: any,
output = 'npm-audit.html',
outputFile = 'npm-audit.html',
template: string,
theme = 'light',
openBrowser = false,
Expand All @@ -72,17 +72,17 @@ const genReport = async (
const templateFile =
template || path.join(__dirname, '../../src/templates/template.hbs')

const modifiedData = await reporter(data, templateFile, output, theme)
const modifiedData = await generateReport({ data, templateFile, outputFile, theme })

if (modifiedData.metadata.vulnerabilities.total > 0 && fatalExitCode) {
process.exitCode = 1
}

console.log(`Vulnerability snapshot saved at ${output}`)
console.log(`Vulnerability snapshot saved at ${outputFile}`)

if (openBrowser) {
console.log('Opening report in default browser...')
await open(path.resolve(output))
await open(path.resolve(outputFile))
}
} catch (err) {
console.log('An error occurred!')
Expand Down
50 changes: 41 additions & 9 deletions src/lib/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@ const severityMap: {
}
}

export interface ReportOverview {
totalVulnerabilities: number
totalDependencies: number
critical: number
high: number
moderate: number
low: number
info: number
}
export interface Report {
createdAt: Date
overview: ReportOverview
advisories: any[]
}

export interface Reporter {
transformReport(data: any): Promise<Report>
}

export class NpmAuditReportVersion1 implements Reporter {
async transformReport(data: any): Promise<Report> {
return
}
}

const generateTemplate = async (data: any, template: string) => {
const htmlTemplate = await fs.readFile(template, 'utf8')
return Handlebars.compile(htmlTemplate)(data)
Expand All @@ -48,10 +73,10 @@ const writeReport = async (report, output: string) => {
const modifyData = async data => {
const vulnerabilities = data.metadata.vulnerabilities || []

// for (const act in data.actions) {
// const action = data.actions[act]
// console.log(action)
// }
// for (const act in data.actions) {
// const action = data.actions[act]
// console.log(action)
// }

// calculate totals
let total = 0
Expand All @@ -65,12 +90,19 @@ const modifyData = async data => {
return data
}

export const reporter = async (
data: any,
templateFile: string,
outputFile: string,
export interface GenerateReportOptions {
data: any
templateFile: string
outputFile: string
theme: string
): Promise<any> => {
}

export const generateReport = async ({
data,
templateFile,
outputFile,
theme
}: GenerateReportOptions): Promise<any> => {
try {
if (!data.metadata) {
if (data.updated) {
Expand Down

0 comments on commit 5be1b64

Please sign in to comment.