1
- <#
1
+ <#
2
2
. SYNOPSIS
3
3
Installs Node.js from https://nodejs.org/dist on a machine
4
4
. DESCRIPTION
@@ -13,53 +13,96 @@ param(
13
13
$Version
14
14
)
15
15
16
+ function DownloadAnInstall {
17
+ $tempPath = [System.IO.Path ]::GetTempPath()
18
+ $tempDir = Join-Path $tempPath nodejs
19
+ New-Item - Path " $tempDir " - ItemType " directory" - Force
20
+ $nodeFile = " node-v$Version -win-x64"
21
+ $url = " https://nodejs.org/dist/v$Version /$nodeFile .zip"
22
+ $integrityUrl = " https://nodejs.org/dist/v$Version /SHASUMS256.txt.asc"
23
+ Write-Host " Starting download of Node.js ${Version} from $url "
24
+ & $PSScriptRoot \Download.ps1 $url nodejs.zip
25
+ Write-Host " Done downloading NodeJS ${Version} "
26
+ Write-Host " Starting download of Node.js integrity file for ${Version} from $integrityUrl "
27
+ & $PSScriptRoot \Download.ps1 $integrityUrl nodejs.integrity.txt
28
+ Write-Host " Done downloading NodeJS ${Version} integrity file"
29
+
30
+ $match = Select-String - Path nodejs.integrity.txt - Pattern " $nodeFile .zip" ;
31
+ if ($null -eq $match ) {
32
+ Write-Host " Could not find $nodeFile .zip in integrity file"
33
+ return $false ;
34
+ }else {
35
+ Write-Host " Found $nodeFile .zip in integrity file"
36
+ }
37
+
38
+ $signature = ($match.Line -split ' ' )[0 ];
39
+ if ($null -eq $signature ) {
40
+ Write-Host " Could not find signature for $nodeFile .zip in integrity file"
41
+ return $false ;
42
+ }else {
43
+ Write-Host " Found signature $signature for $nodeFile .zip in integrity file" ;
44
+ }
45
+
46
+ $hash = Get-FileHash nodejs.zip - Algorithm SHA256;
47
+ if ($hash.Hash.ToLower () -ne $signature.ToLower ()) {
48
+ Write-Host " Hashes do not match, expected $signature , got $hash "
49
+ # Cleanup for next try
50
+ Remove-Item nodejs.zip;
51
+ return $false ;
52
+ }else {
53
+ $hashString = $hash.Hash.ToLower ();
54
+ Write-Host " Hashes match, expected $signature , got $hashString " ;
55
+ }
56
+
57
+ Write-Host " Extracting to $tempDir "
58
+
59
+ if (Get-Command - Name ' Microsoft.PowerShell.Archive\Expand-Archive' - ErrorAction Ignore) {
60
+ # Use built-in commands where possible as they are cross-plat compatible
61
+ Microsoft.PowerShell.Archive\Expand-Archive - Path " nodejs.zip" - DestinationPath $tempDir - Force
62
+ }
63
+ else {
64
+ Remove-Item $tempDir - Recurse - ErrorAction Ignore
65
+ # Fallback to old approach for old installations of PowerShell
66
+ Add-Type - AssemblyName System.IO.Compression.FileSystem
67
+ [System.IO.Compression.ZipFile ]::ExtractToDirectory(" nodejs.zip" , $tempDir )
68
+ }
69
+
70
+ Write-Host " Expanded Node.js to $tempDir , moving $tempDir \$nodeFile to $InstallDir subdir"
71
+ Move-Item $tempDir \$nodeFile $InstallDir
72
+ if (Test-Path " $InstallDir \node.exe" ) {
73
+ Write-Host " Node.exe copied to $InstallDir "
74
+ }
75
+ else {
76
+ Write-Host " Node.exe not copied to $InstallDir "
77
+ }
78
+ return $true ;
79
+ }
80
+
16
81
$ErrorActionPreference = ' Stop'
17
82
$ProgressPreference = ' SilentlyContinue' # Workaround PowerShell/PowerShell#2138
18
83
$InstallDir = $PSScriptRoot + ' \nodejs' # Always install to workitem root / nodejs
19
84
20
85
Set-StrictMode - Version 1
21
86
22
- if (Get-Command " node.exe" - ErrorAction SilentlyContinue)
23
- {
87
+ if (Get-Command " node.exe" - ErrorAction SilentlyContinue) {
24
88
Write-Host " Found node.exe in PATH"
25
89
exit
26
90
}
27
91
28
- if (Test-Path " $InstallDir \node.exe" )
29
- {
92
+ if (Test-Path " $InstallDir \node.exe" ) {
30
93
Write-Host " Node.exe found at $InstallDir \node.exe"
31
94
exit
32
95
}
33
96
34
- $nodeFile = " node-v$Version -win-x64"
35
- $url = " https://nodejs.org/dist/v$Version /$nodeFile .zip"
36
- Write-Host " Starting download of Node.js ${Version} from $url "
37
- & $PSScriptRoot \Download.ps1 $url nodejs.zip
38
- Write-Host " Done downloading NodeJS ${Version} "
39
-
40
- $tempPath = [System.IO.Path ]::GetTempPath()
41
- $tempDir = Join-Path $tempPath nodejs
42
- New-Item - Path " $tempDir " - ItemType " directory" - Force
43
- Write-Host " Extracting to $tempDir "
44
-
45
- if (Get-Command - Name ' Microsoft.PowerShell.Archive\Expand-Archive' - ErrorAction Ignore) {
46
- # Use built-in commands where possible as they are cross-plat compatible
47
- Microsoft.PowerShell.Archive\Expand-Archive - Path " nodejs.zip" - DestinationPath $tempDir - Force
48
- }
49
- else {
50
- Remove-Item $tempDir - Recurse - ErrorAction Ignore
51
- # Fallback to old approach for old installations of PowerShell
52
- Add-Type - AssemblyName System.IO.Compression.FileSystem
53
- [System.IO.Compression.ZipFile ]::ExtractToDirectory(" nodejs.zip" , $tempDir )
54
- }
55
-
56
- Write-Host " Expanded Node.js to $tempDir , moving $tempDir \$nodeFile to $InstallDir subdir"
57
- move $tempDir \$nodeFile $InstallDir
58
- if (Test-Path " $InstallDir \node.exe" )
59
- {
60
- Write-Host " Node.exe copied to $InstallDir "
61
- }
62
- else
63
- {
64
- Write-Host " Node.exe not copied to $InstallDir "
65
- }
97
+ $maxAttempts = 5 ;
98
+ $current = 0 ;
99
+ do {
100
+ Write-Host " Attempt $current of $maxAttempts to download and install Node.js"
101
+ $succeeded = DownloadAnInstall;
102
+ if ($succeeded -eq $false ) {
103
+ Write-Host " Failed to download and install Node.js"
104
+ }else {
105
+ Write-Host " Successfully downloaded and installed Node.js"
106
+ }
107
+ $current ++ ;
108
+ } while ($current -lt $maxAttempts -and $succeeded -eq $false );
0 commit comments