import * as fs from "fs-extra" ; let path = require ( "path" ) ; /**
 * Helper functions for retrieving info from project config
 */ export default class ProjectConfig { param projectConfig param sfdxPackage public static getPackageId ( projectConfig , sfdxPackage ) { if ( projectConfig [ "packageAliases" ] ?. [ sfdxPackage ] ) { return projectConfig [ "packageAliases" ] [ sfdxPackage ] ; } else { throw Error ( "No Package Id found in sfdx-project.json. Please ensure package alias have the package added" ) ; } } param projectDirectory public static getAllPackages ( projectDirectory ) { let projectConfig = ProjectConfig . getSFDXPackageManifest ( projectDirectory ) ; let sfdxpackages = [ ] ; projectConfig [ "packageDirectories" ] . forEach ( ( pkg ) => { sfdxpackages . push ( pkg [ "package" ] ) ; } ) ; return sfdxpackages ; } param projectDirectory public static getSFDXPackageManifest ( projectDirectory ) { let projectConfigJSON ; if ( projectDirectory ) { projectConfigJSON = path . join ( projectDirectory , "sfdx-project.json" ) ; } else { projectConfigJSON = "sfdx-project.json" ; } try { return JSON . parse ( fs . readFileSync ( projectConfigJSON , "utf8" ) ) ; } catch ( error ) { throw new Error ( `sfdx-project.json doesn't exist or not readable at ${ projectConfigJSON }` ) ; } } param projectConfig param sfdxPackage public static getPackageType ( projectConfig , sfdxPackage ) { let packageDescriptor = ProjectConfig . getPackageDescriptorFromConfig ( sfdxPackage , projectConfig ) ; if ( projectConfig [ "packageAliases" ] ?. [ sfdxPackage ] ) { return "Unlocked" ; } else { if ( packageDescriptor . type ?. toLowerCase ( ) === "data" ) return "Data" ; else return "Source" ; } } param projectDirectory param sfdxPackage public static getSFDXPackageDescriptor ( projectDirectory , sfdxPackage ) { let projectConfig = ProjectConfig . getSFDXPackageManifest ( projectDirectory ) ; let sfdxPackageDescriptor = ProjectConfig . getPackageDescriptorFromConfig ( sfdxPackage , projectConfig ) ; return sfdxPackageDescriptor ; } param sfdxPackage param projectConfig public static getPackageDescriptorFromConfig ( sfdxPackage , projectConfig ) { let sfdxPackageDescriptor ; if ( sfdxPackage ) { projectConfig [ "packageDirectories" ] . forEach ( ( pkg ) => { if ( sfdxPackage == pkg [ "package" ] ) { sfdxPackageDescriptor = pkg ; } } ) ; } if ( sfdxPackageDescriptor == null ) throw new Error ( "Package or package directory does not exist" ) ; return sfdxPackageDescriptor ; } param projectDirectory public static getDefaultSFDXPackageDescriptor ( projectDirectory ) { let packageDirectory ; let sfdxPackageDescriptor ; let projectConfig = this . getSFDXPackageManifest ( projectDirectory ) ; projectConfig [ "packageDirectories" ] . forEach ( ( pkg ) => { if ( pkg [ "default" ] == true ) { packageDirectory = pkg [ "path" ] ; sfdxPackageDescriptor = pkg ; } } ) ; if ( packageDirectory == null ) throw new Error ( "Package or package directory not exist" ) ; else return sfdxPackageDescriptor ; } param projectDirectory param sfdxPackage public static cleanupMPDFromManifest ( projectDirectory , sfdxPackage ) { let sfdxManifest = this . getSFDXPackageManifest ( projectDirectory ) ; if ( sfdxPackage ) { let i = sfdxManifest [ "packageDirectories" ] . length ; while ( i -- ) { if ( sfdxPackage != sfdxManifest [ "packageDirectories" ] [ i ] [ "package" ] ) { sfdxManifest [ "packageDirectories" ] . splice ( i , 1 ) ; } } } else { let i = sfdxManifest [ "packageDirectories" ] . length ; while ( i -- ) { if ( ! fs . existsSync ( sfdxManifest [ "packageDirectories" ] [ i ] [ "path" ] ) ) { sfdxManifest [ "packageDirectories" ] . splice ( i , 1 ) ; } } } sfdxManifest [ "packageDirectories" ] [ 0 ] [ "default" ] = true ; return sfdxManifest ; } }