|
1 | | -const pa11y = require('pa11y'); |
2 | | -const path = require('path'); |
3 | | -const fs = require('fs'); |
| 1 | +const { extname } = require('path') |
4 | 2 |
|
5 | | -const { promisify } = require('util'); |
6 | | -const readDir = promisify(fs.readdir); |
| 3 | +const pa11y = require('pa11y'); |
| 4 | +const readdirp = require('readdirp') |
| 5 | +const { isDirectory, isFile } = require('path-type') |
7 | 6 |
|
8 | 7 | exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) { |
9 | 8 | let results = await Promise.all(htmlFilePaths.map(pa11y)); |
@@ -32,34 +31,29 @@ exports.generateFilePaths = async function({ |
32 | 31 | testMode, |
33 | 32 | debugMode |
34 | 33 | }) { |
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) |
46 | 38 | }; |
47 | 39 |
|
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 (!(await isFile(fileAndDirPath))) { |
| 47 | + 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`) |
| 48 | + return [] |
| 49 | + } |
| 50 | + |
| 51 | + if (extname(fileAndDirPath) !== '.html') { |
| 52 | + return [] |
| 53 | + } |
| 54 | + |
| 55 | + return [fileAndDirPath] |
| 56 | +} |
63 | 57 |
|
64 | 58 | // res: |
65 | 59 | // [ { code: 'WCAG2AA.Principle1.Guideline1_1.1_1_1.H37', |
|
0 commit comments