Skip to content

Commit 8153fb4

Browse files
committed
✨ Add completion for package's scripts
1 parent cc3d423 commit 8153fb4

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/commands.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
. $PSScriptRoot\options.ps1
22
. $PSScriptRoot\subCommands.ps1
3+
. $PSScriptRoot\values.ps1
34

45
Set-Variable -Name commands -Value @(
56
'add',

src/completion.ps1

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlo
1010

1111
# Main command
1212
$mainCommand = if ($commandAst.CommandElements[1]) { $commandAst.CommandElements[1].Value } else { $null }
13-
# Sub-command or command's options
14-
$subCommandOrOption = if ($commandAst.CommandElements[2]) { $commandAst.CommandElements[2].Value } else { $null }
13+
# Command's value or sub-command or command's options
14+
$valueOrsubCommandOrOption = if ($commandAst.CommandElements[2]) { $commandAst.CommandElements[2].Value } else { $null }
1515
# Sub-command's option or option's value of main command
1616
$subCommandOptionOrOptionValue = if ($commandAst.CommandElements[3]) { $commandAst.CommandElements[3].Value } else { $null }
1717

@@ -24,8 +24,14 @@ Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlo
2424
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)
2525
}
2626
}
27-
# If word to complete is equal to sub-command/command's options, suggest sub-commmands and command's options
28-
elseif (AreEqual $subCommandOrOption $wordToComplete) {
27+
# If word to complete is equal to command's value/sub-command/command's options, suggest command's values and sub-commmands and command's options
28+
elseif (AreEqual $valueOrsubCommandOrOption $wordToComplete) {
29+
# Main command's values
30+
if ($commandValues[$mainCommand]) {
31+
$completions += Invoke-Command -ScriptBlock $commandValues[$mainCommand] | ForEach-Object {
32+
[System.Management.Automation.CompletionResult]::new($_, $_, 'DynamicKeyword', $_)
33+
}
34+
}
2935
# Sub-commands
3036
if ($subCommands[$mainCommand]) {
3137
$completions += $subCommands[$mainCommand].Keys | Where-Object $searchBlock | ForEach-Object {
@@ -43,8 +49,8 @@ Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlo
4349
# suggest main command option's values or sub-command's options
4450
elseif (AreEqual $subCommandOptionOrOptionValue $wordToComplete) {
4551
# Main command option's value
46-
if ($options[$mainCommand][$subCommandOrOption]) {
47-
$optionValues = $options[$mainCommand][$subCommandOrOption]
52+
if ($options[$mainCommand][$valueOrsubCommandOrOption]) {
53+
$optionValues = $options[$mainCommand][$valueOrsubCommandOrOption]
4854

4955
$completions += $optionValues | Where-Object $searchBlock | ForEach-Object {
5056
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
@@ -55,8 +61,8 @@ Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlo
5561
}
5662

5763
# Sub-command's options
58-
if ($subCommands[$mainCommand][$subCommandOrOption]) {
59-
$subCommand = $subCommands[$mainCommand][$subCommandOrOption]
64+
if ($subCommands[$mainCommand][$valueOrsubCommandOrOption]) {
65+
$subCommand = $subCommands[$mainCommand][$valueOrsubCommandOrOption]
6066
$subCommandOptionOrOptionValues = if ($subCommand.options) { $subCommand.options.Keys } else { $null }
6167

6268
$completions += $subCommandOptionOrOptionValues | Where-Object $searchBlock | ForEach-Object {

src/values.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$valuesScriptPath = "$PSScriptRoot\values" | Resolve-Path
2+
$valuesScripts = Get-ChildItem $valuesScriptPath | Where-Object {
3+
$_.Name -like '*.ps1'
4+
}
5+
6+
# Load all command's values
7+
$valuesScripts | ForEach-Object {
8+
. $_.FullName
9+
}

src/values/run.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
. $PSScriptRoot\..\utils.ps1
2+
3+
if (-not $commandValues) {
4+
$commandValues = [ordered] @{ }
5+
}
6+
7+
$commandValues['run'] = {
8+
$scriptNames = Get-PackageScripts
9+
10+
return $scriptNames
11+
}

0 commit comments

Comments
 (0)