Skip to content

allow explicitly ignoring directories #21

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ package = "netlify-plugin-a11y"
]

# # optional config
# ignoreDirectories = ['/admin'] # explicitly ignore these directories

# resultMode = "warn" # is "error" by default

# # Developer only
Expand Down
2 changes: 2 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: netlify-plugin-a11y
inputs:
- name: checkPaths
required: true
- name: ignoreDirectories
required: false
- name: resultMode
default: error
- name: debugMode
Expand Down
3 changes: 2 additions & 1 deletion plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const pluginCore = require('./pluginCore');

module.exports = {
async onPostBuild({
inputs: { checkPaths, resultMode, debugMode },
inputs: { checkPaths, ignoreDirectories, resultMode, debugMode },
constants: { PUBLISH_DIR },
utils: { build }
}) {
const htmlFilePaths = await pluginCore.generateFilePaths({
fileAndDirPaths: checkPaths,
ignoreDirectories: ignoreDirectories || [],
PUBLISH_DIR
});
if (debugMode) {
Expand Down
16 changes: 13 additions & 3 deletions plugin/pluginCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {

exports.generateFilePaths = async function({
fileAndDirPaths, // array, mix of html and directories
ignoreDirectories = [],
PUBLISH_DIR,
testMode,
debugMode
}) {
const excludeDirGlobs = ignoreDirectories.map(
// add ! and strip leading slash
(dir) => `!${dir.replace(/^\/+/, "")}`
);
const htmlFilePaths = await Promise.all(
fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`))
fileAndDirPaths.map(fileAndDirPath =>
findHtmlFiles(`${PUBLISH_DIR}${fileAndDirPath}`, excludeDirGlobs)
)
)
return [].concat(...htmlFilePaths)
};

const findHtmlFiles = async function(fileAndDirPath) {
const findHtmlFiles = async function (fileAndDirPath, directoryFilter) {
if (await isDirectory(fileAndDirPath)) {
const fileInfos = await readdirp.promise(fileAndDirPath, { fileFilter: '*.html' })
const fileInfos = await readdirp.promise(fileAndDirPath, {
fileFilter: '*.html',
directoryFilter
})
return fileInfos.map(({ fullPath }) => fullPath)
}

Expand Down
10 changes: 10 additions & 0 deletions tests/generateFilePaths/publishDir/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body></body>
</html>
28 changes: 28 additions & 0 deletions tests/generateFilePaths/this.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,31 @@ test('generateFilePaths works', async () => {
});
expect(results).toMatchSnapshot();
});

const pathInResults = (expectedPath, results) => {
return results.findIndex((r) => r.endsWith(expectedPath)) != -1;
};

test("ignoreDirectories works including leading slash", async () => {
const results = await pluginCore.generateFilePaths({
fileAndDirPaths: ["/"],
ignoreDirectories: ["/admin"],
PUBLISH_DIR,
});
expect(pathInResults("publishDir/blog/post1.html", results)).toBe(true);
expect(pathInResults("publishDir/about.html", results)).toBe(true);
expect(pathInResults("publishDir/index.html", results)).toBe(true);
expect(pathInResults("publishDir/admin/index.html", results)).toBe(false);
});

test("ignoreDirectories works without leading slash", async () => {
const results = await pluginCore.generateFilePaths({
fileAndDirPaths: ["/"],
ignoreDirectories: ["admin"],
PUBLISH_DIR,
});
expect(pathInResults("publishDir/blog/post1.html", results)).toBe(true);
expect(pathInResults("publishDir/about.html", results)).toBe(true);
expect(pathInResults("publishDir/index.html", results)).toBe(true);
expect(pathInResults("publishDir/admin/index.html", results)).toBe(false);
});