Skip to content

Commit 3084887

Browse files
authored
🔀 Merge pull request #4 from gluons/script-completion
Add script name completion
2 parents 352b536 + 8153fb4 commit 3084887

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-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/utils.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,46 @@ function AreEqual($a, $b) {
99
return $a -eq $b
1010
}
1111
}
12+
13+
function Read-PackageJson {
14+
begin {
15+
$ErrorActionPreference = 'SilentlyContinue'
16+
}
17+
18+
process {
19+
$cwd = Get-Location
20+
$packagePath = Join-Path $cwd 'package.json'
21+
22+
if (-not (Test-Path $packagePath -PathType Leaf)) {
23+
return $null
24+
}
25+
26+
$packagePath = Resolve-Path $packagePath
27+
28+
return Get-Content $packagePath | ConvertFrom-Json
29+
}
30+
}
31+
32+
function Get-PackageScripts {
33+
begin {
34+
$ErrorActionPreference = 'SilentlyContinue'
35+
}
36+
37+
process {
38+
$package = Read-PackageJson
39+
40+
if ((-not $package) -or (-not $package.scripts)) {
41+
return $()
42+
}
43+
44+
$scripts = $package.scripts | Get-Member -MemberType NoteProperty | ForEach-Object {
45+
$_.Name
46+
}
47+
48+
if (-not $scripts) {
49+
return $()
50+
}
51+
52+
return $scripts
53+
}
54+
}

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)