Skip to content

Instantly share code, notes, and snippets.

@fellypsantos
Created September 28, 2024 00:42
Show Gist options
  • Save fellypsantos/814e60fb6674e8050936cdcaf4347239 to your computer and use it in GitHub Desktop.
Save fellypsantos/814e60fb6674e8050936cdcaf4347239 to your computer and use it in GitHub Desktop.
Script to setup environment variables to make react-native build work.
# Function to set an environment variable if it doesn't exist
function Set-EnvironmentVariableIfNotExists {
param (
[string]$name,
[string]$value,
[string]$target = "User" # By default, set for current user
)
# Check if the environment variable exists
$currentValue = [System.Environment]::GetEnvironmentVariable($name, $target)
if (-not $currentValue) {
# If it doesn't exist, set the environment variable
[System.Environment]::SetEnvironmentVariable($name, $value, $target)
Write-Host "Environment variable '$name' set to '$value'"
} else {
Write-Host "Environment variable '$name' already exists with value: '$currentValue'"
}
}
# Function to add a directory to the user's Path if it's not already there
function Add-ToUserPath {
param (
[string]$newPath
)
# Get the current user's Path environment variable
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
# Check if the path already exists in the Path variable
if ($currentPath -notlike "*$newPath*") {
# Append the new path
$newPathValue = "$currentPath;$newPath"
# Set the updated Path environment variable for the current user
[System.Environment]::SetEnvironmentVariable("Path", $newPathValue, "User")
# Display success message
Write-Host "Added $newPath to the user's Path environment variable"
} else {
Write-Host "$newPath is already in the user's Path environment variable"
}
}
# Set ANDROID_HOME
$androidHome = "$env:LOCALAPPDATA\Android\Sdk"
Set-EnvironmentVariableIfNotExists -name "ANDROID_HOME" -value $androidHome
# Set JAVA_HOME (JDK bundled with Android Studio)
$javaHome = "C:\Program Files\Android\Android Studio\jbr"
Set-EnvironmentVariableIfNotExists -name "JAVA_HOME" -value $javaHome
# Add platform-tools to the user's Path
$platformToolsPath = "$env:LOCALAPPDATA\Android\Sdk\platform-tools"
Add-ToUserPath -newPath $platformToolsPath
Write-Host "React Native environment setup is complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment