Skip to content

Commit 74aa06b

Browse files
authored
Merge pull request #22 from sw-yx/feat/error-handling
2 parents c1ec47d + 8a7a8f2 commit 74aa06b

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

plugin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
}
2323
const results = await pluginCore.runPa11y({
2424
htmlFilePaths,
25+
build,
2526
debugMode
2627
});
2728

plugin/pluginCore.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const pa11y = require('pa11y');
44
const readdirp = require('readdirp')
55
const { isDirectory, isFile } = require('path-type')
66

7-
exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
8-
let results = await Promise.all(htmlFilePaths.map(pa11y));
7+
exports.runPa11y = async function({ htmlFilePaths, build, testMode, debugMode }) {
8+
let results = await Promise.all(htmlFilePaths.map(htmlFilePath => runPa11yOnFile(htmlFilePath, build)));
99
results = results
1010
.filter((res) => res.issues.length)
1111
.map((res) =>
@@ -25,6 +25,14 @@ exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
2525
return flattenedResults;
2626
};
2727

28+
const runPa11yOnFile = async function(htmlFilePath, build) {
29+
try {
30+
return await pa11y(htmlFilePath)
31+
} catch (error) {
32+
build.failBuild(`pa11y failed`, { error })
33+
}
34+
}
35+
2836
exports.generateFilePaths = async function({
2937
fileAndDirPaths, // array, mix of html and directories
3038
PUBLISH_DIR,

tests/runPa11y/this.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const path = require('path');
55
const pluginCore = require('../../plugin/pluginCore.js');
66
test('runPa11y works', async () => {
77
const results = await pluginCore.runPa11y({
8-
htmlFilePaths: [path.join(__dirname, 'publishDir/index.html')]
8+
htmlFilePaths: [path.join(__dirname, 'publishDir/index.html')],
9+
build: { failBuild() {} }
910
});
1011
expect(results).toMatchSnapshot();
1112
});

0 commit comments

Comments
 (0)