Skip to content

Instantly share code, notes, and snippets.

@jeremygaither
Last active February 19, 2020 22:58
Show Gist options
  • Save jeremygaither/ec992483ead0617d28fb62f3ac12ac35 to your computer and use it in GitHub Desktop.
Save jeremygaither/ec992483ead0617d28fb62f3ac12ac35 to your computer and use it in GitHub Desktop.
$nvmexe = $null
$nodeexe = "node.exe"
if ($null -eq $env:NVM_HOME) {
Write-Output "NVM is not installed, or not installed correctly..."
} else {
Write-Output "NVM_HOME=$env:NVM_HOME"
Write-Output "NVM_SYMLINK=$env:NVM_SYMLINK"
Write-Output "PATH=$env:PATH"
$nvmexe = "$env:NVM_HOME\nvm.exe"
$nodeexe = "$env:NVM_SYMLINK\node.exe"
Write-Output "NVM managed versions:"
&$nvmexe list
}
Write-Output "Active Node.js Version:"
&$nodeexe --version
if ($LASTEXITCODE -ne 0) {
Write-Output "Unable to locade node.exe in path to check version!"
}
if (Test-Path -Path '.\nvmrc') {
$nvmrcVersion = Get-Content .nvmrc
Write-Output "Desired Node version: $nvmrcVersion"
} else {
Write-Output "No .nvmrc found!"
}
$ErrorActionPreference = "Stop"
choco upgrade -y chocolatey
choco uninstall -y nvm nvm.portable nodejs-lts
choco install -y nvm
refreshenv
Write-Output "NVM_HOME=$env:NVM_HOME"
Write-Output "NVM_SYMLINK=$env:NVM_SYMLINK"
Write-Output "PATH=$env:PATH"
$nvmexe = "$env:NVM_HOME\nvm.exe"
$nodeexe = "$env:NVM_SYMLINK\node.exe"
$nvmrcVersion = Get-Content .nvmrc
Write-Output "Desired Node version: $nvmrcVersion"
$nvmAvailable = (&$nvmexe list available)
$candidateVersionLines = $nvmAvailable | Where-Object -FilterScript {
$_ -like ('* ' + ($nvmrcVersion) + '.*')
}
$candidateVersions = @()
foreach ($line in $candidateVersionLines) {
$versions = $line.Split('|').Trim()
foreach ($version in $versions) {
if ([string]::IsNullOrWhiteSpace($version)) {
continue
}
Write-Output ("testing: $version")
if ($version -like ($nvmrcVersion + '.*')) {
Write-Output ("found: $version")
$candidateVersions += $version
}
}
}
Write-Output "candidateVersions: $candidateVersions"
$desiredVersion = $candidateVersions[0]
if ((&$nvmexe use $desiredVersion) -like '*not installed*') {
Write-Output "desiredVersion ""$desiredVersion"" not installed, installing..."
&$nvmexe install $desiredVersion
if (-not $?) {
Write-Warning 'There was some error installing the desired node version'
exit 1
}
&$nvmexe use $desiredVersion
if (-not $?) {
Write-Warning 'There was some error activating the desired node version'
exit 1
}
}
else {
Write-Output "desiredVersion ""$desiredVersion"" should be installed..."
&$nvmexe use $desiredVersion
}
while ($true) {
if (Test-Path "$env:NVM_SYMLINK") {
break
}
Write-Output "Waiting for nodejs symlink to become available..."
Start-Sleep -Seconds 5
}
$waitCount = 0
$maxWaits = 6
while ($true) {
if ($waitCount -gt $maxWaits) {
Write-Output "unable to switch node versions!"
exit 1
}
$nodeVersion = (&$nodeexe --version)
if ($nodeVersion -like "v$desiredVersion*") {
Write-Output "node version good: $nodeVersion"
break
}
else {
Write-Output "nodeVersion: $nodeVersion"
Write-Output "Waiting for symlink to update..."
Start-Sleep 10
$waitCount++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment