Skip to content

Commit 0ae5a3a

Browse files
committed
Prettify files
1 parent 9f84603 commit 0ae5a3a

File tree

3 files changed

+108
-121
lines changed

3 files changed

+108
-121
lines changed

plugin/config.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
const DEFAULT_CHECK_PATHS = ['/'];
2-
const DEFAULT_FAIL_WITH_ISSUES = true;
3-
const DEFAULT_IGNORE_DIRECTORIES = [];
1+
const DEFAULT_CHECK_PATHS = ['/']
2+
const DEFAULT_FAIL_WITH_ISSUES = true
3+
const DEFAULT_IGNORE_DIRECTORIES = []
44

5-
const PA11Y_DEFAULT_STANDARD = 'WCAG2AA';
6-
const PA11Y_RUNNERS = ['axe'];
7-
const PA11Y_USER_AGENT = 'netlify-plugin-a11y';
5+
const PA11Y_DEFAULT_STANDARD = 'WCAG2AA'
6+
const PA11Y_RUNNERS = ['axe']
7+
const PA11Y_USER_AGENT = 'netlify-plugin-a11y'
88

99
const getConfiguration = ({
10-
constants: { PUBLISH_DIR },
11-
inputs: { checkPaths, debugMode, ignoreDirectories, failWithIssues, standard }
10+
constants: { PUBLISH_DIR },
11+
inputs: { checkPaths, debugMode, ignoreDirectories, failWithIssues, standard },
1212
}) => {
13-
14-
return {
15-
absolutePublishDir: PUBLISH_DIR || process.env.PUBLISH_DIR,
16-
checkPaths: checkPaths || DEFAULT_CHECK_PATHS,
17-
debugMode: debugMode || false,
18-
ignoreDirectories: ignoreDirectories || DEFAULT_IGNORE_DIRECTORIES,
19-
failWithIssues: failWithIssues !== undefined ? failWithIssues : DEFAULT_FAIL_WITH_ISSUES,
20-
pa11yOpts: {
21-
runners: PA11Y_RUNNERS,
22-
userAgent: PA11Y_USER_AGENT,
23-
standard: standard || PA11Y_DEFAULT_STANDARD,
24-
}
25-
}
13+
return {
14+
absolutePublishDir: PUBLISH_DIR || process.env.PUBLISH_DIR,
15+
checkPaths: checkPaths || DEFAULT_CHECK_PATHS,
16+
debugMode: debugMode || false,
17+
ignoreDirectories: ignoreDirectories || DEFAULT_IGNORE_DIRECTORIES,
18+
failWithIssues: failWithIssues !== undefined ? failWithIssues : DEFAULT_FAIL_WITH_ISSUES,
19+
pa11yOpts: {
20+
runners: PA11Y_RUNNERS,
21+
userAgent: PA11Y_USER_AGENT,
22+
standard: standard || PA11Y_DEFAULT_STANDARD,
23+
},
24+
}
2625
}
2726

2827
module.exports = {
29-
getConfiguration
30-
}
28+
getConfiguration,
29+
}

plugin/index.js

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,37 @@
11
const { getConfiguration } = require('./config')
2-
const pluginCore = require('./pluginCore');
2+
const pluginCore = require('./pluginCore')
33

44
module.exports = {
5-
async onPostBuild({
6-
constants,
7-
inputs,
8-
utils: { build },
9-
}) {
5+
async onPostBuild({ constants, inputs, utils: { build } }) {
6+
try {
7+
const { absolutePublishDir, checkPaths, debugMode, ignoreDirectories, pa11yOpts, failWithIssues } =
8+
getConfiguration({ constants, inputs })
9+
const htmlFilePaths = await pluginCore.generateFilePaths({
10+
absolutePublishDir,
11+
ignoreDirectories,
12+
fileAndDirPaths: checkPaths,
13+
})
14+
if (debugMode) {
15+
console.log({ htmlFilePaths })
16+
}
1017

11-
try {
12-
const {
13-
absolutePublishDir,
14-
checkPaths,
15-
debugMode,
16-
ignoreDirectories,
17-
pa11yOpts,
18-
failWithIssues,
19-
} = getConfiguration({ constants, inputs })
20-
const htmlFilePaths = await pluginCore.generateFilePaths({
21-
absolutePublishDir,
22-
ignoreDirectories,
23-
fileAndDirPaths: checkPaths,
24-
});
25-
if (debugMode) {
26-
console.log({ htmlFilePaths });
27-
}
28-
29-
const { report, issueCount } = await pluginCore.runPa11y({
30-
build,
31-
debugMode,
32-
htmlFilePaths,
33-
pa11yOpts,
34-
});
35-
if (issueCount > 0) {
36-
const postRunMsg = `Pa11y found ${issueCount} accessibility violations on your site! Check the logs above for more information.`
37-
console.log(report);
38-
if (failWithIssues) {
39-
build.failBuild(postRunMsg)
40-
} else {
41-
console.warn(postRunMsg)
42-
}
43-
}
44-
45-
} catch(err) {
46-
build.failBuild(err.message)
47-
}
48-
49-
50-
}
18+
const { report, issueCount } = await pluginCore.runPa11y({
19+
build,
20+
debugMode,
21+
htmlFilePaths,
22+
pa11yOpts,
23+
})
24+
if (issueCount > 0) {
25+
const postRunMsg = `Pa11y found ${issueCount} accessibility violations on your site! Check the logs above for more information.`
26+
console.log(report)
27+
if (failWithIssues) {
28+
build.failBuild(postRunMsg)
29+
} else {
30+
console.warn(postRunMsg)
31+
}
32+
}
33+
} catch (err) {
34+
build.failBuild(err.message)
35+
}
36+
},
5137
}

plugin/pluginCore.js

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
1-
const pa11y = require('pa11y');
1+
const pa11y = require('pa11y')
22
const { extname } = require('path')
33
const { isDirectory, isFile } = require('path-type')
4-
const { results: cliReporter } = require('pa11y/lib/reporters/cli');
4+
const { results: cliReporter } = require('pa11y/lib/reporters/cli')
55
const readdirp = require('readdirp')
66

77
const EMPTY_ARRAY = []
88

99
exports.runPa11y = async function ({ htmlFilePaths, pa11yOpts, build }) {
10-
let issueCount = 0;
11-
const results = await Promise.all(htmlFilePaths.map(async path => {
12-
try {
13-
const res = await pa11y(path, pa11yOpts);
14-
if (res.issues && res.issues.length) {
15-
issueCount += res.issues.length;
16-
return cliReporter(res)
17-
}
18-
} catch (error) {
19-
build.failBuild('pa11y failed', { error })
20-
}
21-
}))
10+
let issueCount = 0
11+
const results = await Promise.all(
12+
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+
} catch (error) {
20+
build.failBuild('pa11y failed', { error })
21+
}
22+
}),
23+
)
2224

23-
return {
24-
issueCount,
25-
report: results.join(''),
26-
};
27-
};
25+
return {
26+
issueCount,
27+
report: results.join(''),
28+
}
29+
}
2830

29-
exports.generateFilePaths = async function({
30-
fileAndDirPaths, // array, mix of html and directories
31-
ignoreDirectories,
32-
absolutePublishDir,
33-
testMode,
34-
debugMode
31+
exports.generateFilePaths = async function ({
32+
fileAndDirPaths, // array, mix of html and directories
33+
ignoreDirectories,
34+
absolutePublishDir,
35+
testMode,
36+
debugMode,
3537
}) {
36-
const excludeDirGlobs = ignoreDirectories.map(
37-
// add ! and strip leading slash
38-
(dir) => `!${dir.replace(/^\/+/, "")}`
39-
);
40-
const htmlFilePaths = await Promise.all(
41-
fileAndDirPaths.map(fileAndDirPath =>
42-
findHtmlFiles(`${absolutePublishDir}${fileAndDirPath}`, excludeDirGlobs)
43-
)
44-
)
45-
return [].concat(...htmlFilePaths)
46-
};
38+
const excludeDirGlobs = ignoreDirectories.map(
39+
// add ! and strip leading slash
40+
(dir) => `!${dir.replace(/^\/+/, '')}`,
41+
)
42+
const htmlFilePaths = await Promise.all(
43+
fileAndDirPaths.map((fileAndDirPath) => findHtmlFiles(`${absolutePublishDir}${fileAndDirPath}`, excludeDirGlobs)),
44+
)
45+
return [].concat(...htmlFilePaths)
46+
}
4747

4848
const findHtmlFiles = async function (fileAndDirPath, directoryFilter) {
49-
if (await isDirectory(fileAndDirPath)) {
50-
const fileInfos = await readdirp.promise(fileAndDirPath, {
51-
fileFilter: '*.html',
52-
directoryFilter: !!directoryFilter.length ? directoryFilter : '*'
53-
})
54-
return fileInfos.map(({ fullPath }) => fullPath)
55-
}
49+
if (await isDirectory(fileAndDirPath)) {
50+
const fileInfos = await readdirp.promise(fileAndDirPath, {
51+
fileFilter: '*.html',
52+
directoryFilter: !!directoryFilter.length ? directoryFilter : '*',
53+
})
54+
return fileInfos.map(({ fullPath }) => fullPath)
55+
}
5656

57-
if (!(await isFile(fileAndDirPath))) {
58-
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`)
59-
return EMPTY_ARRAY
60-
}
57+
if (!(await isFile(fileAndDirPath))) {
58+
console.warn(
59+
`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`,
60+
)
61+
return EMPTY_ARRAY
62+
}
6163

62-
if (extname(fileAndDirPath) !== '.html') {
63-
return EMPTY_ARRAY
64-
}
64+
if (extname(fileAndDirPath) !== '.html') {
65+
return EMPTY_ARRAY
66+
}
6567

66-
return [fileAndDirPath]
67-
}
68+
return [fileAndDirPath]
69+
}

0 commit comments

Comments
 (0)