|
1 | 1 | New-Variable -Name sfdxCommands -Scope Script -Force
|
| 2 | +New-Variable -Name sfdxCommandsFile -Scope Global -Force |
| 3 | + |
| 4 | +$global:sfdxCommandsFile = "$HOME/.sfdxcommands.json" |
2 | 5 |
|
3 | 6 | <# The below script is executed in the background when a new ps session starts to pull all sfdx commands into a variable #>
|
4 | 7 | $sfdxCommandsFileCreateBlock = {
|
5 |
| - return sfdx commands --json | ConvertFrom-Json |
| 8 | + Param($sfdxCommandsFile) |
| 9 | + $tempCommandsFile = "$HOME/.sfdxcommandsinit.json" |
| 10 | + sfdx commands --json | Out-File -FilePath $tempCommandsFile |
| 11 | + Move-Item -Path $tempCommandsFile -Destination $sfdxCommandsFile |
| 12 | + return Get-Content $sfdxCommandsFile | ConvertFrom-Json |
6 | 13 | }
|
7 | 14 |
|
8 | 15 | <# executes the above script in the background so user is not waiting for the shell to start #>
|
9 |
| -$sfdxCommandsFileCreateJob = Start-Job -ScriptBlock $sfdxCommandsFileCreateBlock |
| 16 | +$sfdxCommandsFileCreateJob = Start-Job -ScriptBlock $sfdxCommandsFileCreateBlock -argumentlist $global:sfdxCommandsFile |
10 | 17 |
|
11 | 18 | <# script block for autocomplete. looks up matching commands from the file created above #>
|
12 | 19 | $scriptBlock = {
|
13 | 20 | param($wordToComplete, $commandAst, $cursorPosition)
|
14 | 21 |
|
15 |
| - if (!$script:sfdxCommands) |
16 |
| - { |
17 |
| - $script:sfdxCommands = Receive-Job -Wait -Job $sfdxCommandsFileCreateJob |
18 |
| - Remove-Job $sfdxCommandsFileCreateJob |
| 22 | + if (!$script:sfdxCommands) { |
| 23 | + if (Test-Path $global:sfdxCommandsFile -PathType Leaf) { |
| 24 | + $script:sfdxCommands = Get-Content $global:sfdxCommandsFile | ConvertFrom-Json |
| 25 | + } |
| 26 | + else { |
| 27 | + $script:sfdxCommands = Receive-Job -Wait -Job $sfdxCommandsFileCreateJob |
| 28 | + } |
19 | 29 | }
|
20 | 30 |
|
21 |
| - if ($commandAst.CommandElements.Count -eq 1) <# List all commands #> |
22 |
| - { |
| 31 | + if ($commandAst.CommandElements.Count -eq 1) { |
| 32 | + <# List all commands #> |
23 | 33 | $script:sfdxCommands | ForEach-Object {
|
24 | 34 | [System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'Method', $_.description)
|
25 | 35 | }
|
26 | 36 | }
|
27 |
| - elseif ($commandAst.CommandElements.Count -eq 2 -and $wordToComplete -ne "") <# Completing a command #> |
28 |
| - { |
| 37 | + elseif ($commandAst.CommandElements.Count -eq 2 -and $wordToComplete -ne "") { |
| 38 | + <# Completing a command #> |
29 | 39 | $commandPattern = "^(force:)?" + $commandAst.CommandElements[1].Value + ".+" <# Complete if force: is not specified too #>
|
30 | 40 | $script:sfdxCommands | Where-Object id -match $commandPattern | ForEach-Object {
|
31 | 41 | [System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'Method', $_.description)
|
32 | 42 | }
|
33 | 43 | }
|
34 |
| - elseif ($commandAst.CommandElements.Count -gt 2) <# Completing a parameter #> |
35 |
| - { |
| 44 | + elseif ($commandAst.CommandElements.Count -gt 2) { |
| 45 | + <# Completing a parameter #> |
36 | 46 | $parameterToMatch = $commandAst.CommandElements[-1].ToString().TrimStart("-") + "*";
|
37 | 47 |
|
38 | 48 | ($script:sfdxCommands | Where-Object id -eq $commandAst.CommandElements[1].Value).flags.PsObject.Properties | Where-Object Name -like $parameterToMatch | ForEach-Object {
|
|
0 commit comments