#Requires -RunAsAdministrator function ExitWithCode { param ( $exitcode ) $host.SetShouldExit($exitcode) exit } # Print the splash Write-Host '***************************************' Write-Host '**SVR.JS installer for Windows Server**' Write-Host '***************************************' Write-Host '' # Check the CPU architecture $processor = Get-WmiObject -Class Win32_Processor $architectureInt = $processor.AddressWidth if ($processor.Architecture -eq 5) { $architecture = "arm64" } elseif ($architectureInt -eq 64) { $architecture = "x64" } elseif ($architectureInt -eq 32) { $architecture = "x86" } else { Write-Host "Invalid CPU architecture detected." ExitWithCode -exitcode 1 } # Select SVR.JS installation type Write-Host 'Select your SVR.JS installation type. Valid SVR.JS installation types:' Write-Host '0 - Latest stable version' Write-Host '1 - Latest LTS version' Write-Host '2 - Install and update manually' $ITP = Read-Host 'Your SVR.JS installation type' switch ($ITP) { 0 { $installType = 'stable' } 1 { $installType = 'lts' } 2 { $installType = 'manual' } default { Write-Host 'Invalid SVR.JS installation type!'; ExitWithCode -exitcode 1 } } # Create an SVR.JS installation directory if (!(Test-Path -Path "$env:SYSTEMDRIVE\svrjs")) { New-Item -Path "$env:SYSTEMDRIVE\" -Name "svrjs" -ItemType "directory" | Out-Null } # Create subdirectories if (!(Test-Path -Path "$env:SYSTEMDRIVE\svrjs\nodejs")) { New-Item -Path "$env:SYSTEMDRIVE\svrjs" -Name "nodejs" -ItemType "directory" | Out-Null } if (!(Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs")) { New-Item -Path "$env:SYSTEMDRIVE\svrjs" -Name "svrjs" -ItemType "directory" | Out-Null } # Download SVR.JS zip archive $manuallyInstalled = $False if ($installType -eq "manual") { $manuallyInstalled = $True $svrjsZipArchive = Read-Host "Path to SVR.JS zip archive" } elseif ($INSTALLTYPE -eq "stable") { $svrjsVersion = (Invoke-RestMethod -Uri "https://downloads.svrjs.org/latest.svrjs").Trim() if (-not $svrjsVersion) { Write-Host "There was a problem while determining latest SVR.JS version!" ExitWithCode -exitcode 1 } $svrjsZipArchive = "$env:SYSTEMDRIVE\svrjs\svrjs.zip" Invoke-WebRequest -Uri "https://downloads.svrjs.org/svr.js.$svrjsVersion.zip" -OutFile $svrjsZipArchive if (-not (Test-Path $svrjsZipArchive)) { Write-Host "There was a problem while downloading latest SVR.JS version!" ExitWithCode -exitcode 1 } } elseif ($INSTALLTYPE -eq "lts") { $svrjsVersion = (Invoke-RestMethod -Uri "https://downloads.svrjs.org/latest-lts.svrjs").Trim() if (-not $svrjsVersion) { Write-Host "There was a problem while determining latest LTS SVR.JS version!" ExitWithCode -exitcode 1 } $svrjsZipArchive = "$env:SYSTEMDRIVE\svrjs\svrjs.zip" Invoke-WebRequest -Uri "https://downloads.svrjs.org/svr.js.$svrjsVersion.zip" -OutFile $svrjsZipArchive if (-not (Test-Path $svrjsZipArchive)) { Write-Host "There was a problem while downloading latest LTS SVR.JS version!" ExitWithCode -exitcode 1 } } else { Write-Host "There was a problem determining SVR.JS installation type!" ExitWithCode -exitcode 1 } # Check if SVR.JS zip archive exists if (!(Test-Path -Path $svrjsZipArchive)) { Write-Host "Can't find SVR.JS archive! Make sure to download SVR.JS archive file from https://svrjs.org and rename it to 'svrjs.zip'." ExitWithCode -exitcode 1 } # Download Node.JS archive $nodejsZipArchive = "$env:SYSTEMDRIVE\svrjs\nodejs.zip" Invoke-WebRequest -Uri "https://nodejs.org/dist/latest-jod/node-latest-jod-win-$architecture.zip" -OutFile $nodejsZipArchive if (!(Test-Path -Path $nodejsZipArchive)) { Write-Host "Can't find Node.JS archive!" ExitWithCode -exitcode 1 } # Download WinSW $winsw = "$env:SYSTEMDRIVE\svrjs\winsw.exe" $winswUrl = "https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW-$architecture.exe" if ($architecture -eq 'arm64') { $winswUrl = "https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW.NET4.exe" } Invoke-WebRequest -Uri $winswUrl -OutFile $winsw if (!(Test-Path -Path $winsw)) { Write-Host "Can't find WinSW executable!" ExitWithCode -exitcode 1 } # Extract Node.JS files Write-Host "Extracting Node.JS files..." Expand-Archive -Path $nodejsZipArchive -DestinationPath "$env:SYSTEMDRIVE\svrjs\nodejs" -Force $nodejsDir = "$env:SYSTEMDRIVE\svrjs\nodejs\$((Get-ChildItem -Path "$env:SYSTEMDRIVE\svrjs\nodejs" -Directory | Where-Object { $_.Name -match "node-v\d+\.\d+\.\d+-win-.*" } | Sort-Object -Property LastWriteTime -Descending).Name)" Remove-Item -Path $nodejsZipArchive # Extract SVR.JS files Write-Host "Extracting SVR.JS files..." Expand-Archive -Path $svrjsZipArchive -DestinationPath "$env:SYSTEMDRIVE\svrjs\svrjs" -Force if (!($manuallyInstalled)) { Remove-Item -Path $svrjsZipArchive } if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs\svr.compressed" -PathType "Leaf") { Push-Location Set-Location -Path "$env:SYSTEMDRIVE\svrjs\svrjs" Start-Process -FilePath "$nodejsDir\node.exe" -ArgumentList "$env:SYSTEMDRIVE\svrjs\svrjs\svr.js" -Wait Pop-Location } if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs\svrjs.yaml" -PathType "Leaf") { $svrjsYamlConfig = @" global: wwwroot: `"$env:SYSTEMDRIVE\\svrjs\\wwwroot`" "@ $svrjsYamlConfig | Out-File -FilePath "$env:SYSTEMDRIVE\svrjs\svrjs\svrjs.yaml" -Encoding utf8 } else { Start-Process -FilePath "$nodejsDir\node.exe" -ArgumentList "-e `"var fs=require('fs'),config=JSON.parse(fs.readFileSync('$env:SYSTEMDRIVE\\svrjs\\svrjs\\config.json').toString());config.wwwroot='$env:SYSTEMDRIVE\\svrjs\\wwwroot',fs.writeFileSync('$env:SYSTEMDRIVE\\svrjs\\svrjs\\config.json',JSON.stringify(config,null,2));`"" -Wait } if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs\wwwroot" -PathType "Container") { if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\wwwroot") { Remove-Item -Path "$env:SYSTEMDRIVE\svrjs\wwwroot" -Recurse -Force | Out-Null New-Item -Path "$env:SYSTEMDRIVE\svrjs" -Name "wwwroot" -ItemType "directory" | Out-Null } Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\wwwroot" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" } else { if (!(Test-Path -Path "$env:SYSTEMDRIVE\svrjs\wwwroot")) { New-Item -Path "$env:SYSTEMDRIVE\svrjs" -Name "wwwroot" -ItemType "directory" | Out-Null } Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\index.html" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\tests.html" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\licenses" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\testdir" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\serverSideScript.js" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\logo.png" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\powered.png" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs\favicon.ico") { Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\favicon.ico" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" } if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs\views.txt") { Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\views.txt" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" } if (Test-Path -Path "$env:SYSTEMDRIVE\svrjs\svrjs\hviews.txt") { Move-Item -Path "$env:SYSTEMDRIVE\svrjs\svrjs\hviews.txt" -Destination "$env:SYSTEMDRIVE\svrjs\wwwroot" } } # Generate WinSW Configuration Write-Host "Generating WinSW configuration..." $winswConfig = @" svrjs SVR.JS This service runs SVR.JS - a web server running on Node.JS. $nodejsDir\node.exe $env:SYSTEMDRIVE\svrjs\svrjs\svr.js "@ $winswConfig | Out-File -FilePath "$env:SYSTEMDRIVE\svrjs\winsw.xml" -Encoding utf8 # Install the service Write-Host "Installing the SVR.JS service using WinSW..." Start-Process -FilePath $winsw -ArgumentList "install" -Wait Start-Service -Name "svrjs" # Create the uninstallation batch file $uninstallBat = @" @echo off sc stop svrjs %SYSTEMDRIVE%\svrjs\winsw.exe uninstall rd %SYSTEMDRIVE%\svrjs /s /q "@ $uninstallBat | Out-File -FilePath "$env:SYSTEMDRIVE\uninstall_svrjs.bat" -Encoding utf8 echo "Done! SVR.JS is installed successfully!"