From 64d0f1aedebcd3ec3ffd63d16aa1bf1f9916586b Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 26 Mar 2025 08:34:15 +0100 Subject: [PATCH 1/3] feat: enhance Install-SalesforceClient with email parameter and modular command aliases --- helper/invokeCommand.helper.ps1 | 43 +++++++++ public/Install-SalesforceClient.ps1 | 135 +++++++++++++++++++--------- 2 files changed, 138 insertions(+), 40 deletions(-) create mode 100644 helper/invokeCommand.helper.ps1 diff --git a/helper/invokeCommand.helper.ps1 b/helper/invokeCommand.helper.ps1 new file mode 100644 index 0000000..d2d05cd --- /dev/null +++ b/helper/invokeCommand.helper.ps1 @@ -0,0 +1,43 @@ + +# SET MY INVOKE COMMAND ALIAS +# +# Allows calling constitely InvokeHelper with the module tag +# Need to define a variable called $MODULE_INVOKATION_TAG +# +# Sample: +# $MODULE_INVOKATION_TAG = "SfHelperModule" + +$moduleRootPath = $PSScriptRoot | Split-Path -Parent +$MODULE_NAME = (Get-ChildItem -Path $moduleRootPath -Filter *.psd1 | Select-Object -First 1).BaseName +$MODULE_INVOKATION_TAG = "$($MODULE_NAME)Module" + +function Set-MyInvokeCommandAlias{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,Position=0)][string]$Alias, + [Parameter(Mandatory,Position=1)][string]$Command + ) + + # throw if MODULE_INVOKATION_TAG is not set or is "MyModuleModule" + if (-not $MODULE_INVOKATION_TAG) { + throw "MODULE_INVOKATION_TAG is not set. Please set it to a unique value before calling Set-MyInvokeCommandAlias." + } + + if ($PSCmdlet.ShouldProcess("InvokeCommandAliasList", ("Add Command Alias [{0}] = [{1}]" -f $Alias, $Command))) { + InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG + } +} + +function Reset-MyInvokeCommandAlias{ + [CmdletBinding(SupportsShouldProcess)] + param() + + # throw if MODULE_INVOKATION_TAG is not set or is "MyModuleModule" + if (-not $MODULE_INVOKATION_TAG) { + throw "MODULE_INVOKATION_TAG is not set. Please set it to a unique value before calling Set-MyInvokeCommandAlias." + } + InvokeHelper\Reset-InvokeCommandAlias -Tag $MODULE_INVOKATION_TAG +} + +# Reset all aliases for this module on each refresh +Reset-MyInvokeCommandAlias \ No newline at end of file diff --git a/public/Install-SalesforceClient.ps1 b/public/Install-SalesforceClient.ps1 index ca175ee..42d62b2 100644 --- a/public/Install-SalesforceClient.ps1 +++ b/public/Install-SalesforceClient.ps1 @@ -1,9 +1,8 @@ - +Set-MyInvokeCommandAlias -Alias GetUserEmail -Command "gh api user --jq '.email'" Set-MyInvokeCommandAlias -Alias GetNpmVersion -Command "npm --version" Set-MyInvokeCommandAlias -Alias SalesforceCliInstall -Command "npm install @salesforce/cli --global" Set-MyInvokeCommandAlias -Alias GetSalesforceCliVersion -Command "sf --version" Set-MyInvokeCommandAlias -Alias SalesforceCliSetConfig -Command "sf config set --global target-org {email} --json" -Set-MyInvokeCommandAlias -Alias GetUserEmail -Command '$env:GITHUB_USER ? "$($env:GITHUB_USER)@github.com" : $null' # Set-MyInvokeCommandAlias -Alias SalesforceCliLogin -Command "sf org login device" Set-MyInvokeCommandAlias -Alias SalesforceCliDisplay -COmmand "sf org display --json" @@ -11,17 +10,43 @@ Set-MyInvokeCommandAlias -Alias SalesforceCliGetConfig -Command "sf config get t function Install-SalesforceClient{ [CmdletBinding(SupportsShouldProcess)] - param() + param( + [Parameter()][string]$Email + ) - # check that npm is install in the system - $result = Invoke-MyCommand -Command GetNpmVersion - if($null -eq $result){ - throw "0. npm not installed. Please install npm to allow the installation of Salesforce Cli through npm." - } else { + # 0. NPM setup check + if(-not (Test-NpmSetup)){return} + + # 1. Sf Install + if(-not (Invoke-SfInstall)){return} + + # 2. Test Sf Login + if(-not (Test-SfLogin)){return} + + # 3. Sf Config + if(-not(Invoke-SfConfig -Email:$Email)){return} + +} Export-ModuleMember -Function Install-SalesforceClient + +function Test-NpmSetup{ + [CmdletBinding()] + + $result = Invoke-MyCommand -Command GetNpmVersion -ErrorAction SilentlyContinue + + if($result){ "0. npm installed. version: $result" | Write-ToConsole -Color "Green" + return $true + } else { + throw "0. npm not installed. Please install npm to allow the installation of Salesforce Cli through npm." + return $false } +} Export-ModuleMember -Function Test-NpmSetup - # Check for Salesforce CLI installed and install if not +function Invoke-SfInstall{ + [CmdletBinding()] + param() + + # Check and Install for Salesforce CLI installed and install if not $sfversion = Invoke-MyCommand -Command GetSalesforceCliVersion -ErrorAction SilentlyContinue if($null -eq $sfversion){ if ($PSCmdlet.ShouldProcess("Target", "Operation")) { @@ -29,15 +54,19 @@ function Install-SalesforceClient{ Invoke-MyCommand -Command SalesforceCliInstall } } - - # Sf installation check $sfversion = Invoke-MyCommand -Command GetSalesforceCliVersion -ErrorAction SilentlyContinue if($null -eq $sfversion){ "1. Salesforce Cli installation failed. Run ""npm install @salesforce/cli --global"" to install manually!!!" | Write-MyError - return + return $false } else{ "1. Salesforce CLI installed. Version: $sfversion} " | Write-ToConsole -Color "Green" + return $true } +} Export-ModuleMember -Function Invoke-SfInstall + +function Test-SfLogin{ + [CmdletBinding(SupportsShouldProcess)] + param() # Sf logging $result = Invoke-MyCommand -Command SalesforceCliDisplay -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue @@ -45,43 +74,69 @@ function Install-SalesforceClient{ $user = $result.result if($user.connectedStatus -eq "Connected"){ "2. Salesforce CLI already connected with user $($user.username)" | Write-ToConsole -Color "Green" - } else { + return $user.username "2. Run the following command to loging to Salesforce CLI: ""sf org login device""" | Write-ToConsole -Color "Magenta" - return + return $null } # # Logging in to Salesforce CLI # "Loging in to Salesforce CLI..." | Write-MyHost # if ($PSCmdlet.ShouldProcess("Target", "Operation")) { - # Invoke-MyCommand -Command SalesforceCliLogin - # } + # Invoke-MyCommand -Command SalesforceCliLogin + # } +} Export-ModuleMember -Function Test-SfLogin - $result = Invoke-MyCommand -Command SalesforceCliGetConfig -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue - $result | Write-MyVerbose - $config = $result.result - if($config.success){ - "3. Salesforce CLI configured with user $($config.value)" | Write-ToConsole -Color "Green" +function Invoke-SfConfig{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter()][string]$Email + ) + + $Email = Resolve-Email -Email $Email + + "Using email $Email to configure Salesforce CLI" | Write-MyVerbose + + # $result = Invoke-MyCommand -Command SalesforceCliGetConfig -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue + # $result | Write-MyVerbose + # $config = $result.result + # if($config.success){ + # "3. Salesforce CLI already configured with user $($config.value)" | Write-ToConsole -Color "Green" + # return + # } + + # Configure Salesforce CLI + "Configuring Salesforce CLI ..." | Write-MyVerbose + + $result = Invoke-MyCommand -Command SalesforceCliSetConfig -Parameters @{ email = $Email } | ConvertFrom-Json -ErrorAction SilentlyContinue + if($result.result.successes.value -eq $Email){ + "3. Salesforce CLI configured with user $($Email) " | Write-ToConsole -Color "Green" + return $Email } else { - "3. Salesforce CLI not configured. Run the following command to configure Salesforce CLI: ""sf config set --global target-org {email}""" | Write-ToConsole -Color "Magenta" + "3. Salesforce CLI configuration failed with email $Email. " | Write-MyError + $result | ConvertTo-Json | Write-MyVerbose return } - # # Configure Salesforce CLI - # $email = Invoke-MyCommand -Command GetUserEmail - # "Configuring Salesforce CLI ..." | Write-MyHost - # if($email){ - # $json = Invoke-MyCommand -Command SalesforceCliSetConfig -Parameters @{ email = $email } - # $result = $json | ConvertFrom-Json -ErrorAction SilentlyContinue - # if($user.result.successes.success){ - # "Configured Salesforce CLI with user $($user.result.successes.value) " | Write-MyHost - # } else { - # "Salesforce CLI configuration failed with email $email" | Write-MyError - # return - # } - # } else { - # "Can not find your github user. Run the following command to configure your Salesforce CLI: sf config set --global target-org {email}" | Write-MyHost - # "Salesforce CLI installed but pending to be configured." | Write-MyWarning - # return - # } +} Export-ModuleMember -Function Invoke-SfConfig + +function Resolve-Email{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter()][string]$Email + ) + + if(-not [string]::IsNullOrEmpty($Email)){ + "Resolving email [$Email] from parameter" | Write-MyVerbose + return $Email + } + + $Email = Invoke-MyCommand -Command GetUserEmail -ErrorAction SilentlyContinue + "Resolved email [$Email] from github" | Write-MyVerbose + + if ($null -eq $Email){ + throw "Unable to resolve user email. Please provide email as parameter or set proper gh cli credentials and user github profile email." | Write-MyError + } + + return $Email -} Export-ModuleMember -Function Install-SalesforceClient \ No newline at end of file +} Export-ModuleMember -Function Resolve-Email \ No newline at end of file From 761287b63a1dd674b855418b1dfa120cfde91e37 Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 26 Mar 2025 09:45:39 +0100 Subject: [PATCH 2/3] feat: refactor Salesforce CLI command aliases for consistency and clarity --- public/Install-SalesforceClient.ps1 | 50 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/public/Install-SalesforceClient.ps1 b/public/Install-SalesforceClient.ps1 index 42d62b2..1455a3b 100644 --- a/public/Install-SalesforceClient.ps1 +++ b/public/Install-SalesforceClient.ps1 @@ -1,12 +1,11 @@ Set-MyInvokeCommandAlias -Alias GetUserEmail -Command "gh api user --jq '.email'" Set-MyInvokeCommandAlias -Alias GetNpmVersion -Command "npm --version" -Set-MyInvokeCommandAlias -Alias SalesforceCliInstall -Command "npm install @salesforce/cli --global" -Set-MyInvokeCommandAlias -Alias GetSalesforceCliVersion -Command "sf --version" -Set-MyInvokeCommandAlias -Alias SalesforceCliSetConfig -Command "sf config set --global target-org {email} --json" +Set-MyInvokeCommandAlias -Alias SfCliInstall -Command "npm install @salesforce/cli --global" +Set-MyInvokeCommandAlias -Alias SfCliVersion -Command "sf --version" +Set-MyInvokeCommandAlias -Alias SfCliDisplay -COmmand "sf org display --target-org={email} --json" +Set-MyInvokeCommandAlias -Alias SfCliGetConfig -Command "sf config get target-org --json" +Set-MyInvokeCommandAlias -Alias SfCliSetConfig -Command "sf config set --global target-org {email} --json" -# Set-MyInvokeCommandAlias -Alias SalesforceCliLogin -Command "sf org login device" -Set-MyInvokeCommandAlias -Alias SalesforceCliDisplay -COmmand "sf org display --json" -Set-MyInvokeCommandAlias -Alias SalesforceCliGetConfig -Command "sf config get target-org --json" function Install-SalesforceClient{ [CmdletBinding(SupportsShouldProcess)] @@ -43,18 +42,18 @@ function Test-NpmSetup{ } Export-ModuleMember -Function Test-NpmSetup function Invoke-SfInstall{ - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] param() # Check and Install for Salesforce CLI installed and install if not - $sfversion = Invoke-MyCommand -Command GetSalesforceCliVersion -ErrorAction SilentlyContinue + $sfversion = Invoke-MyCommand -Command SfCliVersion -ErrorAction SilentlyContinue if($null -eq $sfversion){ if ($PSCmdlet.ShouldProcess("Target", "Operation")) { "Installing Salesforce CLI using npm..." | Write-MyHost - Invoke-MyCommand -Command SalesforceCliInstall + Invoke-MyCommand -Command SfCliInstall } } - $sfversion = Invoke-MyCommand -Command GetSalesforceCliVersion -ErrorAction SilentlyContinue + $sfversion = Invoke-MyCommand -Command SfCliVersion -ErrorAction SilentlyContinue if($null -eq $sfversion){ "1. Salesforce Cli installation failed. Run ""npm install @salesforce/cli --global"" to install manually!!!" | Write-MyError return $false @@ -66,15 +65,20 @@ function Invoke-SfInstall{ function Test-SfLogin{ [CmdletBinding(SupportsShouldProcess)] - param() + param( + [Parameter()][string]$Email + ) + + $email = Resolve-Email -Email $Email # Sf logging - $result = Invoke-MyCommand -Command SalesforceCliDisplay -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue + $result = Invoke-MyCommand -Command SfCliDisplay -Parameters @{ email = $email } -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue $result | Write-MyVerbose $user = $result.result if($user.connectedStatus -eq "Connected"){ "2. Salesforce CLI already connected with user $($user.username)" | Write-ToConsole -Color "Green" return $user.username + } else { "2. Run the following command to loging to Salesforce CLI: ""sf org login device""" | Write-ToConsole -Color "Magenta" return $null } @@ -96,24 +100,24 @@ function Invoke-SfConfig{ "Using email $Email to configure Salesforce CLI" | Write-MyVerbose - # $result = Invoke-MyCommand -Command SalesforceCliGetConfig -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue - # $result | Write-MyVerbose - # $config = $result.result - # if($config.success){ - # "3. Salesforce CLI already configured with user $($config.value)" | Write-ToConsole -Color "Green" - # return - # } + $result = Invoke-MyCommand -Command SfCliGetConfig -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue + $result | Write-MyVerbose + $config = $result.result + if($result.result.value -eq $Email){ + "3. Salesforce CLI already configured with user $($config.value)" | Write-ToConsole -Color "Green" + return $true + } # Configure Salesforce CLI "Configuring Salesforce CLI ..." | Write-MyVerbose - $result = Invoke-MyCommand -Command SalesforceCliSetConfig -Parameters @{ email = $Email } | ConvertFrom-Json -ErrorAction SilentlyContinue + $result = Invoke-MyCommand -Command SfCliSetConfig -Parameters @{ email = $Email } | ConvertFrom-Json -Depth 10 -ErrorAction SilentlyContinue if($result.result.successes.value -eq $Email){ - "3. Salesforce CLI configured with user $($Email) " | Write-ToConsole -Color "Green" + "3. Salesforce CLI configured with user $($Email) " | Write-MyHost return $Email } else { - "3. Salesforce CLI configuration failed with email $Email. " | Write-MyError - $result | ConvertTo-Json | Write-MyVerbose + "3. Salesforce CLI configuration failed with email $Email. Logging to Salesforce Cli and try again. " | Write-MyError + $result | ConvertTo-Json -Depth 10 | Write-MyVerbose return } From 85e53c8269306e1bd2b1b1657bb0bcb8fffe27fa Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 26 Mar 2025 09:49:13 +0100 Subject: [PATCH 3/3] fix: update output method in Invoke-SfConfig for Salesforce CLI configuration success message --- public/Install-SalesforceClient.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/Install-SalesforceClient.ps1 b/public/Install-SalesforceClient.ps1 index 1455a3b..ac6cf9e 100644 --- a/public/Install-SalesforceClient.ps1 +++ b/public/Install-SalesforceClient.ps1 @@ -113,7 +113,7 @@ function Invoke-SfConfig{ $result = Invoke-MyCommand -Command SfCliSetConfig -Parameters @{ email = $Email } | ConvertFrom-Json -Depth 10 -ErrorAction SilentlyContinue if($result.result.successes.value -eq $Email){ - "3. Salesforce CLI configured with user $($Email) " | Write-MyHost + "3. Salesforce CLI configured with user $($Email) " | Write-ToConsole -Color "Green" return $Email } else { "3. Salesforce CLI configuration failed with email $Email. Logging to Salesforce Cli and try again. " | Write-MyError