Skip to content

Instantly share code, notes, and snippets.

@maxisam
Created February 9, 2025 19:58
Show Gist options
  • Save maxisam/9b5ece770c62e6a3ee978b4e671138d4 to your computer and use it in GitHub Desktop.
Save maxisam/9b5ece770c62e6a3ee978b4e671138d4 to your computer and use it in GitHub Desktop.
$origNvm = Get-Command nvm -CommandType Application -ErrorAction SilentlyContinue
if ($origNvm) { Set-Alias nvm_external $origNvm.Path }
function nvmFunc {
param(
[string]$Argument,
[string]$Version = ""
)
function Get-NvmVersion {
if (Test-Path ".nvmrc") {
return (Get-Content .nvmrc).Trim()
}
elseif (Test-Path ".node-version") {
return (Get-Content .node-version).Trim()
}
else {
Write-Warning "No .nvmrc or .node-version file found"
return $null
}
}
if ($Argument -eq "install" -and $Version -eq "") {
$ver = Get-NvmVersion
if ($ver) { & nvm install $ver }
}
elseif ($Argument -eq "use") {
if ($Version -eq "") {
$ver = Get-NvmVersion
}
else {
$ver = $Version
}
if ($ver) { & cmd /c nvm use $ver }
}
else {
& nvm_external $Argument
}
}
Set-alias nvm nvmFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment