Skip to content

Commit

Permalink
Merge pull request #22 from sw-yx/feat/error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
swyxio authored May 11, 2020
2 parents c1ec47d + 8a7a8f2 commit 74aa06b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
}
const results = await pluginCore.runPa11y({
htmlFilePaths,
build,
debugMode
});

Expand Down
12 changes: 10 additions & 2 deletions plugin/pluginCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const pa11y = require('pa11y');
const readdirp = require('readdirp')
const { isDirectory, isFile } = require('path-type')

exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
let results = await Promise.all(htmlFilePaths.map(pa11y));
exports.runPa11y = async function({ htmlFilePaths, build, testMode, debugMode }) {
let results = await Promise.all(htmlFilePaths.map(htmlFilePath => runPa11yOnFile(htmlFilePath, build)));
results = results
.filter((res) => res.issues.length)
.map((res) =>
Expand All @@ -25,6 +25,14 @@ exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
return flattenedResults;
};

const runPa11yOnFile = async function(htmlFilePath, build) {
try {
return await pa11y(htmlFilePath)
} catch (error) {
build.failBuild(`pa11y failed`, { error })
}
}

exports.generateFilePaths = async function({
fileAndDirPaths, // array, mix of html and directories
PUBLISH_DIR,
Expand Down
3 changes: 2 additions & 1 deletion tests/runPa11y/this.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const path = require('path');
const pluginCore = require('../../plugin/pluginCore.js');
test('runPa11y works', async () => {
const results = await pluginCore.runPa11y({
htmlFilePaths: [path.join(__dirname, 'publishDir/index.html')]
htmlFilePaths: [path.join(__dirname, 'publishDir/index.html')],
build: { failBuild() {} }
});
expect(results).toMatchSnapshot();
});

0 comments on commit 74aa06b

Please sign in to comment.