Skip to content

Commit dd303be

Browse files
committed
Update package meta info
1 parent 211085f commit dd303be

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

lib/config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const DEFAULT_CHECK_PATHS = ['/'];
2+
const DEFAULT_FAIL_WITH_ISSUES = true;
3+
const DEFAULT_IGNORE_DIRECTORIES = [];
4+
const PA11Y_DEFAULT_STANDARD = 'WCAG2AA';
5+
const PA11Y_RUNNERS = ['axe'];
6+
const PA11Y_USER_AGENT = 'netlify-plugin-a11y';
7+
8+
const DEFAULT_INPUTS = {
9+
checkPaths: DEFAULT_CHECK_PATHS,
10+
11+
}
12+
const getConfiguration = ({ constants: { PUBLISH_DIR }, inputs: { checkPaths, debugMode, ignoreDirectories, failWithIssues, standard } = {
13+
14+
}, }) => {
15+
return {
16+
absolutePublishDir: PUBLISH_DIR || process.env.PUBLISH_DIR,
17+
checkPaths: checkPaths || DEFAULT_CHECK_PATHS,
18+
debugMode: debugMode || false,
19+
ignoreDirectories: ignoreDirectories || DEFAULT_IGNORE_DIRECTORIES,
20+
failWithIssues: failWithIssues !== undefined ? failWithIssues : DEFAULT_FAIL_WITH_ISSUES,
21+
pa11yOpts: {
22+
runners: PA11Y_RUNNERS,
23+
userAgent: PA11Y_USER_AGENT,
24+
standard: standard || PA11Y_DEFAULT_STANDARD,
25+
},
26+
};
27+
};
28+
module.exports = {
29+
getConfiguration,
30+
};

lib/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { getConfiguration } = require('./config');
2+
const pluginCore = require('./pluginCore');
3+
module.exports = {
4+
async onPostBuild({ constants, inputs, utils: { build } }) {
5+
try {
6+
const { absolutePublishDir, checkPaths, debugMode, ignoreDirectories, pa11yOpts, failWithIssues } = getConfiguration({ constants, inputs });
7+
const htmlFilePaths = await pluginCore.generateFilePaths({
8+
absolutePublishDir,
9+
ignoreDirectories,
10+
fileAndDirPaths: checkPaths,
11+
});
12+
if (debugMode) {
13+
console.log({ htmlFilePaths });
14+
}
15+
const { report, issueCount } = await pluginCore.runPa11y({
16+
build,
17+
htmlFilePaths,
18+
pa11yOpts,
19+
});
20+
if (issueCount > 0) {
21+
const postRunMsg = `Pa11y found ${issueCount} accessibility violations on your site! Check the logs above for more information.`;
22+
console.log(report);
23+
if (failWithIssues) {
24+
build.failBuild(postRunMsg);
25+
}
26+
else {
27+
console.warn(postRunMsg);
28+
}
29+
}
30+
}
31+
catch (err) {
32+
build.failBuild(err.message);
33+
}
34+
},
35+
};

lib/pluginCore.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const pa11y = require('pa11y');
2+
const { extname } = require('path');
3+
const { isDirectory, isFile } = require('path-type');
4+
const { results: cliReporter } = require('pa11y/lib/reporters/cli');
5+
const readdirp = require('readdirp');
6+
const EMPTY_ARRAY = [];
7+
const ASTERISK = '*';
8+
const HTML_EXT = '.html';
9+
const GLOB_HTML = '*.html';
10+
exports.runPa11y = async function ({ htmlFilePaths, pa11yOpts, build }) {
11+
let issueCount = 0;
12+
const results = await Promise.all(htmlFilePaths.map(async (path) => {
13+
try {
14+
const res = await pa11y(path, pa11yOpts);
15+
if (res.issues && res.issues.length) {
16+
issueCount += res.issues.length;
17+
return cliReporter(res);
18+
}
19+
}
20+
catch (error) {
21+
build.failBuild('pa11y failed', { error });
22+
}
23+
}));
24+
return {
25+
issueCount,
26+
report: results.join(''),
27+
};
28+
};
29+
exports.generateFilePaths = async function ({ fileAndDirPaths, ignoreDirectories, absolutePublishDir, }) {
30+
const excludeDirGlobs = ignoreDirectories.map((dir) => `!${dir.replace(/^\/+/, '')}`);
31+
const htmlFilePaths = await Promise.all(fileAndDirPaths.map((fileAndDirPath) => findHtmlFiles(`${absolutePublishDir}${fileAndDirPath}`, excludeDirGlobs)));
32+
return [].concat(...htmlFilePaths);
33+
};
34+
const findHtmlFiles = async function (fileAndDirPath, directoryFilter) {
35+
if (await isDirectory(fileAndDirPath)) {
36+
const filePaths = [];
37+
const stream = readdirp(fileAndDirPath, {
38+
fileFilter: GLOB_HTML,
39+
directoryFilter: !!directoryFilter.length ? directoryFilter : ASTERISK,
40+
});
41+
for await (const { fullPath } of stream) {
42+
filePaths.push(fullPath);
43+
}
44+
return filePaths;
45+
}
46+
if (!(await isFile(fileAndDirPath))) {
47+
console.warn(`Folder ${fileAndDirPath} was provided in "checkPaths", but does not exist - it either indicates something went wrong with your build, or you can simply delete this folder from your "checkPaths" in netlify.toml`);
48+
return EMPTY_ARRAY;
49+
}
50+
if (extname(fileAndDirPath) !== HTML_EXT) {
51+
return EMPTY_ARRAY;
52+
}
53+
return [fileAndDirPath];
54+
};

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"a11y"
2222
],
2323
"author": "swyx <swyx@dontemail.me>",
24+
"contributors": [
25+
"EJ Mason <ej.mason@dontemail.me>"
26+
],
2427
"license": "MIT",
2528
"engines": {
2629
"node": ">=12.13.0"
@@ -30,8 +33,8 @@
3033
"gh-release": "^6.0.1",
3134
"jest": "^27.3.1"
3235
},
33-
"repository": "https://github.com/sw-yx/netlify-plugin-a11y",
36+
"repository": "https://github.com/netlify-labs/netlify-plugin-a11y",
3437
"bugs": {
35-
"url": "https://github.com/sw-yx/netlify-plugin-a11y/issues"
38+
"url": "https://github.com/netlify-labs/netlify-plugin-a11y/issues"
3639
}
37-
}
40+
}

0 commit comments

Comments
 (0)