1- const pa11y = require ( 'pa11y' ) ;
1+ const pa11y = require ( 'pa11y' )
22const { extname } = require ( 'path' )
33const { isDirectory, isFile } = require ( 'path-type' )
4- const { results : cliReporter } = require ( 'pa11y/lib/reporters/cli' ) ;
4+ const { results : cliReporter } = require ( 'pa11y/lib/reporters/cli' )
55const readdirp = require ( 'readdirp' )
66
77const EMPTY_ARRAY = [ ]
88
99exports . runPa11y = async function ( { htmlFilePaths, pa11yOpts, build } ) {
10- let issueCount = 0 ;
11- const results = await Promise . all ( htmlFilePaths . map ( async path => {
12- try {
13- const res = await pa11y ( path , pa11yOpts ) ;
14- if ( res . issues && res . issues . length ) {
15- issueCount += res . issues . length ;
16- return cliReporter ( res )
17- }
18- } catch ( error ) {
19- build . failBuild ( 'pa11y failed' , { error } )
20- }
21- } ) )
10+ let issueCount = 0
11+ const results = await Promise . all (
12+ htmlFilePaths . map ( async ( path ) => {
13+ try {
14+ const res = await pa11y ( path , pa11yOpts )
15+ if ( res . issues && res . issues . length ) {
16+ issueCount += res . issues . length
17+ return cliReporter ( res )
18+ }
19+ } catch ( error ) {
20+ build . failBuild ( 'pa11y failed' , { error } )
21+ }
22+ } ) ,
23+ )
2224
23- return {
24- issueCount,
25- report : results . join ( '' ) ,
26- } ;
27- } ;
25+ return {
26+ issueCount,
27+ report : results . join ( '' ) ,
28+ }
29+ }
2830
29- exports . generateFilePaths = async function ( {
30- fileAndDirPaths, // array, mix of html and directories
31- ignoreDirectories,
32- absolutePublishDir,
33- testMode,
34- debugMode
31+ exports . generateFilePaths = async function ( {
32+ fileAndDirPaths, // array, mix of html and directories
33+ ignoreDirectories,
34+ absolutePublishDir,
35+ testMode,
36+ debugMode,
3537} ) {
36- const excludeDirGlobs = ignoreDirectories . map (
37- // add ! and strip leading slash
38- ( dir ) => `!${ dir . replace ( / ^ \/ + / , "" ) } `
39- ) ;
40- const htmlFilePaths = await Promise . all (
41- fileAndDirPaths . map ( fileAndDirPath =>
42- findHtmlFiles ( `${ absolutePublishDir } ${ fileAndDirPath } ` , excludeDirGlobs )
43- )
44- )
45- return [ ] . concat ( ...htmlFilePaths )
46- } ;
38+ const excludeDirGlobs = ignoreDirectories . map (
39+ // add ! and strip leading slash
40+ ( dir ) => `!${ dir . replace ( / ^ \/ + / , '' ) } ` ,
41+ )
42+ const htmlFilePaths = await Promise . all (
43+ fileAndDirPaths . map ( ( fileAndDirPath ) => findHtmlFiles ( `${ absolutePublishDir } ${ fileAndDirPath } ` , excludeDirGlobs ) ) ,
44+ )
45+ return [ ] . concat ( ...htmlFilePaths )
46+ }
4747
4848const findHtmlFiles = async function ( fileAndDirPath , directoryFilter ) {
49- if ( await isDirectory ( fileAndDirPath ) ) {
50- const fileInfos = await readdirp . promise ( fileAndDirPath , {
51- fileFilter : '*.html' ,
52- directoryFilter : ! ! directoryFilter . length ? directoryFilter : '*'
53- } )
54- return fileInfos . map ( ( { fullPath } ) => fullPath )
55- }
49+ if ( await isDirectory ( fileAndDirPath ) ) {
50+ const fileInfos = await readdirp . promise ( fileAndDirPath , {
51+ fileFilter : '*.html' ,
52+ directoryFilter : ! ! directoryFilter . length ? directoryFilter : '*' ,
53+ } )
54+ return fileInfos . map ( ( { fullPath } ) => fullPath )
55+ }
5656
57- if ( ! ( await isFile ( fileAndDirPath ) ) ) {
58- 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` )
59- return EMPTY_ARRAY
60- }
57+ if ( ! ( await isFile ( fileAndDirPath ) ) ) {
58+ console . warn (
59+ `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` ,
60+ )
61+ return EMPTY_ARRAY
62+ }
6163
62- if ( extname ( fileAndDirPath ) !== '.html' ) {
63- return EMPTY_ARRAY
64- }
64+ if ( extname ( fileAndDirPath ) !== '.html' ) {
65+ return EMPTY_ARRAY
66+ }
6567
66- return [ fileAndDirPath ]
67- }
68+ return [ fileAndDirPath ]
69+ }
0 commit comments