Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix HTML crawling #20

Merged
merged 2 commits into from
May 4, 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
16 changes: 14 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 25 additions & 31 deletions plugin/pluginCore.js
Original file line number Diff line number Diff line change
@@ -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, isFile } = require('path-type')

exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
let results = await Promise.all(htmlFilePaths.map(pa11y));
Expand Down Expand Up @@ -32,34 +31,29 @@ 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 (!(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 []
}

return [fileAndDirPath]
}

// res:
// [ { code: 'WCAG2AA.Principle1.Guideline1_1.1_1_1.H37',
Expand Down