四零语境后端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.1 KiB

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