Skip to content

Commit

Permalink
✨ feat: nvm completion for powershell (windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquindev committed Dec 8, 2024
1 parent b6e5be8 commit cd5c940
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions dotposh/Config/posh-completions/nvm.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# powershell completion for nvm-for-windows

if (Get-Command nvm -ErrorAction SilentlyContinue) {
# nvm powershell completion start
$script:NvmCommands = @(
'arch', 'current', 'debug', 'install', 'list', 'ls', 'on', 'off', 'proxy', 'node_mirror', 'npm_mirror', 'uninstall', 'use', 'root', 'v', 'version'
)

$script:NvmSubcommands = @{
arch = '32 64'
list = 'available'
ls = 'available'
}

function script:NvmExpandCmdParams($cmds, $cmd, $filter) {
$cmds.$cmd -split ' ' | Where-Object { $_ -like "$filter*" }
}

function script:NvmExpandCmd($filter) {
$cmdList = @()
$cmdList += $NvmCommands
$cmdList -like "$filter*" | Sort-Object
}

function script:NvmExpandParamValues($cmd, $param, $filter) {
$NvmParamValues[$cmd][$param] -split ' ' |
Where-Object { $_ -like "$filter*" } |
Sort-Object
}

function script:NvmVersions($filter, $use) {
$versions = @()
if ($use) {
$versions += $NvmSubcommands["use"] -split ' '
}
$versions += Get-ChildItem -Filter '*v*' -Path "$env:NVM_HOME\nodejs" -Directory -Name | Where-Object { $_ -like "$filter*" }
$versions -like "$filter*"
}

function script:NvmAvailableVersions($filter, $install) {
$versions = @()
if ($install) {
$versions += $NvmSubcommands["install"] -split ' '
}
$versions += Invoke-WebRequest -UseBasicParsing -Uri https://nodejs.org/dist/index.json | ConvertFrom-Json | ForEach-Object { $_.version }
$versions -like "$filter*"
}

function script:NvmTabExpansion($lastBlock) {
switch -regex ($lastBlock) {
# nvm uninstall <version>
"^uninstall\s+(?:.+\s+)?(?<version>[\w][\-\.\w]*)?$" {
return NvmVersions $matches['version'] $false
}

# nvm use <version>
"^use\s+(?:.+\s+)?(?<version>[\w][\-\.\w]*)?$" {
return NvmVersions $matches['version'] $true
}

# nvm install <version>
"^install\s+(?:.+\s+)?(?<version>[\w][\-\.\w]*)?$" {
return NvmAvailableVersions $matches['version'] $true
}

# nvm <cmd> <subcommand>
"^(?<cmd>$($NvmSubcommands.Keys -join '|'))\s+(?<op>\S*)$" {
return NvmExpandCmdParams $NvmSubcommands $matches['cmd'] $matches['op']
}

# nvm <cmd>
"^(?<cmd>\S*)$" {
return NvmExpandCmd $matches['cmd'] $true
}
}
}

Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -MaxTriggerCount 1 -Action {
Register-ArgumentCompleter -Native -CommandName @('nvm', 'nvm.exe') -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$rest = $commandAst.CommandElements[1..$commandAst.CommandElements.Count] -join ' '
if ($rest -ne "" -and $wordToComplete -eq "") {
$rest += " "
}
NvmTabExpansion $rest
}
} | Out-Null
# nvm powershell completion end
}

0 comments on commit cd5c940

Please sign in to comment.