import { AICrawler , CombinedScanResult , AICombinedReportDataConverter , ScanArguments , CrawlerParametersBuilder , BaselineOptionsBuilder , BaselineFileUpdater , } from 'accessibility-insights-scan' ; import { inject , injectable } from 'inversify' ; import * as util from 'util' ; import { iocTypes } from '../ioc/ioc-types' ; import { LocalFileServer } from '../local-file-server' ; import { Logger } from '../logger/logger' ; import { AllProgressReporter } from '../progress-reporter/all-progress-reporter' ; import { PromiseUtils } from '../utils/promise-utils' ; import { CombinedReportParameters } from 'accessibility-insights-report' ; import { toolName } from '../content/strings' ; import { AxeInfo } from '../axe/axe-info' ; import { ConsolidatedReportGenerator } from '../report/consolidated-report-generator' ; import { CrawlArgumentHandler } from './crawl-argument-handler' ; import { TaskConfig } from '../task-config' ; import { isEmpty } from 'lodash' ; @ injectable ( ) export class Scanner { constructor ( @ inject ( AICrawler ) private readonly crawler , @ inject ( ConsolidatedReportGenerator ) private readonly reportGenerator , @ inject ( AllProgressReporter ) private readonly allProgressReporter , @ inject ( LocalFileServer ) private readonly fileServer , @ inject ( PromiseUtils ) private readonly promiseUtils , @ inject ( AxeInfo ) private readonly axeInfo , @ inject ( AICombinedReportDataConverter ) private readonly combinedReportDataConverter , @ inject ( iocTypes . Process ) protected readonly currentProcess , @ inject ( Logger ) private readonly logger , @ inject ( CrawlArgumentHandler ) private readonly crawlArgumentHandler , @ inject ( iocTypes . TaskConfig ) private readonly taskConfig , @ inject ( CrawlerParametersBuilder ) private readonly crawlerParametersBuilder , @ inject ( BaselineOptionsBuilder ) private readonly baselineOptionsBuilder , @ inject ( BaselineFileUpdater ) private readonly baselineFileUpdater ) { } public async scan ( ) { const scanTimeoutMsec = this . taskConfig . getScanTimeout ( ) ; await this . promiseUtils . waitFor ( this . invokeScan ( ) , scanTimeoutMsec , async ( ) => { this . logger . logError ( `Scan timed out after ${ scanTimeoutMsec / 1000 } seconds` ) ; this . currentProcess . exit ( 1 ) ; } ) ; } private async invokeScan ( ) { let scanArguments ; let localServerUrl ; try { await this . allProgressReporter . start ( ) ; if ( isEmpty ( this . taskConfig . getUrl ( ) ) ) { localServerUrl = await this . fileServer . start ( ) ; } scanArguments = this . crawlArgumentHandler . processScanArguments ( localServerUrl ) ; this . logger . logInfo ( `Starting accessibility scanning of URL ${ scanArguments . url }` ) ; this . logger . logInfo ( `Chrome app executable: ${ scanArguments . chromePath ?? 'system default' }` ) ; const crawlerParameters = this . crawlerParametersBuilder . build ( scanArguments ) ; const scanStarted = new Date ( ) ; const combinedScanResult = await this . crawler . crawl ( crawlerParameters , this . baselineOptionsBuilder . build ( scanArguments ) ) ; const scanEnded = new Date ( ) ; const combinedReportParameters = this . getCombinedReportParameters ( combinedScanResult , scanStarted , scanEnded ) ; this . reportGenerator . generateReport ( combinedReportParameters ) ; await this . baselineFileUpdater . updateBaseline ( scanArguments , combinedScanResult . baselineEvaluation ) ; await this . allProgressReporter . completeRun ( combinedReportParameters , combinedScanResult . baselineEvaluation ) ; } catch ( error ) { this . logger . trackExceptionAny ( error , `An error occurred while scanning website page ${ scanArguments ?. url }` ) ; await this . allProgressReporter . failRun ( util . inspect ( error ) ) ; } finally { this . fileServer . stop ( ) ; this . logger . logInfo ( `Accessibility scanning of URL ${ scanArguments ?. url } completed` ) ; } } private getCombinedReportParameters ( combinedScanResult , scanStarted , scanEnded ) { const scanResultData = { baseUrl : combinedScanResult . scanMetadata . baseUrl ?? 'n/a' , basePageTitle : combinedScanResult . scanMetadata . basePageTitle , scanEngineName : toolName , axeCoreVersion : this . axeInfo . version , browserUserAgent : combinedScanResult . scanMetadata . userAgent , urlCount : combinedScanResult . urlCount , scanStarted , scanEnded , browserResolution : '1920x1080' , } ; return this . combinedReportDataConverter . convertCrawlingResults ( combinedScanResult . combinedAxeResults , scanResultData ) ; } }