Skip to content

Commit

Permalink
Added return of HTML file content and option to stop html file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
lpelypenko committed Sep 7, 2021
1 parent b8a1127 commit 4f6ae23
Show file tree
Hide file tree
Showing 4 changed files with 1,773 additions and 9 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ Notes:

Please check [sample report output.](https://lpelypenko.github.io/axe-html-reporter/)

`createHtmlReport` returns HTML content that can be additionally used for specific integrations.

If only HTML content needed, user can pass `doNotCreateReportFile: true` to stop report file creation.

Suggestion on how to use this library if you don't need a report file but need only HTML it produces:

```javascript
const reportHTML = createHtmlReport({
results: rawAxeResults,
options: {
projectKey: 'I need only raw HTML',
doNotCreateReportFile: true,
},
});
console.log('reportHTML will have full content of HTML file.');
// suggestion on how to create file by yourself
if (!fs.existsSync('build/reports/saveReportHere.html')) {
fs.mkdirSync('build/reports', {
recursive: true,
});
}
fs.writeFileSync('build/reports/saveReportHere.html', reportHTML);
```

## Install

```
Expand Down
25 changes: 17 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface Options {
outputDir?: string;
projectKey?: string;
customSummary?: string;
outputDirPath?: string
outputDirPath?: string;
doNotCreateReportFile?: boolean;
}

export interface CreateReport {
Expand All @@ -25,7 +26,7 @@ export interface PreparedResults {
inapplicable?: Result[];
}

export function createHtmlReport({ results, options }: CreateReport): void {
export function createHtmlReport({ results, options }: CreateReport): string {
if (!results.violations) {
throw new Error(
"'violations' is required for HTML accessibility report. Example: createHtmlReport({ results : { violations: Result[] } })"
Expand Down Expand Up @@ -58,13 +59,21 @@ export function createHtmlReport({ results, options }: CreateReport): void {
hasAxeRawResults: Boolean(results?.timestamp),
rules: prepareAxeRules(results?.toolOptions?.rules || {}),
});
saveHtmlReport({
htmlContent,
reportFileName: options?.reportFileName,
outputDir: options?.outputDir,
outputDirPath: options?.outputDirPath
});
if (options?.doNotCreateReportFile === true) {
console.info('Report file will not be created because user passed options.doNotCreateReportFile = true. Use HTML output of the function to create report file');
} else {
saveHtmlReport({
htmlContent,
reportFileName: options?.reportFileName,
outputDir: options?.outputDir,
outputDirPath: options?.outputDirPath
});
}

return htmlContent;
} catch (e) {
console.warn(`HTML report was not created due to the error ${e.message}`);

return `Failed to create HTML report due to an error ${e.message}`;
}
}
Loading

0 comments on commit 4f6ae23

Please sign in to comment.