Skip to content

Commit 4f6ae23

Browse files
author
Liliia Pelypenko
committed
Added return of HTML file content and option to stop html file creation
1 parent b8a1127 commit 4f6ae23

File tree

4 files changed

+1773
-9
lines changed

4 files changed

+1773
-9
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ Notes:
1111

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

14+
`createHtmlReport` returns HTML content that can be additionally used for specific integrations.
15+
16+
If only HTML content needed, user can pass `doNotCreateReportFile: true` to stop report file creation.
17+
18+
Suggestion on how to use this library if you don't need a report file but need only HTML it produces:
19+
20+
```javascript
21+
const reportHTML = createHtmlReport({
22+
results: rawAxeResults,
23+
options: {
24+
projectKey: 'I need only raw HTML',
25+
doNotCreateReportFile: true,
26+
},
27+
});
28+
console.log('reportHTML will have full content of HTML file.');
29+
// suggestion on how to create file by yourself
30+
if (!fs.existsSync('build/reports/saveReportHere.html')) {
31+
fs.mkdirSync('build/reports', {
32+
recursive: true,
33+
});
34+
}
35+
fs.writeFileSync('build/reports/saveReportHere.html', reportHTML);
36+
```
37+
1438
## Install
1539

1640
```

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export interface Options {
1010
outputDir?: string;
1111
projectKey?: string;
1212
customSummary?: string;
13-
outputDirPath?: string
13+
outputDirPath?: string;
14+
doNotCreateReportFile?: boolean;
1415
}
1516

1617
export interface CreateReport {
@@ -25,7 +26,7 @@ export interface PreparedResults {
2526
inapplicable?: Result[];
2627
}
2728

28-
export function createHtmlReport({ results, options }: CreateReport): void {
29+
export function createHtmlReport({ results, options }: CreateReport): string {
2930
if (!results.violations) {
3031
throw new Error(
3132
"'violations' is required for HTML accessibility report. Example: createHtmlReport({ results : { violations: Result[] } })"
@@ -58,13 +59,21 @@ export function createHtmlReport({ results, options }: CreateReport): void {
5859
hasAxeRawResults: Boolean(results?.timestamp),
5960
rules: prepareAxeRules(results?.toolOptions?.rules || {}),
6061
});
61-
saveHtmlReport({
62-
htmlContent,
63-
reportFileName: options?.reportFileName,
64-
outputDir: options?.outputDir,
65-
outputDirPath: options?.outputDirPath
66-
});
62+
if (options?.doNotCreateReportFile === true) {
63+
console.info('Report file will not be created because user passed options.doNotCreateReportFile = true. Use HTML output of the function to create report file');
64+
} else {
65+
saveHtmlReport({
66+
htmlContent,
67+
reportFileName: options?.reportFileName,
68+
outputDir: options?.outputDir,
69+
outputDirPath: options?.outputDirPath
70+
});
71+
}
72+
73+
return htmlContent;
6774
} catch (e) {
6875
console.warn(`HTML report was not created due to the error ${e.message}`);
76+
77+
return `Failed to create HTML report due to an error ${e.message}`;
6978
}
7079
}

0 commit comments

Comments
 (0)