Skip to content

Commit b624467

Browse files
committed
Changes to prevent blocking command during initial launch
1 parent acfedad commit b624467

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

sfdx-autocomplete.ps1

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
New-Variable -Name sfdxCommands -Scope Script -Force
2+
New-Variable -Name sfdxCommandsFile -Scope Global -Force
3+
4+
$global:sfdxCommandsFile = "$HOME/.sfdxcommands.json"
25

36
<# The below script is executed in the background when a new ps session starts to pull all sfdx commands into a variable #>
47
$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
613
}
714

815
<# 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
1017

1118
<# script block for autocomplete. looks up matching commands from the file created above #>
1219
$scriptBlock = {
1320
param($wordToComplete, $commandAst, $cursorPosition)
1421

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+
}
1929
}
2030

21-
if ($commandAst.CommandElements.Count -eq 1) <# List all commands #>
22-
{
31+
if ($commandAst.CommandElements.Count -eq 1) {
32+
<# List all commands #>
2333
$script:sfdxCommands | ForEach-Object {
2434
[System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'Method', $_.description)
2535
}
2636
}
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 #>
2939
$commandPattern = "^(force:)?" + $commandAst.CommandElements[1].Value + ".+" <# Complete if force: is not specified too #>
3040
$script:sfdxCommands | Where-Object id -match $commandPattern | ForEach-Object {
3141
[System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'Method', $_.description)
3242
}
3343
}
34-
elseif ($commandAst.CommandElements.Count -gt 2) <# Completing a parameter #>
35-
{
44+
elseif ($commandAst.CommandElements.Count -gt 2) {
45+
<# Completing a parameter #>
3646
$parameterToMatch = $commandAst.CommandElements[-1].ToString().TrimStart("-") + "*";
3747

3848
($script:sfdxCommands | Where-Object id -eq $commandAst.CommandElements[1].Value).flags.PsObject.Properties | Where-Object Name -like $parameterToMatch | ForEach-Object {

0 commit comments

Comments
 (0)