Skip to content

Commit

Permalink
Update nodejs-utils.psm1. (#2685)
Browse files Browse the repository at this point in the history
Switch to https and minor formatting tweaks.
  • Loading branch information
XhmikosR authored and FeodorFitsner committed Oct 9, 2018
1 parent 457de26 commit ab48d6e
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions scripts/nodejs-utils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ function Get-Version([string]$str) {
number = 0
}

if($versionDigits.Length -gt 0) {
if ($versionDigits.Length -gt 0) {
$version.major = [int]$versionDigits[0]
}
if($versionDigits.Length -gt 1) {
if ($versionDigits.Length -gt 1) {
$version.minor = [int]$versionDigits[1]
}
if($versionDigits.Length -gt 2) {
if ($versionDigits.Length -gt 2) {
$version.build = [int]$versionDigits[2]
}
if($versionDigits.Length -gt 3) {
if ($versionDigits.Length -gt 3) {
$version.revision = [int]$versionDigits[3]
}

for($i = 0; $i -lt $versionDigits.Length; $i++) {
for ($i = 0; $i -lt $versionDigits.Length; $i++) {
$version.number += [long]$versionDigits[$i] -shl 16 * (3 - $i)
}

Expand All @@ -31,41 +31,41 @@ function Get-Version([string]$str) {
function Get-NodeJsInstallPackage {
param
(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory=$true)]
[string]$version,

[Parameter(Mandatory=$false)]
[string]$bitness = 'x86'
)

$v = Get-Version $version

if ($v.Major -ge 4 -and $bitness -eq 'x86') {
$packageUrl = "http://nodejs.org/dist/v$version/node-v$version-x86.msi"
$packageUrl = "https://nodejs.org/dist/v$version/node-v$version-x86.msi"
} elseif ($v.Major -ge 4 -and $bitness -eq 'x64') {
$packageUrl = "http://nodejs.org/dist/v$version/node-v$version-x64.msi"
$packageUrl = "https://nodejs.org/dist/v$version/node-v$version-x64.msi"
} elseif ($v.Major -eq 0 -and $bitness -eq 'x86') {
$packageUrl = "http://nodejs.org/dist/v$version/node-v$version-x86.msi"
$packageUrl = "https://nodejs.org/dist/v$version/node-v$version-x86.msi"
} elseif ($v.Major -ge 1 -and $bitness -eq 'x86') {
$packageUrl = "https://iojs.org/dist/v$version/iojs-v$version-x86.msi"
} elseif ($v.Major -eq 0) {
$packageUrl = "http://nodejs.org/dist/v$version/x64/node-v$version-x64.msi"
$packageUrl = "https://nodejs.org/dist/v$version/x64/node-v$version-x64.msi"
} elseif ($v.Major -ge 1) {
$packageUrl = "https://iojs.org/dist/v$version/iojs-v$version-x64.msi"
$packageUrl = "httpss://iojs.org/dist/v$version/iojs-v$version-x64.msi"
}

$packageFileName = Join-Path ([IO.Path]::GetTempPath()) $packageUrl.Substring($packageUrl.LastIndexOf('/') + 1)
(New-Object Net.WebClient).DownloadFile($packageUrl, $packageFileName)
return $packageFileName
}

function Get-InstalledNodeJsVersion() {
$nodePath = (cmd /c where node.exe)
if(-not $nodePath) {
$nodePath = (cmd /c where iojs.exe)
if (-not $nodePath) {
$nodePath = (cmd /c where iojs.exe)
}

if($nodePath) {
if ($nodePath) {
$bitness = 'x64'
if ($nodePath.indexOf('(x86)') -ne -1) {
$bitness = 'x86'
Expand All @@ -82,7 +82,7 @@ function Get-InstalledNodeJsVersion() {
function Remove-NodeJsInstallation {
param
(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory=$true)]
[string]$version,

[Parameter(Mandatory=$true)]
Expand All @@ -97,7 +97,7 @@ function Remove-NodeJsInstallation {
function Start-NodeJsInstallation {
param
(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory=$true)]
[string]$version,

[Parameter(Mandatory=$false)]
Expand All @@ -108,7 +108,7 @@ function Start-NodeJsInstallation {
if ($v.Major -eq 0 -or $v.Major -ge 4) {
$features = 'NodeRuntime,NodePerfCtrSupport,NodeEtwSupport,npm'
} else {
$features = 'NodeRuntime,NodeAlias,NodePerfCtrSupport,NodeEtwSupport,npm'
$features = 'NodeRuntime,NodeAlias,NodePerfCtrSupport,NodeEtwSupport,npm'
}

Write-Host "Installing $(ProductName($version)) v$version ($bitness)..."
Expand All @@ -119,7 +119,7 @@ function Start-NodeJsInstallation {
function Update-NodeJsInstallation {
param
(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory=$true)]
[string]$version,

[Parameter(Mandatory=$false)]
Expand All @@ -128,10 +128,9 @@ function Update-NodeJsInstallation {

$installedVersion = Get-InstalledNodeJsVersion

if($installedVersion -eq $null -or $installedVersion.version -ne $version -or $installedVersion.bitness -ne $bitness)
{
if ($installedVersion -eq $null -or $installedVersion.version -ne $version -or $installedVersion.bitness -ne $bitness) {
Write-Host "Updating $(ProductName($version)) v$version ($bitness)"
if($installedVersion) {
if ($installedVersion) {
Remove-NodeJsInstallation $installedVersion.version $installedVersion.bitness
}
Start-NodeJsInstallation $version $bitness
Expand All @@ -142,25 +141,24 @@ function Get-NodeJsLatestBuild([string]$majorVersion) {
# fetch available distros
$v = Get-Version $majorVersion
if ($v.Major -eq 0 -or $v.Major -ge 4) {
$content = (New-Object Net.WebClient).DownloadString('http://nodejs.org/dist/')
$content = (New-Object Net.WebClient).DownloadString('https://nodejs.org/dist/')
} else {
$content = (New-Object Net.WebClient).DownloadString('https://iojs.org/dist/')
$content = (New-Object Net.WebClient).DownloadString('https://iojs.org/dist/')
}

# parse versions and find the latest
$versions = (Select-String '>v(\d*\.\d*\.\d*)/<' -input $content -allmatches `
| % {$_.matches} | % { $_.groups[1].value } `
| Where-Object {"$_.".StartsWith("$majorVersion.") })

if($versions.Count -eq 0) {
if ($versions.Count -eq 0) {
return $null
}
elseif($versions.indexOf('.') -ne -1) {
} elseif ($versions.indexOf('.') -ne -1) {
return $versions
} else {
$maxVersion = $versions[0]
for($i = 0; $i -lt $versions.Count; $i++) {
if((Get-Version $versions[$i]).number -gt (Get-Version $maxVersion).number) {
for ($i = 0; $i -lt $versions.Count; $i++) {
if ((Get-Version $versions[$i]).number -gt (Get-Version $maxVersion).number) {
$maxVersion = $versions[$i]
}
}
Expand Down

0 comments on commit ab48d6e

Please sign in to comment.