Skip to content

Commit 5d05752

Browse files
committed
allow explicitly ignoring directories
1 parent c1ec47d commit 5d05752

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ package = "netlify-plugin-a11y"
4444
]
4545

4646
# # optional config
47+
# ignoreDirectories = ['/admin'] # explicitly ignore these directories
48+
4749
# resultMode = "warn" # is "error" by default
4850

4951
# # Developer only

manifest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: netlify-plugin-a11y
22
inputs:
33
- name: checkPaths
44
required: true
5+
- name: ignoreDirectories
6+
required: false
57
- name: resultMode
68
default: error
79
- name: debugMode

plugin/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const pluginCore = require('./pluginCore');
99

1010
module.exports = {
1111
async onPostBuild({
12-
inputs: { checkPaths, resultMode, debugMode },
12+
inputs: { checkPaths, ignoreDirectories, resultMode, debugMode },
1313
constants: { PUBLISH_DIR },
1414
utils: { build }
1515
}) {
1616
const htmlFilePaths = await pluginCore.generateFilePaths({
1717
fileAndDirPaths: checkPaths,
18+
ignoreDirectories: ignoreDirectories || [],
1819
PUBLISH_DIR
1920
});
2021
if (debugMode) {

plugin/pluginCore.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,29 @@ exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
2727

2828
exports.generateFilePaths = async function({
2929
fileAndDirPaths, // array, mix of html and directories
30+
ignoreDirectories = [],
3031
PUBLISH_DIR,
3132
testMode,
3233
debugMode
3334
}) {
35+
const excludeDirGlobs = ignoreDirectories.map(
36+
// add ! and strip leading slash
37+
(dir) => `!${dir.replace(/^\/+/, "")}`
38+
);
3439
const htmlFilePaths = await Promise.all(
35-
fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`))
40+
fileAndDirPaths.map(fileAndDirPath =>
41+
findHtmlFiles(`${PUBLISH_DIR}${fileAndDirPath}`, excludeDirGlobs)
42+
)
3643
)
3744
return [].concat(...htmlFilePaths)
3845
};
3946

40-
const findHtmlFiles = async function(fileAndDirPath) {
47+
const findHtmlFiles = async function (fileAndDirPath, directoryFilter) {
4148
if (await isDirectory(fileAndDirPath)) {
42-
const fileInfos = await readdirp.promise(fileAndDirPath, { fileFilter: '*.html' })
49+
const fileInfos = await readdirp.promise(fileAndDirPath, {
50+
fileFilter: '*.html',
51+
directoryFilter
52+
})
4353
return fileInfos.map(({ fullPath }) => fullPath)
4454
}
4555

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Document</title>
8+
</head>
9+
<body></body>
10+
</html>

tests/generateFilePaths/this.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,31 @@ test('generateFilePaths works', async () => {
1111
});
1212
expect(results).toMatchSnapshot();
1313
});
14+
15+
const pathInResults = (expectedPath, results) => {
16+
return results.findIndex((r) => r.endsWith(expectedPath)) != -1;
17+
};
18+
19+
test("ignoreDirectories works including leading slash", async () => {
20+
const results = await pluginCore.generateFilePaths({
21+
fileAndDirPaths: ["/"],
22+
ignoreDirectories: ["/admin"],
23+
PUBLISH_DIR,
24+
});
25+
expect(pathInResults("publishDir/blog/post1.html", results)).toBe(true);
26+
expect(pathInResults("publishDir/about.html", results)).toBe(true);
27+
expect(pathInResults("publishDir/index.html", results)).toBe(true);
28+
expect(pathInResults("publishDir/admin/index.html", results)).toBe(false);
29+
});
30+
31+
test("ignoreDirectories works without leading slash", async () => {
32+
const results = await pluginCore.generateFilePaths({
33+
fileAndDirPaths: ["/"],
34+
ignoreDirectories: ["admin"],
35+
PUBLISH_DIR,
36+
});
37+
expect(pathInResults("publishDir/blog/post1.html", results)).toBe(true);
38+
expect(pathInResults("publishDir/about.html", results)).toBe(true);
39+
expect(pathInResults("publishDir/index.html", results)).toBe(true);
40+
expect(pathInResults("publishDir/admin/index.html", results)).toBe(false);
41+
});

0 commit comments

Comments
 (0)