Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

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
51 changes: 20 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 } = 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,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',
Expand Down