Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added html reporting option Closes #11 #21

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/cli/reporter/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { writeFile } from 'fs/promises';
import { CompatData } from '../../types';

export async function createHtml(compatData: Record<string, CompatData>, version: string, path: string = 'compat.html') {
const compatDataKeys = Object.keys(compatData);
const classGreen = "green";
const classRed = "red";
const classYellow = "yellow";

const style = `
h1{
font-family: arial, sans-serif;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
.${classRed}{
color: #ff0000;
}
.${classGreen}{
color: #0f9b4e;
}
.${classYellow}{
color: #ce8d02;
}`

const tableData = compatDataKeys
.map((key) => {
const compatible = compatData[key].compatible;
const compatibleClass = compatible === undefined ? classYellow : compatible ? classGreen : classRed;
return `
<tr>
<td>${key}</td>
<td class="${compatibleClass}">${compatible}</td>
<td>${compatData[key].range}</td>
</tr>
`
})
.join("");

const out = `<!DOCTYPE html>
<html lang="en">
<head>
<style>
${style}
</style>
<title>depngn</title>
</head>
<body>
<h1>Node version: ${version}</h1>
<table>
<tr>
<th>package</th>
<th>compatible</th>
<th>range</th>
</tr>
${tableData}
</table>
</body>
</html>`

await writeFile(path, out);
console.log(`File generated at ${path}`);
}
3 changes: 3 additions & 0 deletions src/cli/reporter/report.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createJson } from './json';
import { createTable} from './table';
import { CompatData } from '../../types';
import { createHtml } from './html';

export function createReport(
compatData: Record<string, CompatData>,
Expand All @@ -12,6 +13,8 @@ export function createReport(
return createTable(compatData, version);
case 'json':
return createJson(compatData, version);
case 'html':
return createHtml(compatData, version);
default:
const wrong = reporter as never;
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion src/cli/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function createUsage() {

Options:
-h, --help output usage information
-r, --reporter which reporter for output. options are: terminal (default), json
-r, --reporter which reporter for output. options are: terminal (default), json, html

Example:
depngn 12.0.0 --reporter=json
Expand Down
2 changes: 1 addition & 1 deletion src/cli/validate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { validate } from 'compare-versions';
import { green, red } from 'kleur/colors';

const REPORTERS = ['terminal', 'json'];
const REPORTERS = ['terminal', 'json', 'html'];

export function validateArgs(nodeVersion: string, reporter: string) {
validateNodeVersion(nodeVersion);
Expand Down