import * as fs from 'fs' ; import { inject , injectable } from 'inversify' ; import { ScanArguments } from '../scan-arguments' ; import { BaselineFileFormatter } from './baseline-file-formatter' ; import { BaselineFileContent , BaselineOptions } from './baseline-types' ; @ injectable ( ) export class BaselineOptionsBuilder { constructor ( @ inject ( BaselineFileFormatter ) private readonly baselineFileFormatter , private readonly fileSystem = fs ) { } public build ( scanArguments ) { if ( scanArguments . baselineFile == null ) { if ( scanArguments . updateBaseline ) { throw new Error ( 'updateBaseline is only supported when baselineFile is specified' ) ; } return null ; } return { baselineContent : this . readPossibleBaselineFile ( scanArguments . baselineFile ) , } ; } private readPossibleBaselineFile ( baselineFile ) { let rawBaselineContent ; try { rawBaselineContent = this . fileSystem . readFileSync ( baselineFile , { encoding : 'utf8' } ) ; } catch ( e ) { if ( e . code === 'ENOENT' ) { return null ; } else { throw e ; } } return this . baselineFileFormatter . parse ( rawBaselineContent ) ; } }