Skip to content

Commit bdbd214

Browse files
committed
Fix HTML crawling
1 parent 7296e44 commit bdbd214

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

package-lock.json

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "plugin/index.js",
66
"dependencies": {
77
"chalk": "^3.0.0",
8-
"pa11y": "^5.3.0"
8+
"pa11y": "^5.3.0",
9+
"path-type": "^4.0.0",
10+
"readdirp": "^3.4.0"
911
},
1012
"scripts": {
1113
"build": "netlify build",

plugin/pluginCore.js

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const pa11y = require('pa11y');
2-
const path = require('path');
3-
const fs = require('fs');
1+
const { extname } = require('path')
42

5-
const { promisify } = require('util');
6-
const readDir = promisify(fs.readdir);
3+
const pa11y = require('pa11y');
4+
const readdirp = require('readdirp')
5+
const { isDirectory } = require('path-type')
76

87
exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
98
let results = await Promise.all(htmlFilePaths.map(pa11y));
@@ -32,34 +31,24 @@ exports.generateFilePaths = async function({
3231
testMode,
3332
debugMode
3433
}) {
35-
let htmlFilePaths = [];
36-
for (fileAndDirPath of fileAndDirPaths) {
37-
const fullDirPath = path.join(PUBLISH_DIR, fileAndDirPath);
38-
if (fs.statSync(fullDirPath).isDirectory()) {
39-
let subPaths = await walk(fullDirPath);
40-
htmlFilePaths = htmlFilePaths.concat(subPaths);
41-
} else {
42-
htmlFilePaths.push(fullDirPath);
43-
}
44-
}
45-
return htmlFilePaths;
34+
const htmlFilePaths = await Promise.all(
35+
fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`))
36+
)
37+
return [].concat(...htmlFilePaths)
4638
};
4739

48-
var walk = async function(dir, filelist) {
49-
var files = await readDir(dir);
50-
filelist = filelist || [];
51-
await Promise.all(
52-
files.map(async function(file) {
53-
const dirfile = path.join(dir, file);
54-
if (fs.statSync(dirfile).isDirectory()) {
55-
filelist = await walk(dirfile + '/', filelist);
56-
} else {
57-
if (dirfile.endsWith('.html')) filelist.push(dirfile);
58-
}
59-
})
60-
);
61-
return filelist;
62-
};
40+
const findHtmlFiles = async function(fileAndDirPath) {
41+
if (await isDirectory(fileAndDirPath)) {
42+
const fileInfos = await readdirp.promise(fileAndDirPath, { fileFilter: '*.html' })
43+
return fileInfos.map(({ fullPath }) => fullPath)
44+
}
45+
46+
if (extname(fileAndDirPath) !== '.html') {
47+
return []
48+
}
49+
50+
return [fileAndDirPath]
51+
}
6352

6453
// res:
6554
// [ { code: 'WCAG2AA.Principle1.Guideline1_1.1_1_1.H37',

0 commit comments

Comments
 (0)