From bdbd21410e807647c1f8cb207f7642a49c11e7a1 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 4 May 2020 16:05:12 +0200 Subject: [PATCH 1/2] Fix HTML crawling --- package-lock.json | 16 ++++++++++++-- package.json | 4 +++- plugin/pluginCore.js | 51 +++++++++++++++++--------------------------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 148f84b..13a50e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5536,6 +5536,11 @@ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", "dev": true }, + "path-type": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", + "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==" + }, "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", @@ -5550,8 +5555,7 @@ "picomatch": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.1.tgz", - "integrity": "sha512-ISBaA8xQNmwELC7eOjqFKMESB2VIqt4PPDD0nsS95b/9dZXvVKOlz9keMSnoGGKcOHXfTvDD6WMaRoSc9UuhRA==", - "dev": true + "integrity": "sha512-ISBaA8xQNmwELC7eOjqFKMESB2VIqt4PPDD0nsS95b/9dZXvVKOlz9keMSnoGGKcOHXfTvDD6WMaRoSc9UuhRA==" }, "pify": { "version": "3.0.0", @@ -5786,6 +5790,14 @@ "util-deprecate": "~1.0.1" } }, + "readdirp": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.4.0.tgz", + "integrity": "sha512-0xe001vZBnJEK+uKcj8qOhyAKPzIT+gStxWr3LCB0DwcXR5NZJ3IaC+yGnHCYzB/S7ov3m3EEbZI2zeNvX+hGQ==", + "requires": { + "picomatch": "^2.2.1" + } + }, "realpath-native": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/realpath-native/-/realpath-native-1.1.0.tgz", diff --git a/package.json b/package.json index 15d9ae6..3b05b9f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "plugin/index.js", "dependencies": { "chalk": "^3.0.0", - "pa11y": "^5.3.0" + "pa11y": "^5.3.0", + "path-type": "^4.0.0", + "readdirp": "^3.4.0" }, "scripts": { "build": "netlify build", diff --git a/plugin/pluginCore.js b/plugin/pluginCore.js index a2805e2..0e5d0ef 100644 --- a/plugin/pluginCore.js +++ b/plugin/pluginCore.js @@ -1,9 +1,8 @@ -const pa11y = require('pa11y'); -const path = require('path'); -const fs = require('fs'); +const { extname } = require('path') -const { promisify } = require('util'); -const readDir = promisify(fs.readdir); +const pa11y = require('pa11y'); +const readdirp = require('readdirp') +const { isDirectory } = require('path-type') exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) { let results = await Promise.all(htmlFilePaths.map(pa11y)); @@ -32,34 +31,24 @@ exports.generateFilePaths = async function({ testMode, debugMode }) { - let htmlFilePaths = []; - for (fileAndDirPath of fileAndDirPaths) { - const fullDirPath = path.join(PUBLISH_DIR, fileAndDirPath); - if (fs.statSync(fullDirPath).isDirectory()) { - let subPaths = await walk(fullDirPath); - htmlFilePaths = htmlFilePaths.concat(subPaths); - } else { - htmlFilePaths.push(fullDirPath); - } - } - return htmlFilePaths; + const htmlFilePaths = await Promise.all( + fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`)) + ) + return [].concat(...htmlFilePaths) }; -var walk = async function(dir, filelist) { - var files = await readDir(dir); - filelist = filelist || []; - await Promise.all( - files.map(async function(file) { - const dirfile = path.join(dir, file); - if (fs.statSync(dirfile).isDirectory()) { - filelist = await walk(dirfile + '/', filelist); - } else { - if (dirfile.endsWith('.html')) filelist.push(dirfile); - } - }) - ); - return filelist; -}; +const findHtmlFiles = async function(fileAndDirPath) { + if (await isDirectory(fileAndDirPath)) { + const fileInfos = await readdirp.promise(fileAndDirPath, { fileFilter: '*.html' }) + return fileInfos.map(({ fullPath }) => fullPath) + } + + if (extname(fileAndDirPath) !== '.html') { + return [] + } + + return [fileAndDirPath] +} // res: // [ { code: 'WCAG2AA.Principle1.Guideline1_1.1_1_1.H37', From 4bd568e930b33b23e5974e55a6131831e397bee1 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 4 May 2020 17:12:54 +0200 Subject: [PATCH 2/2] Validate that `checkPaths` exist --- plugin/pluginCore.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugin/pluginCore.js b/plugin/pluginCore.js index 0e5d0ef..9d30b55 100644 --- a/plugin/pluginCore.js +++ b/plugin/pluginCore.js @@ -2,7 +2,7 @@ const { extname } = require('path') const pa11y = require('pa11y'); const readdirp = require('readdirp') -const { isDirectory } = require('path-type') +const { isDirectory, isFile } = require('path-type') exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) { let results = await Promise.all(htmlFilePaths.map(pa11y)); @@ -43,6 +43,11 @@ const findHtmlFiles = async function(fileAndDirPath) { return fileInfos.map(({ fullPath }) => fullPath) } + if (!(await isFile(fileAndDirPath))) { + 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`) + return [] + } + if (extname(fileAndDirPath) !== '.html') { return [] }