mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-20 04:14:28 +01:00
feat: powershell script now upgrades to the latest LTS node version
This commit is contained in:
parent
dea561fd59
commit
2876b213f2
@ -1,47 +1,49 @@
|
|||||||
# Step 1: Check if Node.js is installed and in the PATH
|
# Function to test if Node.js is available in PATH
|
||||||
function Test-NodeInPath {
|
function Test-NodeInPath {
|
||||||
$nodeInPath = $False
|
|
||||||
try {
|
try {
|
||||||
# Attempt to execute node command
|
$nodeVersion = node --version
|
||||||
node --version
|
Write-Output "Node.js is installed with version $nodeVersion."
|
||||||
$nodeInPath = $True
|
return $true
|
||||||
} catch {
|
} catch {
|
||||||
Write-Output "Node.js is installed but not found in PATH."
|
Write-Output "Node.js is not found in PATH."
|
||||||
|
return $false
|
||||||
}
|
}
|
||||||
return $nodeInPath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check for Node.js and try to add to PATH if necessary
|
# Function to fetch the latest LTS version of Node.js
|
||||||
|
function Get-LatestNodeLTSVersion {
|
||||||
|
$nodeDistUrl = "https://nodejs.org/dist/index.json"
|
||||||
|
$nodeVersions = Invoke-RestMethod -Uri $nodeDistUrl
|
||||||
|
$latestLTS = $nodeVersions | Where-Object { $_.lts -ne $false } | Sort-Object version -Descending | Select-Object -First 1
|
||||||
|
return $latestLTS
|
||||||
|
}
|
||||||
|
|
||||||
|
# Install or upgrade Node.js if not the latest LTS
|
||||||
|
$latestLTS = Get-LatestNodeLTSVersion
|
||||||
if (-Not (Get-Command node -ErrorAction SilentlyContinue) -or -Not (Test-NodeInPath)) {
|
if (-Not (Get-Command node -ErrorAction SilentlyContinue) -or -Not (Test-NodeInPath)) {
|
||||||
# Step 2: Download and install Node.js
|
Write-Output "Node.js is not the latest LTS or not found in PATH. Installing/upgrading..."
|
||||||
Write-Output "Installing Node.js..."
|
|
||||||
|
|
||||||
# Define the Node.js version to install
|
$nodeVersion = $latestLTS.version -replace "v", ""
|
||||||
$nodeVersion = "node-v16.14.2-win-x64"
|
$nodeLTSFileName = "node-$nodeVersion-win-x64"
|
||||||
$url = "https://nodejs.org/dist/v16.14.2/$nodeVersion.zip"
|
$url = "https://nodejs.org/dist/$latestLTS.version/$nodeLTSFileName.zip"
|
||||||
|
|
||||||
# Specify path to save the Node.js ZIP
|
$output = "$env:USERPROFILE\Downloads\$nodeLTSFileName.zip"
|
||||||
$output = "$env:USERPROFILE\Downloads\$nodeVersion.zip"
|
|
||||||
|
|
||||||
# Download Node.js
|
|
||||||
Invoke-WebRequest -Uri $url -OutFile $output
|
Invoke-WebRequest -Uri $url -OutFile $output
|
||||||
|
|
||||||
# Extract Node.js
|
|
||||||
$nodeExtractPath = "$env:USERPROFILE\NodeJS"
|
$nodeExtractPath = "$env:USERPROFILE\NodeJS"
|
||||||
Expand-Archive -LiteralPath $output -DestinationPath $nodeExtractPath
|
Expand-Archive -LiteralPath $output -DestinationPath $nodeExtractPath -Force
|
||||||
|
|
||||||
# Add Node.js to PATH
|
$newPath = "$nodeExtractPath\$nodeLTSFileName"
|
||||||
$newPath = "$nodeExtractPath\$nodeVersion"
|
|
||||||
$env:Path += ";$newPath"
|
$env:Path += ";$newPath"
|
||||||
[Environment]::SetEnvironmentVariable("Path", $env:Path, [EnvironmentVariableTarget]::Machine)
|
[Environment]::SetEnvironmentVariable("Path", $env:Path, [EnvironmentVariableTarget]::Machine)
|
||||||
|
|
||||||
if (Test-NodeInPath) {
|
if (Test-NodeInPath) {
|
||||||
Write-Output "Node.js installed and added to PATH successfully."
|
Write-Output "Node.js upgraded/installed to latest LTS version successfully."
|
||||||
} else {
|
} else {
|
||||||
Write-Output "Failed to add Node.js to PATH. Please add manually to System Environment Variables."
|
Write-Output "Failed to add Node.js to PATH. Please add manually to System Environment Variables."
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Output "Node.js is already installed and available in PATH."
|
Write-Output "Node.js is already installed with the latest LTS version."
|
||||||
}
|
}
|
||||||
|
|
||||||
# Step 3: Install Nx CLI globally using npm
|
# Step 3: Install Nx CLI globally using npm
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user