|
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 } = 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,24 @@ 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 (extname(fileAndDirPath) !== '.html') { |
| 47 | + return [] |
| 48 | + } |
| 49 | + |
| 50 | + return [fileAndDirPath] |
| 51 | +} |
63 | 52 |
|
64 | 53 | // res:
|
65 | 54 | // [ { code: 'WCAG2AA.Principle1.Guideline1_1.1_1_1.H37',
|
|
0 commit comments