#!/usr/bin/env pwsh # cruft installer for Windows — https://cruft.sh/install.ps1 # # powershell -c "irm cruft.sh/install.ps1 | iex" # $v="0.0.10"; powershell -c "irm cruft.sh/install.ps1 | iex" # pin a version # # Downloads the prebuilt cruft binary for Windows from the npm registry (the same # artifact `npm i -g cruftjs` uses — the registry is a plain HTTPS host, so this # needs only PowerShell + tar, not node/npm) and installs it to ~\.cruft\bin. # Override the location with $env:CRUFT_INSTALL. Pin a version with $v. $ErrorActionPreference = "Stop" # Version: $v if set, else "latest". Accept "v0.0.10" or "0.0.10". $Version = if ($v) { $v } else { "latest" } $Version = $Version -replace '^v', '' # tar (bsdtar) ships with Windows 10 1803+ and is needed to unpack the .tgz. if (-not (Get-Command tar -ErrorAction SilentlyContinue)) { Write-Host "error: tar is required (Windows 10 1803+ ships it). Please update Windows or install tar." -ForegroundColor Red exit 1 } # Only a win32-x64 build is published. Windows on ARM runs x64 under emulation, # so target win32-x64 there too. $Arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture if ($Arch -eq "Arm64") { Write-Host "note: no native Windows ARM64 build yet; installing the x64 build (runs under emulation)." -ForegroundColor DarkGray } $Target = "win32-x64" $Registry = if ($env:CRUFT_NPM_REGISTRY) { $env:CRUFT_NPM_REGISTRY } else { "https://registry.npmjs.org" } $Pkg = "@cruftless-dev/cruft-$Target" # Resolve the version + tarball URL from the npm registry. try { $Meta = Invoke-RestMethod -Uri "$Registry/$Pkg/$Version" -UseBasicParsing } catch { Write-Host "error: No cruft release found for $Pkg@$Version (check the version, or your network)." -ForegroundColor Red exit 1 } $Tarball = $Meta.dist.tarball $Resolved = $Meta.version if (-not $Tarball) { Write-Host "error: No prebuilt cruft binary found for $Target (version $Version)." -ForegroundColor Red exit 1 } # Install locations. $InstallDir = if ($env:CRUFT_INSTALL) { $env:CRUFT_INSTALL } else { Join-Path $HOME ".cruft" } $BinDir = Join-Path $InstallDir "bin" $Exe = Join-Path $BinDir "cruft.exe" New-Item -ItemType Directory -Force -Path $BinDir | Out-Null # Download + extract into a temp dir. $Tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("cruft-" + [System.Guid]::NewGuid().ToString("N")) New-Item -ItemType Directory -Force -Path $Tmp | Out-Null try { $Tgz = Join-Path $Tmp "cruft.tgz" Write-Host "Downloading cruft $Resolved for $Target..." Invoke-WebRequest -Uri $Tarball -OutFile $Tgz -UseBasicParsing tar -xzf $Tgz -C $Tmp $Src = Join-Path $Tmp "package\cruft.exe" if (-not (Test-Path $Src)) { Write-Host "error: archive did not contain the expected binary (cruft.exe)." -ForegroundColor Red exit 1 } Copy-Item -Path $Src -Destination $Exe -Force } finally { Remove-Item -Recurse -Force $Tmp -ErrorAction SilentlyContinue } Write-Host "cruft $Resolved was installed successfully to $Exe" -ForegroundColor Green # Add the bin dir to the user PATH (persisted), if not already there. $UserPath = [Environment]::GetEnvironmentVariable("Path", "User") if ($UserPath -notlike "*$BinDir*") { $NewPath = if ([string]::IsNullOrEmpty($UserPath)) { $BinDir } else { "$BinDir;$UserPath" } [Environment]::SetEnvironmentVariable("Path", $NewPath, "User") # Reflect it in the current session too. $env:Path = "$BinDir;$env:Path" Write-Host "Added $BinDir to your user PATH." -ForegroundColor DarkGray } # Set CRUFT_INSTALL for the user so it matches the unix installer's convention. [Environment]::SetEnvironmentVariable("CRUFT_INSTALL", $InstallDir, "User") Write-Host "" Write-Host "To get started, open a new terminal and run:" Write-Host " cruft --help"