# Vite+ CLI Installer for Windows # https://vite.plus/ps1 # # Usage: # irm https://vite.plus/ps1 | iex # # Environment variables: # VP_VERSION - Version to install (default: latest) # VP_HOME - Optional pin for the monolithic layout. If unset, Vite+ reuses an # existing %USERPROFILE%\.vite-plus install. Otherwise, the complete # VP_*_DIR group or Windows Local and Roaming folders select the roots. # VP_BIN_DIR / VP_DATA_DIR / VP_CACHE_DIR - Complete group of absolute # category overrides # NPM_CONFIG_REGISTRY - Custom npm registry URL (default: https://registry.npmjs.org) # VP_LOCAL_TGZ - Path to local vite-plus.tgz (for development/testing) # VP_PR_VERSION - PR number or commit SHA to install from the registry bridge # (for temporary testing of unreleased builds, e.g. VP_PR_VERSION=1569). # When set, overrides VP_VERSION and installs the clearly-defined # 0.0.0-commit. build through the bridge instead of npm. # When dot-sourced, returns script-scoped InstallDir, ShimDir, CacheDir, ConfigDir, and StateDir. # These are resolved paths, not VP_* overrides for subsequent commands. $ViteVersion = if ($env:VP_VERSION) { $env:VP_VERSION } else { "latest" } # npm registry URL (strip trailing slash if present) $NpmRegistry = if ($env:NPM_CONFIG_REGISTRY) { $env:NPM_CONFIG_REGISTRY.TrimEnd('/') } else { "https://registry.npmjs.org" } # Local tarball for development/testing $LocalTgz = $env:VP_LOCAL_TGZ # Local binary path (set by install-global-cli.ts for local dev) $LocalBinary = $env:VP_LOCAL_BINARY # PR number or commit SHA to install as a test build (registry bridge mode) $PrVersion = $env:VP_PR_VERSION # Registry bridge that serves PR preview builds as clearly-versioned packages. # The pkg.pr.new-style download URL (BridgeDownloadBase) 302-redirects to a # canonical 0.0.0-commit. tarball; the registry (BridgeRegistry) resolves # those commit versions (and proxies everything else to npmjs) so a full install # pulls a coherent, clearly-defined test build. $BridgeDownloadBase = "https://registry-bridge.viteplus.dev/voidzero-dev/vite-plus" $BridgeRegistry = "https://registry-bridge.viteplus.dev/" $SupportedProvenancePredicateTypes = @( "https://slsa.dev/provenance/v1", "https://slsa.dev/provenance/v0.2" ) $script:InstallStopSignal = 'VP_INSTALL_STOP' $script:PackageMetadata = $null # Legacy is published beside this bootstrap; preview builds rewrite this origin. $LegacyInstallerUrl = if ($env:VP_LEGACY_INSTALLER_URL) { $env:VP_LEGACY_INSTALLER_URL } else { 'https://viteplus.dev/install-legacy.ps1' } $InstallerDirectory = $PSScriptRoot function Write-Info { param([string]$Message) Write-Host "info: " -ForegroundColor Blue -NoNewline Write-Host $Message } function Test-IsInstallStopException { param( [System.Management.Automation.ErrorRecord]$ErrorRecord ) return $ErrorRecord.Exception.Message -eq $script:InstallStopSignal } function Test-ShouldKeepShellOpenAfterFailure { # Only `irm ... | iex` typed in an already-open interactive shell should keep the # session alive. CI, script files, and `powershell -Command "..."` must exit non-zero. if ($env:CI -eq "true") { return $false } if ($PSCommandPath) { return $false } if (-not [Environment]::UserInteractive) { return $false } try { $commandLine = (Get-CimInstance Win32_Process -Filter "ProcessId=$PID").CommandLine if ($commandLine -match '(^|\s)-Command(\s|$)') { return $false } } catch { return $false } return $true } function Exit-Installer { param([int]$Code = 1) $global:LASTEXITCODE = $Code if (-not (Test-ShouldKeepShellOpenAfterFailure)) { exit $Code } throw $script:InstallStopSignal } function Write-Error-Exit { param([string]$Message) Write-Host "error: " -ForegroundColor Red -NoNewline Write-Host $Message Exit-Installer } function Resolve-BridgeCommitVersion { param([string]$Ref) $sha = $Ref if ($Ref -notmatch '^[0-9a-fA-F]{40}$') { try { $resp = Invoke-WebRequest -Uri "$BridgeDownloadBase@$Ref" -Method Head -UseBasicParsing -ErrorAction Stop } catch { return $null } $commitKey = @($resp.Headers['x-commit-key'])[0] if (-not $commitKey) { return $null } $sha = ($commitKey -split ':')[-1] } if ($sha -notmatch '^[0-9a-fA-F]{40}$') { return $null } return "0.0.0-commit.$sha" } function Get-Architecture { if ([Environment]::Is64BitOperatingSystem) { if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { return "arm64" } else { return "x64" } } else { Write-Error-Exit "32-bit Windows is not supported" } } function Get-MetadataProperty { param([object]$Metadata, [string[]]$Path) # Optional JSON fields must also be readable when the caller enables StrictMode. # Walk nested properties individually; a literal dotted key is not a nested field. foreach ($name in $Path) { if ($Metadata -is [System.Collections.IDictionary]) { $Metadata = $Metadata[$name] } elseif ($null -ne $Metadata -and $null -ne $Metadata.PSObject.Properties[$name]) { $Metadata = $Metadata.PSObject.Properties[$name].Value } else { return $null } } # Preserve arrays so malformed values cannot become valid scalars on the pipeline. return ,$Metadata } function Get-PackageMetadata { if ($null -eq $script:PackageMetadata) { $versionPath = $ViteVersion $metadataUrl = "$NpmRegistry/vite-plus/$versionPath" try { $script:PackageMetadata = Invoke-RestMethod $metadataUrl } catch { if (Test-IsInstallStopException $_) { throw } # Try to extract npm error message from response $errorMsg = if ($_.ErrorDetails) { $_.ErrorDetails.Message } if ($errorMsg) { try { $errorJson = $errorMsg | ConvertFrom-Json $registryError = Get-MetadataProperty $errorJson @('error') if ($registryError) { Write-Error-Exit "Failed to fetch version '${versionPath}': $registryError`n URL: $metadataUrl" } } catch { if (Test-IsInstallStopException $_) { throw } # JSON parsing failed, fall through to generic error } } Write-Error-Exit "Failed to fetch package metadata from: $metadataUrl`nError: $_" } # Check for error in successful response # npm can return {"error":"..."} object or a plain string like "version not found: test" if ($script:PackageMetadata -is [string]) { # Some registries (e.g. JFrog) may return JSON with a non-JSON content type, # causing Invoke-RestMethod to return a raw string. Try parsing it as JSON first. try { $script:PackageMetadata = $script:PackageMetadata | ConvertFrom-Json } catch { if (Test-IsInstallStopException $_) { throw } # Not valid JSON - treat as plain string error Write-Error-Exit "Failed to fetch version '${versionPath}': $script:PackageMetadata`n URL: $metadataUrl" } } $registryError = Get-MetadataProperty $script:PackageMetadata @('error') if ($registryError) { Write-Error-Exit "Failed to fetch version '${versionPath}': $registryError`n URL: $metadataUrl" } } return $script:PackageMetadata } function Get-VersionFromMetadata { $metadata = Get-PackageMetadata $version = Get-MetadataProperty $metadata @('version') if (-not $version) { Write-Error-Exit "Failed to extract version from package metadata" } return $version } function Get-PlatformPackageMetadata { param( [string]$PackageName, [string]$Version, [string]$Registry = $NpmRegistry ) $encodedPackageName = [System.Uri]::EscapeDataString($PackageName) $metadataUrl = "$($Registry.TrimEnd('/'))/$encodedPackageName/$Version" try { $metadata = Invoke-RestMethod -Uri $metadataUrl -Headers @{ Accept = "application/json" } } catch { if (Test-IsInstallStopException $_) { throw } $errorMsg = if ($_.ErrorDetails) { $_.ErrorDetails.Message } if ($errorMsg) { try { $errorJson = $errorMsg | ConvertFrom-Json $registryError = Get-MetadataProperty $errorJson @('error') if ($registryError) { Write-Error-Exit "Failed to fetch CLI package metadata '${PackageName}@${Version}': $registryError`n URL: $metadataUrl" } } catch { if (Test-IsInstallStopException $_) { throw } # JSON parsing failed, fall through to the generic network error. } } Write-Error-Exit "Failed to fetch CLI package metadata from: $metadataUrl`nError: $_" } # Some custom registries return JSON using a non-JSON content type. Match # Get-PackageMetadata by parsing that raw string before inspecting fields. if ($metadata -is [string]) { try { $metadata = $metadata | ConvertFrom-Json } catch { if (Test-IsInstallStopException $_) { throw } Write-Error-Exit "Failed to parse CLI package metadata '${PackageName}@${Version}'`n URL: $metadataUrl" } } $registryError = Get-MetadataProperty $metadata @('error') if ($registryError) { Write-Error-Exit "Failed to fetch CLI package metadata '${PackageName}@${Version}': $registryError`n URL: $metadataUrl" } return $metadata } function Get-VerifiedPlatformDistribution { param( [object]$Metadata, [string]$PackageName, [string]$Version ) # Check nested provenance metadata; registry signatures and package-defined # top-level fields do not satisfy the release check. $predicateType = Get-MetadataProperty $Metadata @('dist', 'attestations', 'provenance', 'predicateType') # Commit preview builds do not carry npm provenance, regardless of registry. $isCommitPreview = $Version -cmatch '\A0\.0\.0-commit\.[0-9a-fA-F]{40}\z' if (-not $isCommitPreview -and $SupportedProvenancePredicateTypes -notcontains $predicateType) { Write-Error-Exit "Refusing to install ${PackageName}@${Version}: the package does not contain supported npm provenance metadata. Vite+ only installs release binaries published with npm provenance." } $tarballUrl = Get-MetadataProperty $Metadata @('dist', 'tarball') if (-not $tarballUrl) { Write-Error-Exit "CLI package metadata for ${PackageName}@${Version} does not include dist.tarball" } $integrity = Get-MetadataProperty $Metadata @('dist', 'integrity') # Require a canonical base64 encoding of a 64-byte SHA-512 digest. if ($integrity -isnot [string] -or $integrity -cnotmatch '\Asha512-[A-Za-z0-9+/]{85}[AQgw]==\z') { Write-Error-Exit "CLI package metadata for ${PackageName}@${Version} does not include a valid SHA-512 dist.integrity" } return [pscustomobject]@{ TarballUrl = [string]$tarballUrl Integrity = $integrity } } function Assert-ArchiveIntegrity { param( [string]$Path, [string]$Integrity ) $expectedBytes = [Convert]::FromBase64String($Integrity.Substring(7)) $expected = [BitConverter]::ToString($expectedBytes).Replace('-', '') $actual = (Get-FileHash -LiteralPath $Path -Algorithm SHA512 -ErrorAction Stop).Hash if ($actual -cne $expected) { Write-Error-Exit "Platform package integrity mismatch: the downloaded archive does not match dist.integrity" } } function Get-PlatformSuffix { param([string]$Platform) # Windows needs -msvc suffix, other platforms map directly if ($Platform.StartsWith("win32-")) { return "${Platform}-msvc" } return $Platform } function Get-UserHomeDir { if (-not [string]::IsNullOrWhiteSpace($env:USERPROFILE)) { return $env:USERPROFILE } if (-not [string]::IsNullOrWhiteSpace($env:HOME)) { return $env:HOME } return [Environment]::GetFolderPath('UserProfile') } # Released setup-vp versions add %USERPROFILE%\.vite-plus\bin to the GitHub # Actions PATH. They do this after the installer exits. Use the monolithic # layout until setup-vp declares support for VP_DUMP_DIRS. function Enable-SetupVpLegacyCompatibility { if ($env:GITHUB_ACTION_REPOSITORY -cne "voidzero-dev/setup-vp") { return } if ($env:VP_VPDIRS_AWARE -eq "1") { return } if ($env:VP_HOME -or $env:VP_BIN_DIR -or $env:VP_DATA_DIR -or $env:VP_CACHE_DIR) { return } $userHome = Get-UserHomeDir if ([string]::IsNullOrWhiteSpace($userHome)) { Write-Error-Exit "Vite+ could not resolve the user home directory." } $env:VP_HOME = Join-Path $userHome ".vite-plus" } function Main { # Keep installation rules stable even when a profile selects a future Latest. # Function scope also preserves the caller's setting for `irm ... | iex`. Set-StrictMode -Version 3.0 Enable-SetupVpLegacyCompatibility if ($PrVersion -and $LocalTgz) { Write-Error-Exit "VP_PR_VERSION and VP_LOCAL_TGZ cannot be used together" } # Suppress progress bars for cleaner output $ProgressPreference = 'SilentlyContinue' # Local development mode: use local tgz if ($LocalTgz) { # Validate local tgz if (-not (Test-Path $LocalTgz)) { Write-Error-Exit "Local tarball not found: $LocalTgz" } # Use version as-is (default to "local-dev") if ($ViteVersion -eq "latest" -or $ViteVersion -eq "test") { $ViteVersion = "local-dev" } if (-not $LocalBinary -or -not (Test-Path -LiteralPath $LocalBinary -PathType Leaf)) { Write-Error-Exit "Set VP_LOCAL_BINARY when you use VP_LOCAL_TGZ." } } elseif ($PrVersion) { # Registry bridge mode: resolve the requested PR/SHA to the bridge's # immutable commit version (0.0.0-commit.), the clearly-defined test # version we install. Legacy receives the full SHA as its preview ref. $PrCommitVersion = Resolve-BridgeCommitVersion -Ref $PrVersion if (-not $PrCommitVersion) { Write-Error-Exit "Could not resolve a registry bridge build for $PrVersion" } $ViteVersion = $PrCommitVersion Write-Info "Using registry bridge build: $PrCommitVersion" } else { # Fetch package metadata and resolve version from npm $ViteVersion = Get-VersionFromMetadata } Get-PayloadAndHandoff } function Get-PayloadAndHandoff { $arch = Get-Architecture $platform = "win32-$arch" $binaryName = "vp.exe" # Keep acquisition separate from permanent installation. The bootstrap owns cleanup. $platformTempExtract = $null try { if (-not $LocalTgz) { # npm registry or registry bridge (when PrVersion is set) $platformSuffix = Get-PlatformSuffix -Platform $platform $packageName = "@voidzero-dev/vite-plus-cli-$platformSuffix" $registry = if ($PrVersion) { $BridgeRegistry } else { $NpmRegistry } # Preview metadata binds the same immutable commit version to its archive. $platformMetadata = Get-PlatformPackageMetadata -PackageName $packageName -Version $ViteVersion -Registry $registry $distribution = Get-VerifiedPlatformDistribution -Metadata $platformMetadata -PackageName $packageName -Version $ViteVersion $platformUrl = $distribution.TarballUrl $platformTempFile = New-TemporaryFile try { Invoke-WebRequest -Uri $platformUrl -OutFile $platformTempFile Assert-ArchiveIntegrity -Path $platformTempFile -Integrity $distribution.Integrity # Create temp extraction directory $platformTempExtract = Join-Path $env:TEMP "vite-platform-$(Get-Random)" New-Item -ItemType Directory -Force -Path $platformTempExtract | Out-Null # Extract the package & "$env:SystemRoot\System32\tar.exe" -xzf $platformTempFile -C $platformTempExtract if ($LASTEXITCODE -ne 0) { Write-Error-Exit "Failed to extract platform package from: $platformUrl" } } finally { Remove-Item $platformTempFile -ErrorAction SilentlyContinue } $binarySource = Join-Path (Join-Path $platformTempExtract "package") $binaryName if (-not (Test-Path -LiteralPath $binarySource -PathType Leaf)) { Write-Error-Exit "Downloaded package does not contain $binaryName" } Unblock-File -LiteralPath $binarySource } else { $binarySource = $LocalBinary } $binarySource = (Resolve-Path -LiteralPath $binarySource).Path # VP_ENV_FORBIDDEN=1 (exact value only) stops Vite+ from managing Node.js # and every package manager. if ($env:VP_ENV_FORBIDDEN -ceq '1') { # Binaries without VP_ENV_FORBIDDEN support would ignore it and download managed tools. if (-not (Test-EnvForbiddenSupport -BinarySource $binarySource)) { Write-Error-Exit "vite-plus $ViteVersion does not support VP_ENV_FORBIDDEN. Choose a version that supports it, or unset VP_ENV_FORBIDDEN." } Invoke-InstallHandoff -BinarySource $binarySource } elseif (Test-SelfSetupSupport -BinarySource $binarySource) { Invoke-InstallHandoff -BinarySource $binarySource } else { Invoke-LegacyInstaller -BinarySource $binarySource } } finally { if ($platformTempExtract) { Remove-Item -LiteralPath $platformTempExtract -Recurse -Force -ErrorAction SilentlyContinue } } } function Test-SelfSetupSupport { param([string]$BinarySource) $previous = $env:VP_SELF_SETUP_SUPPORT_CHECK try { $env:VP_SELF_SETUP_SUPPORT_CHECK = '1' # Old binaries must exit with help rather than opening an interactive picker. $response = & $BinarySource --help 2>$null return $LASTEXITCODE -eq 0 -and @($response).Count -eq 1 -and $response -ceq 'vite-plus-self-setup-v1' } catch { return $false } finally { $env:VP_SELF_SETUP_SUPPORT_CHECK = $previous } } function Test-EnvForbiddenSupport { param([string]$BinarySource) $previous = $env:VP_SELF_SETUP_SUPPORT_CHECK try { $env:VP_SELF_SETUP_SUPPORT_CHECK = 'env-forbidden' $response = @(& $BinarySource --help 2>$null) return $LASTEXITCODE -eq 0 -and $response.Count -eq 2 -and $response[0] -ceq 'vite-plus-self-setup-v1' -and $response[1] -ceq 'vite-plus-self-setup-env-forbidden' } catch { return $false } finally { $env:VP_SELF_SETUP_SUPPORT_CHECK = $previous } } function Invoke-LegacyInstaller { param([string]$BinarySource) $global:LASTEXITCODE = 0 $legacyScript = if ($InstallerDirectory) { Join-Path $InstallerDirectory 'install-legacy.ps1' } if ($legacyScript -and (Test-Path -LiteralPath $legacyScript -PathType Leaf)) { . $legacyScript -BinarySource $BinarySource -ResolvedVersion $ViteVersion -PreviewRef $PrVersion } else { $response = Invoke-WebRequest -Uri $LegacyInstallerUrl -UseBasicParsing $content = if ($response.Content -is [byte[]]) { [Text.Encoding]::UTF8.GetString($response.Content) } else { $response.Content } . ([scriptblock]::Create($content)) -BinarySource $BinarySource -ResolvedVersion $ViteVersion -PreviewRef $PrVersion } # A child script's exit only returns to this bootstrap, so forward its failure. if ($LASTEXITCODE -ne 0) { Exit-Installer -Code $LASTEXITCODE } } function Invoke-InstallHandoff { param([string]$BinarySource) $previous = $env:VP_SELF_SETUP_SUPPORT_CHECK $previousShell = $env:VP_SELF_SETUP_SHELL $previousVpShell = $env:VP_SHELL $previousRegistry = $env:NPM_CONFIG_REGISTRY try { Remove-Item Env:VP_SELF_SETUP_SUPPORT_CHECK -ErrorAction SilentlyContinue $env:VP_SELF_SETUP_SHELL = 'powershell' if (-not $env:VP_SHELL) { $env:VP_SHELL = 'powershell' } # Preview dependencies must use the same registry as the downloaded binary. if ($PrVersion) { $env:NPM_CONFIG_REGISTRY = $BridgeRegistry } $result = & $BinarySource if ($LASTEXITCODE -ne 0) { Exit-Installer -Code $LASTEXITCODE } Invoke-Expression ($result -join "`n") # A child can update the user PATH, but this session needs the resolved bin directory too. if (($env:Path -split ';') -notcontains $script:ShimDir) { $env:Path = "$script:ShimDir;$env:Path" } } finally { $env:VP_SELF_SETUP_SHELL = $previousShell $env:VP_SHELL = $previousVpShell $env:NPM_CONFIG_REGISTRY = $previousRegistry $env:VP_SELF_SETUP_SUPPORT_CHECK = $previous } } $previousErrorActionPreference = $ErrorActionPreference try { $ErrorActionPreference = "Stop" Main } catch { if (Test-IsInstallStopException $_) { if (Test-ShouldKeepShellOpenAfterFailure) { return } exit $global:LASTEXITCODE } throw } finally { $ErrorActionPreference = $previousErrorActionPreference }