import path from "path" ; import * as fs from "fs-extra" ; import { ApexClasses } from "./SFPPackage" ; import xml2json from "../utils/xml2json" ; const xml2js = require ( "xml2js" ) ; export default class PackageManifest { private _manifestJson ; private _manifestXml : string ; /**
     * Getter for package manifest JSON
     */ get manifestJson ( ) { return this . _manifestJson ; } /**
     * Getter for package manifest XML
     */ get manifestXml ( ) : string { return this . _manifestXml ; } private constructor ( ) { } ; param mdapiDir returns static async create ( mdapiDir ) { const packageManifest = new PackageManifest ( ) ; const packageXml = fs . readFileSync ( path . join ( mdapiDir , "package.xml" ) , "utf8" ) ; packageManifest . _manifestXml = packageXml ; packageManifest . _manifestJson = await xml2json ( packageXml ) ; return packageManifest ; } param components param apiVersion returns static createFromScratch ( components , apiVersion ) { const packageManifest = new PackageManifest ( ) ; const packageJson = { $ : { xmlns : "http://soap.sforce.com/2006/04/metadata" } , types : [ ] , version : apiVersion , } ; components . forEach ( component => { const type = packageJson . types . find ( type => type . name === component . type ) ; if ( type ) { type . members . push ( component . fullName ) ; } else { const newType = { name : component . type , members : [ component . fullName ] } ; packageJson . types . push ( newType ) ; } } ) ; const builder = new xml2js . Builder ( { xmldec : { version : "1.0" , encoding : "UTF-8" } , } ) ; let packageObj = { Package : packageJson , } ; packageManifest . _manifestXml = builder . buildObject ( packageObj ) ; packageManifest . _manifestJson = packageObj ; return packageManifest ; } param manifest returns static async createWithJSONManifest ( manifest ) { const packageManifest = new PackageManifest ( ) ; packageManifest . _manifestJson = manifest ; const builder = new xml2js . Builder ( { xmldec : { version : "1.0" , encoding : "UTF-8" } , } ) ; packageManifest . _manifestXml = builder . buildObject ( manifest ) ; return packageManifest ; } returns public isProfilesInPackage ( ) { let isProfilesFound = false ; if ( this . _manifestJson . Package . types ) { if ( Array . isArray ( this . _manifestJson . Package . types ) ) { for ( const type of this . _manifestJson . Package . types ) { if ( type . name === "Profile" ) { isProfilesFound = true ; break ; } } } else if ( this . manifestJson . Package . types . name === "Profile" ) { isProfilesFound = true ; } } return isProfilesFound ; } public isPermissionSetGroupsFoundInPackage ( ) { let isPermissionSetGroupFound = false ; if ( Array . isArray ( this . _manifestJson . Package . types ) ) { for ( let type of this . _manifestJson . Package . types ) { if ( type . name === "PermissionSetGroup" ) { isPermissionSetGroupFound = true ; break ; } } } else if ( this . _manifestJson . Package . types . name === "PermissionSetGroup" ) { isPermissionSetGroupFound = true ; } return isPermissionSetGroupFound ; } returns public isApexInPackage ( ) { let isApexFound = false ; if ( this . _manifestJson . Package . types ) { if ( Array . isArray ( this . _manifestJson . Package . types ) ) { for ( const type of this . _manifestJson . Package . types ) { if ( type . name === "ApexClass" || type . name === "ApexTrigger" ) { isApexFound = true ; break ; } } } else if ( this . _manifestJson . Package . types . name === "ApexClass" || this . _manifestJson . Package . types . name === "ApexTrigger" ) { isApexFound = true ; } } return isApexFound ; } returns public fetchTriggers ( ) { let triggers ; let types ; if ( this . _manifestJson . Package . types ) { if ( this . _manifestJson . Package . types instanceof Array ) { types = this . _manifestJson . Package . types ; } else { types = [ this . _manifestJson . Package . types ] ; } } if ( types ) { for ( const type of types ) { if ( type . name === "ApexTrigger" ) { if ( type . members instanceof Array ) { triggers = type . members ; } else { triggers = [ type . members ] ; } break ; } } } return triggers ; } public isPayloadContainTypesOtherThan ( providedType ) { let anyOtherType = false ; if ( this . _manifestJson . Package . types ) { if ( Array . isArray ( this . _manifestJson . Package . types ) ) { for ( const type of this . _manifestJson . Package . types ) { if ( type . name !== providedType ) { anyOtherType = true ; break ; } } } else if ( this . _manifestJson . Package . types . name !== providedType ) { anyOtherType = true ; } } return anyOtherType ; } public isPayLoadContainTypesSupportedByProfiles ( ) { const profileSupportedMetadataTypes = [ "ApexClass" , "CustomApplication" , "CustomObject" , "CustomField" , "Layout" , "ApexPage" , "CustomTab" , "RecordType" , "SystemPermissions" , ] ; let containsProfileSupportedType = false ; if ( this . _manifestJson . Package . types ) { if ( Array . isArray ( this . _manifestJson . Package . types ) ) { for ( const type of this . _manifestJson . Package . types ) { if ( profileSupportedMetadataTypes . includes ( type . name ) ) { containsProfileSupportedType = true ; break ; } } } else if ( profileSupportedMetadataTypes . includes ( this . _manifestJson . Package . types . name ) ) { containsProfileSupportedType = true ; } } return containsProfileSupportedType ; } }