Skip to content

Commit 7aebe88

Browse files
authored
🔀 Merge pull request #2 from gluons/feat-options
Add options completion and update commands completion.
2 parents 67bc2e7 + 5eaac07 commit 7aebe88

34 files changed

+442
-33
lines changed

src/commands.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
$cmds = @(
1+
. $PSScriptRoot\options.ps1
2+
. $PSScriptRoot\subCommands.ps1
3+
4+
Set-Variable -Name commands -Value @(
25
'add',
6+
'audit',
37
'autoclean',
48
'bin',
59
'cache',
@@ -16,25 +20,29 @@ $cmds = @(
1620
'install',
1721
'licenses',
1822
'link',
23+
'list',
1924
'lockfile',
2025
'login',
2126
'logout',
22-
'list',
27+
'node',
2328
'outdated',
2429
'owner',
2530
'pack',
31+
'policies',
2632
'prune',
2733
'publish',
2834
'remove',
2935
'run',
30-
'self-update',
3136
'tag',
3237
'team',
3338
'test',
3439
'unlink',
40+
'unplug',
3541
'upgrade',
3642
'upgrade-interactive',
3743
'version',
3844
'versions',
39-
'why'
45+
'why',
46+
'workspace',
47+
'workspaces'
4048
)

src/completion.ps1

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
1-
@('yarn', 'yarn.cmd') | ForEach-Object {
2-
Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
3-
param($wordToComplete, $commandAst, $cursorPosition)
1+
Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlock {
2+
param($wordToComplete, $commandAst, $cursorPosition)
43

5-
$subCommand = $commandAst.CommandElements[1].Value
4+
. $PSScriptRoot\commands.ps1
5+
. $PSScriptRoot\utils.ps1
66

7-
switch ($subCommand) {
8-
'global' {
9-
. $PSScriptRoot\sub-commands\global.ps1
10-
}
11-
Default {
12-
. $PSScriptRoot\commands.ps1
7+
$searchBlock = { $_ -like "$wordToComplete*" }
8+
$completions = @()
9+
$secondPart = if ($commandAst.CommandElements[1]) { $commandAst.CommandElements[1].Value } else { $null } # Main command
10+
$thirdPart = if ($commandAst.CommandElements[2]) { $commandAst.CommandElements[2].Value } else { $null } # Sub-command
11+
$fourthPart = if ($commandAst.CommandElements[3]) { $commandAst.CommandElements[3].Value } else { $null } # Sub-command's option
1312

14-
# Don't complete any word after sub-command when it isn't one of above commands.
15-
if ($subCommand -in $cmds) {
16-
$cmds = @()
17-
}
18-
}
13+
# If word to complete is equal to main command, suggest all commands & options of `install` command
14+
if (AreEqual $secondPart $wordToComplete) {
15+
$completions += $commands | Where-Object $searchBlock | ForEach-Object {
16+
[System.Management.Automation.CompletionResult]::new($_, $_, 'Command', $_)
1917
}
18+
$completions += $options['install'] | Where-Object $searchBlock | ForEach-Object {
19+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)
20+
}
21+
}
22+
# If word to complete is equal to sub-command, suggest sub-commmands
23+
if ((-not $(AreEqual $secondPart $wordToComplete)) -and $(AreEqual $thirdPart $wordToComplete)) {
24+
$completions += $subCommands[$secondPart].Keys | Where-Object $searchBlock | ForEach-Object {
25+
[System.Management.Automation.CompletionResult]::new($_, $_, 'Command', $_)
26+
}
27+
}
28+
# If word to complete is equal to sub-command's options, suggest sub-command's options
29+
if (
30+
(-not $(AreEqual $secondPart $wordToComplete)) -and
31+
(-not $(AreEqual $thirdPart $wordToComplete)) -and
32+
$(AreEqual $fourthPart $wordToComplete)
33+
) {
34+
$subCommand = $subCommands[$secondPart][$thirdPart]
35+
$subCommandOptions = if ($subCommand.options) { $subCommand.options } else { $null }
2036

21-
$cmds |
22-
Where-Object { $_ -like "$wordToComplete*" } |
23-
Sort-Object |
24-
ForEach-Object {
25-
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
26-
}
37+
$completions += $subCommandOptions | Where-Object $searchBlock | ForEach-Object {
38+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)
39+
}
40+
}
41+
42+
# Always suggest global options
43+
$completions += $globalOptions | Where-Object $searchBlock | ForEach-Object {
44+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)
2745
}
46+
47+
return $completions
2848
}

src/options.ps1

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

src/options/__.ps1

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Global options
2+
Set-Variable -Name globalOptions -Value @(
3+
'-v',
4+
'--version',
5+
'--no-default-rc',
6+
'--use-yarnrc',
7+
'--verbose',
8+
'--offline',
9+
'--prefer-offline',
10+
'--enable-pnp',
11+
'--pnp',
12+
'--disable-pnp',
13+
'--strict-semver',
14+
'--json',
15+
'--ignore-scripts',
16+
'--har',
17+
'--ignore-platform',
18+
'--ignore-engines',
19+
'--ignore-optional',
20+
'--force',
21+
'--skip-integrity-check',
22+
'--check-files',
23+
'--no-bin-links',
24+
'--flat',
25+
'--prod',
26+
'--production',
27+
'--no-lockfile',
28+
'--pure-lockfile',
29+
'--frozen-lockfile',
30+
'--update-checksums',
31+
'--link-duplicates',
32+
'--link-folder',
33+
'--global-folder',
34+
'--modules-folder',
35+
'--preferred-cache-folder',
36+
'--cache-folder',
37+
'--mutex',
38+
'--emoji',
39+
'-s',
40+
'--silent',
41+
'--cwd',
42+
'--proxy',
43+
'--https-proxy',
44+
'--registry',
45+
'--no-progress',
46+
'--network-concurrency',
47+
'--network-timeout',
48+
'--non-interactive',
49+
'--scripts-prepend-node-path',
50+
'--no-node-version-check',
51+
'--focus',
52+
'--otp'
53+
)

src/options/add.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
if (-not $options) {
2+
$options = [ordered] @{ }
3+
}
4+
5+
# Options for `yarn add`
6+
$options['add'] = @(
7+
'-W',
8+
'--ignore-workspace-root-check',
9+
'-D',
10+
'--dev',
11+
'-P',
12+
'--peer',
13+
'-O',
14+
'--optional',
15+
'-E',
16+
'--exact',
17+
'-T',
18+
'--tilde',
19+
'-A',
20+
'--audit'
21+
)

src/options/audit.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if (-not $options) {
2+
$options = [ordered] @{ }
3+
}
4+
5+
# Options for `yarn audit`
6+
$options['audit'] = @(
7+
'--summary',
8+
'--groups',
9+
'--level'
10+
)

src/options/autoclean.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if (-not $options) {
2+
$options = [ordered] @{ }
3+
}
4+
5+
# Options for `yarn autoclean`
6+
$options['autoclean'] = @(
7+
'-I',
8+
'--init',
9+
'-F',
10+
'--force'
11+
)

src/options/cache.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (-not $options) {
2+
$options = [ordered] @{ }
3+
}
4+
5+
# Options for `yarn cache`
6+
$options['cache'] = @(
7+
'--pattern'
8+
)

src/options/check.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if (-not $options) {
2+
$options = [ordered] @{ }
3+
}
4+
5+
# Options for `yarn check`
6+
$options['check'] = @(
7+
'--integrity',
8+
'--verify-tree'
9+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if (-not $options) {
2+
$options = [ordered] @{ }
3+
}
4+
5+
# Options for `yarn generate-lock-entry`
6+
$options['generate-lock-entry'] = @(
7+
'--use-manifest',
8+
'--resolved',
9+
'--registry'
10+
)

0 commit comments

Comments
 (0)