-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from snyk-tech-services/feat/output-pdf
feat: refactor to accomodate pdf output
- Loading branch information
Showing
17 changed files
with
607 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
version: 2.1 | ||
orbs: | ||
snyk: snyk/[email protected] | ||
puppeteer: threetreeslight/[email protected] | ||
jobs: | ||
build-test-monitor: | ||
docker: | ||
|
@@ -9,6 +10,15 @@ jobs: | |
- checkout | ||
- run: npm install semantic-release @semantic-release/exec pkg --save-dev | ||
- run: npm install | ||
- run: | ||
name: Install Headless Chrome dependencies | ||
command: | | ||
sudo apt-get install -yq \ | ||
gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \ | ||
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \ | ||
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 \ | ||
libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates \ | ||
fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget | ||
- run: npm test | ||
- snyk/scan: | ||
fail-on-issues: true | ||
|
@@ -21,6 +31,15 @@ jobs: | |
steps: | ||
- checkout | ||
- run: npm install | ||
- run: | ||
name: Install Headless Chrome dependencies | ||
command: | | ||
sudo apt-get install -yq \ | ||
gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \ | ||
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \ | ||
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 \ | ||
libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates \ | ||
fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget | ||
- run: npm test | ||
- snyk/scan: | ||
fail-on-issues: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,20 @@ | ||
import * as Handlebars from 'handlebars'; | ||
import * as path from 'path'; | ||
import * as pathLib from 'path'; | ||
import * as fs from 'fs'; | ||
import * as debugLib from 'debug'; | ||
import { writeContentsToFile } from '../../write-contents-to-file'; | ||
|
||
import { LicenseReportData } from '../../generate-org-license-report'; | ||
import { getOrgData, OrgData } from '../../get-org-data'; | ||
const debug = debugLib('snyk-licenses:saveHtmlReport'); | ||
|
||
const debug = debugLib('snyk-licenses:generateHtmlReport'); | ||
const DEFAULT_TEMPLATE = './templates/licenses-view.hbs'; | ||
|
||
export const enum SupportedViews { | ||
ORG_LICENSES = 'org-licenses', | ||
// TODO: support later | ||
// PROJECT_DEPENDENCIES = 'project-dependencies', | ||
} | ||
|
||
const transformDataFunc = { | ||
[SupportedViews.ORG_LICENSES]: transformDataForLicenseView, | ||
// TODO: support later | ||
// [SupportedViews.PROJECT_DEPENDENCIES]: transformDataForDependencyView, | ||
}; | ||
|
||
export async function generateHtmlReport( | ||
orgPublicId: string, | ||
data: LicenseReportData, | ||
templateOverridePath: string | undefined = undefined, | ||
view: SupportedViews = SupportedViews.ORG_LICENSES, | ||
) { | ||
// TODO: add any helpers & data transformations that are useful here | ||
debug('ℹ️ Generating HTML report'); | ||
const hbsTemplate = selectTemplate(view, templateOverridePath); | ||
debug( | ||
`✅ Using template ${ | ||
hbsTemplate === DEFAULT_TEMPLATE ? 'default template' : hbsTemplate | ||
}`, | ||
); | ||
debug(`✅ Registered Handlebars.js partials`); | ||
const htmlTemplate = await compileTemplate(hbsTemplate); | ||
debug(`✅ Compiled template ${hbsTemplate}`); | ||
|
||
const orgData = await getOrgData(orgPublicId); | ||
const transformedData = transformDataFunc[view](orgPublicId, data, orgData); | ||
|
||
return htmlTemplate(transformedData); | ||
} | ||
|
||
function transformDataForLicenseView( | ||
orgPublicId: string, | ||
data: LicenseReportData, | ||
orgData: OrgData, | ||
): { | ||
licenses: LicenseReportData; | ||
orgPublicId: string; | ||
orgData: OrgData; | ||
} { | ||
return { licenses: data, orgPublicId, orgData }; | ||
} | ||
|
||
// TODO: support later | ||
// function transformDataForDependencyView(data: LicenseReportData) { | ||
// return data; | ||
// } | ||
|
||
function selectTemplate(view: SupportedViews, templateOverride?): string { | ||
switch (view) { | ||
case SupportedViews.ORG_LICENSES: | ||
return templateOverride || DEFAULT_TEMPLATE; | ||
// TODO: support later | ||
// case SupportedViews.PROJECT_DEPENDENCIES: | ||
// return templateOverride || '../templates/project-dependencies-view.hbs'; | ||
default: | ||
return DEFAULT_TEMPLATE; | ||
} | ||
} | ||
|
||
async function registerPeerPartial( | ||
templatePath: string, | ||
name: string, | ||
): Promise<void> { | ||
const file = path.join(__dirname, templatePath); | ||
debug(`ℹ️ Registering peer partial template ${file}`); | ||
|
||
const template = await compileTemplate(file); | ||
debug(`✅ Compiled template ${file}`); | ||
|
||
Handlebars.registerPartial(name, template); | ||
} | ||
|
||
async function compileTemplate( | ||
export async function saveHtmlReport( | ||
fileName: string, | ||
): Promise<HandlebarsTemplateDelegate> { | ||
return readFile(path.resolve(__dirname, fileName), 'utf8').then( | ||
Handlebars.compile, | ||
); | ||
} | ||
|
||
function readFile(filePath: string, encoding: string): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
fs.readFile(filePath, encoding, (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(data); | ||
}); | ||
}); | ||
data: string, | ||
): Promise<void> { | ||
if (!data) { | ||
throw new Error('No report data to save!'); | ||
} | ||
const outputFile = pathLib.resolve(__dirname, fileName); | ||
debug(`⏳ Saving generated report to ${outputFile}`); | ||
writeContentsToFile(data, outputFile); | ||
debug(`✅ Saved HTML report to ${outputFile}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
export function generatePdfReport() { | ||
// TODO: find a package that could convert the HTML to pdf | ||
// or where we could use the same handlebars template & css | ||
// to generate the pdf | ||
import * as puppeteer from 'puppeteer'; | ||
import * as debugLib from 'debug'; | ||
|
||
const debug = debugLib('snyk-licenses:generatePdfReport'); | ||
|
||
export async function savePdfReport( | ||
fileName: string, | ||
data: string, | ||
): Promise<void> { | ||
if (!data) { | ||
throw new Error('No report data to save!'); | ||
} | ||
debug(`⏳ Saving PDF to ${fileName}`); | ||
// start browser in headless mode | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
// We set the page content as the generated html by handlebars | ||
await page.setContent(data); | ||
await page.pdf({ path: fileName, format: 'A4' }); | ||
await browser.close(); | ||
debug(`✅ Saved PDF report to ${fileName}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { OrgData } from "./get-org-data"; | ||
|
||
export function generateReportName(orgData: OrgData, view: string): string { | ||
return `${orgData.slug}-${orgData.id}-${view}`; | ||
} |
Oops, something went wrong.