1- // @ts -check
1+ import pa11y from 'pa11y'
2+ import { extname , join } from 'path'
3+ import { isDirectory , isFile } from 'path-type'
4+ import { results as cliReporter } from './reporter'
5+ import readdirp from 'readdirp'
6+ import { StaticServer , SERVER_ADDRESS } from './server'
27
3- const pa11y = require ( 'pa11y' )
4- const { extname, join } = require ( 'path' )
5- const { isDirectory, isFile } = require ( 'path-type' )
6- const { results : cliReporter } = require ( './reporter' )
7- const readdirp = require ( 'readdirp' )
8- const { StaticServer, SERVER_ADDRESS } = require ( './server' )
8+ import type { NetlifyPluginUtils } from '@netlify/build'
9+ import type { Pa11yOpts } from './config'
910
1011const EMPTY_ARRAY = [ ]
11- const ASTERISK = '*'
12+ const ASTERISK = '*' ;
1213const HTML_EXT = '.html'
1314const GLOB_HTML = '*.html'
1415
15- exports . runPa11y = async function ( { build, htmlFilePaths, pa11yOpts, publishDir } ) {
16+ export const runPa11y = async function ( {
17+ build,
18+ htmlFilePaths,
19+ pa11yOpts,
20+ publishDir,
21+ } : {
22+ build : NetlifyPluginUtils [ 'build' ]
23+ htmlFilePaths : string [ ]
24+ pa11yOpts : Pa11yOpts
25+ publishDir : string
26+ } ) {
1627 let issueCount = 0
1728
1829 const staticServer = new StaticServer ( publishDir ) . listen ( )
1930
2031 const results = await Promise . all (
21- htmlFilePaths . map ( async ( /** @type { string } */ filePath ) => {
32+ htmlFilePaths . map ( async ( filePath ) => {
2233 try {
2334 const res = await pa11y ( join ( SERVER_ADDRESS , filePath ) , pa11yOpts )
2435 if ( res . issues . length ) {
@@ -41,10 +52,14 @@ exports.runPa11y = async function ({ build, htmlFilePaths, pa11yOpts, publishDir
4152 }
4253}
4354
44- exports . generateFilePaths = async function ( {
55+ export const generateFilePaths = async function ( {
4556 fileAndDirPaths, // array, mix of html and directories
4657 ignoreDirectories,
4758 publishDir,
59+ } : {
60+ fileAndDirPaths : string [ ]
61+ ignoreDirectories : string [ ]
62+ publishDir : string
4863} ) {
4964 const directoryFilter =
5065 ignoreDirectories . length === 0
@@ -56,10 +71,11 @@ exports.generateFilePaths = async function ({
5671 const htmlFilePaths = await Promise . all (
5772 fileAndDirPaths . map ( ( fileAndDirPath ) => findHtmlFiles ( `${ publishDir } ${ fileAndDirPath } ` , directoryFilter ) ) ,
5873 )
59- return [ ] . concat ( ...htmlFilePaths )
74+
75+ return [ ] . concat ( ...htmlFilePaths ) as string [ ]
6076}
6177
62- const findHtmlFiles = async function ( fileAndDirPath , directoryFilter ) {
78+ const findHtmlFiles = async function ( fileAndDirPath : string , directoryFilter : '*' | string [ ] ) : Promise < string [ ] > {
6379 if ( await isDirectory ( fileAndDirPath ) ) {
6480 const filePaths = [ ]
6581 const stream = readdirp ( fileAndDirPath , {
0 commit comments