file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description license import * as core from '@salesforce/core' ; import { JsonMap } from '@salesforce/ts-types' ; import * as path from 'path' ; import { SfdxFalconDebug } from '../sfdx-falcon-debug' ; import { SfdxFalconError } from '../sfdx-falcon-error' ; import { SfdxFalconRecipe } from '../sfdx-falcon-recipe' ; import { SfdxFalconResult } from '../sfdx-falcon-result' ; import { ProjectResolutionOptions } from '../sfdx-falcon-types' ; import { SfdxFalconProjectConfig } from '../sfdx-falcon-types' ; import { SfdxFalconLocalConfig } from '../sfdx-falcon-types' ; import { SfdxFalconGlobalConfig } from '../sfdx-falcon-types' ; const dbgNs = 'PROJECT:sfdx-falcon-project:' ; class summary description public export class SfdxFalconProject { method param { string } projectPath returns { Promise < SfdxFalconProject > } description public static async public static async resolve ( projectPath , opts = { } ) { opts = { ... { resolveProjectConfig : true , resolveLocalConfig : true , resolveGlobalConfig : true } , ... opts } as ProjectResolutionOptions ; const sfdxFPO = new SfdxFalconProject ( ) ; await SfdxFalconProject . initializeConfig ( projectPath , sfdxFPO , opts ) ; await SfdxFalconProject . initializePaths ( projectPath , sfdxFPO ) ; await SfdxFalconProject . initializeSourceTracking ( projectPath , sfdxFPO ) ; await SfdxFalconProject . validateOverallConfig ( sfdxFPO , opts ) ; SfdxFalconDebug . obj ( `${ dbgNs }resolve:sfdxFPO:` , sfdxFPO , `sfdxFPO: ` ) ; return sfdxFPO ; } method param { string } projectPath returns { Promise < JsonMap > } description private static async private static async resolveAggregatedSfdxConfig ( projectPath ) { const sfdxProject = await core . SfdxProject . resolve ( projectPath ) ; SfdxFalconDebug . obj ( `${ dbgNs }resolveAggregatedSfdxConfig:sfdxProject:` , sfdxProject , `sfdxProject: ` ) ; const sfdxAggregateConfig = await sfdxProject . resolveProjectConfig ( ) ; SfdxFalconDebug . obj ( `${ dbgNs }resolveAggregatedSfdxConfig:sfdxAggregateConfig:` , sfdxAggregateConfig , `sfdxAggregateConfig: ` ) ; return sfdxAggregateConfig ; } method returns { Promise < SfdxFalconGlobalConfig > } description private static async private static async resolveGlobalSfdxFalconConfig ( ) { return { propertiesTBD : 'TBD' } ; } method param { string } projectPath returns { Promise < SfdxFalconLocalConfig > } description private static async private static async resolveSfdxFalconLocalConfig ( projectPath ) { const configOptions = { rootFolder : path . join ( projectPath , '.sfdx-falcon' ) , filename : 'sfdx-falcon-config.json' , isGlobal : false , isState : false } ; SfdxFalconDebug . obj ( `${ dbgNs }resolveSfdxFalconLocalConfig:configOptions:` , configOptions , `configOptions: ` ) ; const sfdxFalconLocalConfigFile = await core . ConfigFile . create ( configOptions ) ; if ( await sfdxFalconLocalConfigFile . exists ( ) === false ) { const combinedPath = path . join ( configOptions . rootFolder , configOptions . filename ) ; throw new SfdxFalconError ( `Config file does not exist - ${ combinedPath }` , `FileNotFound` , `${ dbgNs }resolveSfdxFalconLocalConfig` ) ; } SfdxFalconDebug . obj ( `${ dbgNs }resolveSfdxFalconLocalConfig:sfdxFalconLocalConfigFile:` , sfdxFalconLocalConfigFile , `falconLocalConfigFile: ` ) ; const sfdxFalconLocalConfig = sfdxFalconLocalConfigFile . toObject ( ) as unknown as SfdxFalconLocalConfig ; SfdxFalconDebug . obj ( `${ dbgNs }resolveSfdxFalconLocalConfig:sfdxFalconLocalConfig:` , sfdxFalconLocalConfig , `sfdxFalconLocalConfig: ` ) ; return sfdxFalconLocalConfig ; } method param { string } projectPath param { SfdxFalconProject } sfdxFPO param { ProjectResolutionOptions } opts returns { Promise < void > } description private static async private static async initializeConfig ( projectPath , sfdxFPO , opts ) { sfdxFPO . _sfdxAggregateConfig = await SfdxFalconProject . resolveAggregatedSfdxConfig ( projectPath ) ; sfdxFPO . _falconProjectConfig = opts . resolveProjectConfig ? sfdxFPO . _sfdxAggregateConfig . plugins [ 'sfdxFalcon' ] : { } ; sfdxFPO . _falconLocalConfig = opts . resolveLocalConfig ? await SfdxFalconProject . resolveSfdxFalconLocalConfig ( projectPath ) : { } ; sfdxFPO . _falconGlobalConfig = opts . resolveGlobalConfig ? await SfdxFalconProject . resolveGlobalSfdxFalconConfig ( ) : { } ; } method param { string } projectPath param { SfdxFalconProject } sfdxFPO returns { Promise < void > } description private static async private static async initializePaths ( projectPath , sfdxFPO ) { sfdxFPO . _projectPath = projectPath ; sfdxFPO . _falconConfigPath = path . join ( projectPath , `.sfdx-falcon` ) ; sfdxFPO . _templatesPath = path . join ( projectPath , `.templates` ) ; sfdxFPO . _configPath = path . join ( projectPath , `config` ) ; sfdxFPO . _mdapiSourcePath = path . join ( projectPath , `mdapi-source` ) ; sfdxFPO . _sfdxSourcePath = path . join ( projectPath , `sfdx-source` ) ; sfdxFPO . _dataPath = path . join ( projectPath , `data` ) ; sfdxFPO . _docsPath = path . join ( projectPath , `docs` ) ; sfdxFPO . _toolsPath = path . join ( projectPath , `tools` ) ; sfdxFPO . _tempPath = path . join ( projectPath , `temp` ) ; } method param { string } projectPath param { SfdxFalconProject } sfdxFPO returns { Promise < void > } description private static async private static async initializeSourceTracking ( projectPath , sfdxFPO ) { sfdxFPO . _trackedByGit = null ; } method param { SfdxFalconProject } sfdxFPO returns { boolean | string [ ] } description private static private static validateFalconGlobalConfig ( sfdxFPO ) { const invalidConfigKeys = new Array < string > ( ) ; if ( invalidConfigKeys . length > 0 ) { return invalidConfigKeys ; } else { return true ; } } method param { SfdxFalconProject } sfdxFPO returns { boolean | Array < string > } description private static private static validateFalconLocalConfig ( sfdxFPO ) { const invalidConfigKeys = new Array < string > ( ) ; if ( ! sfdxFPO . _falconLocalConfig . devHubAlias ) invalidConfigKeys . push ( 'devHubAlias' ) ; if ( invalidConfigKeys . length > 0 ) { return invalidConfigKeys ; } else { return true ; } } method param { SfdxFalconProject } sfdxFPO returns { boolean | Array < string > } description private static private static validateFalconProjectConfig ( sfdxFPO ) { if ( typeof sfdxFPO . _falconProjectConfig !== 'object' ) { throw new SfdxFalconError ( `${ sfdxFPO . projectPath } does not contain a valid SFDX-Falcon project.` , `InvalidSfdxFalconProject` , `${ dbgNs }validateFalconProjectConfig` ) ; } const invalidConfigKeys = new Array < string > ( ) ; if ( ! sfdxFPO . _falconProjectConfig . projectAlias ) invalidConfigKeys . push ( 'projectAlias' ) ; if ( ! sfdxFPO . _falconProjectConfig . projectName ) invalidConfigKeys . push ( 'projectName' ) ; if ( ! sfdxFPO . _falconProjectConfig . projectType ) invalidConfigKeys . push ( 'projectType' ) ; if ( ! sfdxFPO . _falconProjectConfig . defaultRecipe ) invalidConfigKeys . push ( 'defaultRecipe' ) ; if ( ! sfdxFPO . _falconProjectConfig . projectVersion ) invalidConfigKeys . push ( 'projectVersion' ) ; if ( ! sfdxFPO . _falconProjectConfig . schemaVersion ) invalidConfigKeys . push ( 'schemaVersion' ) ; if ( ! sfdxFPO . _falconProjectConfig . pluginVersion ) invalidConfigKeys . push ( 'pluginVersion' ) ; if ( ! sfdxFPO . _falconProjectConfig . schemaVersion ) invalidConfigKeys . push ( 'schemaVersion' ) ; if ( invalidConfigKeys . length > 0 ) { return invalidConfigKeys ; } else { return true ; } } method param { SfdxFalconProject } sfdxFPO param { ProjectResolutionOptions } opts returns { Promise < void > } description private static async private static async validateOverallConfig ( sfdxFPO , opts ) { if ( opts . resolveProjectConfig ) { const validationResponse = SfdxFalconProject . validateFalconProjectConfig ( sfdxFPO ) ; if ( validationResponse !== true ) { throw new SfdxFalconError ( `Configuration for "plugins.sfdxFalcon" in sfdx-project.json has missing/invalid settings (${ validationResponse }).` , `InvalidConfig` , `${ dbgNs }validateOverallConfig` ) ; } } if ( opts . resolveLocalConfig ) { const validationResponse = SfdxFalconProject . validateFalconLocalConfig ( sfdxFPO ) ; if ( validationResponse !== true ) { throw new SfdxFalconError ( `Configuration in ".sfdx-falcon/sfdx-falcon-config.json" has missing/invalid settings (${ validationResponse }).` , `InvalidConfig` , `${ dbgNs }validateOverallConfig` ) ; } } if ( opts . resolveGlobalConfig ) { const validationResponse = SfdxFalconProject . validateFalconGlobalConfig ( sfdxFPO ) ; if ( validationResponse !== true ) { throw new SfdxFalconError ( `Configuration in "~/.sfdx/sfdx-falcon-config.json" has missing/invalid settings (${ validationResponse }).` , `InvalidConfig` , `${ dbgNs }validateOverallConfig` ) ; } } } private _sfdxAggregateConfig : JsonMap ; get sfdxAggregateConfig ( ) : JsonMap { return this . _sfdxAggregateConfig ; } private _falconProjectConfig : SfdxFalconProjectConfig ; get falconProjectConfig ( ) : SfdxFalconProjectConfig { return this . _falconProjectConfig ; } private _falconLocalConfig : SfdxFalconLocalConfig ; get falconLocalConfig ( ) : SfdxFalconLocalConfig { return this . _falconLocalConfig ; } private _falconGlobalConfig : SfdxFalconGlobalConfig ; get falconGlobalConfig ( ) : SfdxFalconGlobalConfig { return this . _falconGlobalConfig ; } private _projectPath : string ; get projectPath ( ) : string { return this . _projectPath ; } private _falconConfigPath : string ; get falconConfigPath ( ) : string { return this . _falconConfigPath ; } private _templatesPath : string ; get templatesPath ( ) : string { return this . _templatesPath ; } private _configPath : string ; get configPath ( ) : string { return this . _configPath ; } private _mdapiSourcePath : string ; get mdapiSourcePath ( ) : string { return this . _mdapiSourcePath ; } private _sfdxSourcePath : string ; get sfdxSourcePath ( ) : string { return this . _sfdxSourcePath ; } private _dataPath : string ; get dataPath ( ) : string { return this . _dataPath ; } private _docsPath : string ; get docsPath ( ) : string { return this . _docsPath ; } private _toolsPath : string ; get toolsPath ( ) : string { return this . _toolsPath ; } private _tempPath : string ; get tempPath ( ) : string { return this . _tempPath ; } private _trackedByGit : string ; get trackedByGit ( ) : string { return this . _trackedByGit ; } constructs description private private constructor ( ) { } method param { JsonMap } compileOptions param { JsonMap } executionOptions returns { Promise < SfdxFalconResult > } description public async public async runChosenRecipe ( compileOptions = { } , executionOptions = { } ) { throw new SfdxFalconError ( `Method runChosenRecipe() is not yet implemented` , `NotImplemented` , `${ dbgNs }runChosenRecipe` ) ; } method param { JsonMap } compileOptions param { JsonMap } executionOptions returns { Promise < SfdxFalconResult > } description public async public async runDefaultRecipe ( compileOptions = { } , executionOptions = { } ) { const recipeName = this . _falconProjectConfig . defaultRecipe ; return await this . compileAndRunRecipe ( recipeName , compileOptions , executionOptions ) ; } method param { string } recipeName param { JsonMap } compileOptions param { JsonMap } executionOptions returns { Promise < SfdxFalconRecipeResponse > } description public async public async runSpecifiedRecipe ( recipeName , compileOptions = { } , executionOptions = { } ) { return await this . compileAndRunRecipe ( recipeName , compileOptions , executionOptions ) ; } method param { string } recipeName param { JsonMap } compileOptions param { JsonMap } executionOptions returns { Promise < SfdxFalconResult > } description private async private async compileAndRunRecipe ( recipeName , compileOptions = { } , executionOptions = { } ) { const recipeToRun = await SfdxFalconRecipe . read ( this , recipeName ) ; await recipeToRun . compile ( compileOptions ) ; return await recipeToRun . execute ( executionOptions ) ; } }