-
Notifications
You must be signed in to change notification settings - Fork 10
/
install.ps1
142 lines (112 loc) · 4.14 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env pwsh
# Stop executing script on any error
$ErrorActionPreference = 'Stop'
# Do not show download progress
$ProgressPreference = 'SilentlyContinue'
# Taken from https://stackoverflow.com/a/34559554/6537420
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
$platform = $null
$architecture = $null
$pnpmName = $null
# PowerShell versions before 6.* were only for Windows OS
if ($PSVersionTable.PSVersion.Major -eq 5) {
$platform = 'win'
}
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($PSVersionTable.Platform -eq 'Unix') {
switch -Wildcard ($PSVersionTable.OS) {
'Darwin*' {
$platform = 'macos'
}
'Linux*' {
$platform = 'linux'
}
'Ubuntu*' {
$platform = 'linux'
}
}
# PowerShell does not seem to have normal cmdlets for retrieving system information, so we use UNAME(1) for this.
$arch = uname -m
switch -Wildcard ($arch) {
'x86_64' { $architecture = 'x64'; Break }
'amd64' { $architecture = 'x64'; Break }
'armv*' { $architecture = 'arm'; Break }
'arm64' { $architecture = 'arm64'; Break }
'aarch64' { $architecture = 'arm64'; Break }
}
# 'uname -m' in some cases mis-reports 32-bit OS as 64-bit, so double check
if ([System.Environment]::Is64BitOperatingSystem -eq $false) {
if ($architecture -eq 'x64') {
$architecture = 'i686'
}
if ($architecture -eq 'arm64') {
$architecture = 'arm'
}
}
$pnpmName = "pnpm"
}
if ($PSVersionTable.Platform -eq 'Win32NT') {
$platform = 'win'
}
}
if ($platform -eq 'win') {
if ([System.Environment]::Is64BitOperatingSystem -eq $true) {
$architecture = 'x64'
}
if ([System.Environment]::Is64BitOperatingSystem -eq $false) {
$architecture = 'i686'
}
$pnpmName = "pnpm.exe"
}
if ($null -eq $platform) {
Write-Error "Platform could not be determined! Only Windows, Linux and MacOS are supported."
}
switch ($architecture) {
'x64' { ; Break }
'arm64' { ; Break }
Default {
Write-Error "Sorry! pnpm currently only provides pre-built binaries for x86_64/arm64 architectures."
}
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$pkgInfo = Invoke-WebRequest "https://registry.npmjs.org/@pnpm/exe" -UseBasicParsing
$versionJson = $pkgInfo.Content | ConvertFrom-Json
$versions = Get-Member -InputObject $versionJson.versions -Type NoteProperty | Select-Object -ExpandProperty Name
$distTags = Get-Member -InputObject $versionJson.'dist-tags' -Type NoteProperty | Select-Object -ExpandProperty Name
$version = $null
$preferredVersion = "latest"
if ($null -ne $env:PNPM_VERSION -and $env:PNPM_VERSION -ne "") {
$preferredVersion = $env:PNPM_VERSION
}
if ($null -eq $version -and $preferredVersion -in $distTags) {
$version = $versionJson.'dist-tags' | Select-Object -ExpandProperty $preferredVersion
}
if ($null -eq $version -and $preferredVersion -in $versions) {
$version = $preferredVersion
}
if ($null -eq $version) {
Write-Host "Current tags:" -ForegroundColor Yellow -NoNewline
$versionJson.'dist-tags' | Format-List
Write-Host "Versions:" -ForegroundColor Yellow -NoNewline
$versionJson.versions | Get-Member -Type NoteProperty | Format-Wide -Property Name -AutoSize
Write-Error "Sorry! pnpm '$preferredVersion' version could not be found. Use one of the tags or published versions from the provided list"
}
Write-Host "Downloading pnpm from GitHub...`n" -ForegroundColor Green
$tempFileFolder = New-TemporaryDirectory
$tempFile = (Join-Path $tempFileFolder.FullName $pnpmName)
$archiveUrl="https://github.com/pnpm/pnpm/releases/download/v$version/pnpm-$platform-$architecture"
if ($platform -eq 'win') {
$archiveUrl="$archiveUrl.exe"
}
Invoke-WebRequest $archiveUrl -OutFile $tempFile -UseBasicParsing
Write-Host "Running setup...`n" -ForegroundColor Green
if ($platform -ne 'win') {
chmod +x $tempFile
}
Start-Process -FilePath $tempFile -ArgumentList "setup" -NoNewWindow -Wait -ErrorAction Continue
Remove-Item $tempFile
Remove-Item $tempFileFolder -Recurse