Last active
May 17, 2024 08:56
-
-
Save flaviodelgrosso/1bc0993a9dc8fb6271ae0e0fa7f14dce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define variables | |
$nvmUrl = "https://github.com/coreybutler/nvm-windows/releases/latest/download/nvm-noinstall.zip" | |
$userFolder = [System.Environment]::GetFolderPath('UserProfile') | |
$nvmFolder = "$userFolder\nvm" | |
$nvmZipPath = "$userFolder\nvm-noinstall.zip" | |
$settingsFilePath = "$nvmFolder\settings.txt" | |
# Create NVM folder if it does not exist | |
if (-Not (Test-Path -Path $nvmFolder)) { | |
New-Item -ItemType Directory -Force -Path $nvmFolder | |
} | |
# Download NVM for Windows zip file | |
Write-Output "Downloading NVM for Windows..." | |
Invoke-WebRequest -Uri $nvmUrl -OutFile $nvmZipPath | |
# Unzip the downloaded file to the NVM folder | |
Write-Output "Unzipping NVM to user folder..." | |
Add-Type -AssemblyName System.IO.Compression.FileSystem | |
[System.IO.Compression.ZipFile]::ExtractToDirectory($nvmZipPath, $nvmFolder) | |
# Remove the downloaded zip file | |
Remove-Item -Path $nvmZipPath | |
# Update user environment variables | |
$nvmPath = "$nvmFolder" | |
$nodePath = "$nvmFolder\nodejs" | |
# Create settings.txt file with the environment variables | |
$settingsContent = @" | |
root: $nvmPath | |
path: $nodePath | |
arch: 64 | |
proxy: none | |
"@ | |
Write-Output "Creating settings.txt file..." | |
Set-Content -Path $settingsFilePath -Value $settingsContent | |
# Function to set a user environment variable persistently | |
function Set-UserEnvironmentVariable { | |
param ( | |
[string]$name, | |
[string]$value | |
) | |
[System.Environment]::SetEnvironmentVariable($name, $value, [System.EnvironmentVariableTarget]::User) | |
} | |
# Set NVM_HOME and NVM_SYMLINK environment variables persistently | |
Write-Output "Setting NVM_HOME and NVM_SYMLINK environment variables persistently..." | |
Set-UserEnvironmentVariable -name "NVM_HOME" -value $nvmPath | |
Set-UserEnvironmentVariable -name "NVM_SYMLINK" -value $nodePath | |
# Function to add a path to the user's PATH environment variable persistently | |
function Add-ToUserPath { | |
param ( | |
[string]$newPath | |
) | |
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) | |
if (-Not $currentPath.Split(';').Contains($newPath)) { | |
$newPath = "$currentPath;$newPath" | |
[System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::User) | |
} | |
} | |
# Add NVM and Node.js paths to the user's PATH environment variable persistently | |
Write-Output "Updating user PATH environment variable persistently..." | |
Add-ToUserPath -newPath $nvmPath | |
Add-ToUserPath -newPath $nodePath | |
# Inform the user to restart the terminal | |
Write-Output "NVM installation completed. Please restart your terminal to start using NVM." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment