From 5d05752da9126d46696ba47e1f8032f49a65e574 Mon Sep 17 00:00:00 2001 From: Jeff Hackshaw Date: Tue, 5 May 2020 21:47:50 -0400 Subject: [PATCH] allow explicitly ignoring directories --- README.md | 2 ++ manifest.yml | 2 ++ plugin/index.js | 3 +- plugin/pluginCore.js | 16 +++++++++-- .../publishDir/admin/index.html | 10 +++++++ tests/generateFilePaths/this.test.js | 28 +++++++++++++++++++ 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 tests/generateFilePaths/publishDir/admin/index.html diff --git a/README.md b/README.md index 2ef5bb3..257340a 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ package = "netlify-plugin-a11y" ] # # optional config + # ignoreDirectories = ['/admin'] # explicitly ignore these directories + # resultMode = "warn" # is "error" by default # # Developer only diff --git a/manifest.yml b/manifest.yml index df5c2b8..441706e 100644 --- a/manifest.yml +++ b/manifest.yml @@ -2,6 +2,8 @@ name: netlify-plugin-a11y inputs: - name: checkPaths required: true + - name: ignoreDirectories + required: false - name: resultMode default: error - name: debugMode diff --git a/plugin/index.js b/plugin/index.js index b93e243..36d6cee 100644 --- a/plugin/index.js +++ b/plugin/index.js @@ -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) { diff --git a/plugin/pluginCore.js b/plugin/pluginCore.js index 9d30b55..6eda87f 100644 --- a/plugin/pluginCore.js +++ b/plugin/pluginCore.js @@ -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) } diff --git a/tests/generateFilePaths/publishDir/admin/index.html b/tests/generateFilePaths/publishDir/admin/index.html new file mode 100644 index 0000000..63b4887 --- /dev/null +++ b/tests/generateFilePaths/publishDir/admin/index.html @@ -0,0 +1,10 @@ + + + + + + + Document + + + diff --git a/tests/generateFilePaths/this.test.js b/tests/generateFilePaths/this.test.js index 53cae9a..e2872a3 100644 --- a/tests/generateFilePaths/this.test.js +++ b/tests/generateFilePaths/this.test.js @@ -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); +});