import * as fs from 'fs' ; import { inject , injectable } from 'inversify' ; import { ScanArguments } from '../scan-arguments' ; import { ConsolidatedReportGenerator } from '../report/consolidated-report-generator' ; import { CrawlerParametersBuilder } from '../crawler/crawler-parameters-builder' ; import { AICrawler } from '../crawler/ai-crawler' ; import { BaselineOptionsBuilder } from '../baseline/baseline-options-builder' ; import { OutputFileWriter } from '../files/output-file-writer' ; import { BaselineFileUpdater } from '../baseline/baseline-file-updater' ; import { CommandRunner } from './command-runner' ; @ injectable ( ) export class CrawlerCommandRunner implements CommandRunner { constructor ( @ inject ( AICrawler ) private readonly crawler , @ inject ( CrawlerParametersBuilder ) private readonly crawlerParametersBuilder , @ inject ( ConsolidatedReportGenerator ) private readonly consolidatedReportGenerator , @ inject ( OutputFileWriter ) private readonly outputFileWriter , @ inject ( BaselineOptionsBuilder ) private readonly baselineOptionsBuilder , @ inject ( BaselineFileUpdater ) private readonly baselineFileUpdater , private readonly filesystem = fs , private readonly stdoutWriter = console . log ) { } public async runCommand ( scanArguments ) { if ( this . canRunCommand ( scanArguments ) !== true ) { return ; } const crawlerRunOptions = this . crawlerParametersBuilder . build ( scanArguments ) ; const baselineOptions = this . baselineOptionsBuilder . build ( scanArguments ) ; const scanStarted = new Date ( ) ; const combinedScanResult = await this . crawler . crawl ( crawlerRunOptions , baselineOptions ) ; const scanEnded = new Date ( ) ; this . stdoutWriter ( 'Generating summary scan report...' ) ; const reportContent = await this . consolidatedReportGenerator . generateReport ( combinedScanResult , scanStarted , scanEnded ) ; const reportLocation = this . outputFileWriter . writeToDirectory ( scanArguments . output , 'index' , 'html' , reportContent ) ; this . stdoutWriter ( `Summary report was saved as ${ reportLocation }` ) ; await this . baselineFileUpdater . updateBaseline ( scanArguments , combinedScanResult . baselineEvaluation ) ; } private canRunCommand ( scanArguments ) { if ( this . filesystem . existsSync ( scanArguments . output ) && ! scanArguments . restart && ! scanArguments . continue ) { this . stdoutWriter ( 'The last scan result was found on a disk. Use --continue option to continue scan for the last URL provided, or --restart option to delete the last scan result.' ) ; return false ; } return true ; } }