import Git from "../git/Git" ; import IgnoreFiles from "../ignore/IgnoreFiles" ; import ProjectConfig from "../project/ProjectConfig" ; import MetadataFiles from "../metadata/MetadataFiles" ; import Component from "./Component" ; import * as fs from "fs-extra" ; import path = require ( "path" ) ; import SFPLogger , { LoggerLevel } from "../logger/SFPLogger" ; export default class ChangedComponentsFetcher { constructor ( private baseBranch ) { } async fetch ( ) { const components = [ ] ; let git = new Git ( ) ; let projectConfig = ProjectConfig . getSFDXPackageManifest ( null ) ; if ( ! this . baseBranch . includes ( "origin" ) ) { this . baseBranch = `remotes/origin/${ this . baseBranch }` ; } let diff = await git . diff ( [ this . baseBranch , `HEAD` , `--no-renames` , `--name-only` ] ) ; diff = diff . filter ( ( filepath ) => projectConfig . packageDirectories . find ( ( pkg ) => filepath . includes ( pkg . path ) ) ) ; let ignoreFiles = new IgnoreFiles ( fs . readFileSync ( ".forceignore" , "utf8" ) ) ; diff = ignoreFiles . filter ( diff ) ; let componentSuccesses = this . getComponentSuccessesFromReports ( ) ; if ( diff . length > 0 ) { for ( const filepath of diff ) { const fullApiName = MetadataFiles . getFullApiName ( filepath ) ; const indexOfPackage = projectConfig . packageDirectories . findIndex ( pkg => filepath . includes ( pkg . path ) ) ; const packageName = projectConfig . packageDirectories [ indexOfPackage ] . package ; const componentSuccess = componentSuccesses . find ( component => component . fullName === fullApiName && component . id ) ; if ( componentSuccess ) { const component = { id : componentSuccess . id , fullName : componentSuccess . fullName , type : componentSuccess . componentType , files : [ filepath ] , package : packageName , packageType : ProjectConfig . getPackageType ( projectConfig , packageName ) , indexOfPackage : indexOfPackage } ; components . push ( component ) ; } else { SFPLogger . log ( `Unable to find ID for ${ fullApiName } in deployment reports` , LoggerLevel . DEBUG ) ; continue ; } } } return components ; } /**
     * Aggregates component successes from MDAPI deploy reports
     */ private getComponentSuccessesFromReports ( ) { let componentSuccesses = [ ] ; const reportsDir = ".sfpowerscripts/mdapiDeployReports" ; let reports = fs . readdirSync ( reportsDir ) ; reports . forEach ( ( report ) => { let data = JSON . parse ( fs . readFileSync ( path . join ( reportsDir , report ) , "utf8" ) ) ; componentSuccesses = componentSuccesses . concat ( data . result . details . componentSuccesses ) ; } ) ; return componentSuccesses ; } }