#Requires -Version 5.1 <# YAGNI Code CLI installer for Windows irm https://yagni.app/install.ps1 | iex Installs @yagni-app/code globally with npm and makes sure the `yagni` command is on PATH. This is the Windows counterpart of install.sh: the same checks, the same "never sudo" rule (npm's global prefix on Windows is per-user already, so no elevated prompt is ever needed), and the same repair when the prefix turns out not to be writable, which happens on managed machines with a company-owned Node install. Safe to re-run. Runs in the current session, so it works under any execution policy; no downloaded script file is ever executed. The `yagni` command npm installs is a .ps1 shim, though, so Complete-Install checks the policy and says what to do when it would refuse to load. #> $ErrorActionPreference = 'Stop' $Package = '@yagni-app/code' $MinNode = [version]'22.19' # Where the prefix moves when the configured one is not writable. npm's bin # dir on Windows is the prefix itself (no `bin\`), so this is also the PATH entry. $UserPrefix = if ($env:YAGNI_NPM_PREFIX) { $env:YAGNI_NPM_PREFIX } else { Join-Path $env:LOCALAPPDATA 'yagni\npm' } function Write-Note([string] $Message) { Write-Host $Message } function Write-Fail([string] $Message) { Write-Host "yagni install: $Message" -ForegroundColor Red } # `npm` on PATH resolves to npm.ps1 first, which a Restricted execution policy # refuses to run even though this installer itself is fine (iex runs in # memory). npm.cmd is what the Node installer puts on PATH and is policy-free. function Find-Npm { foreach ($name in 'npm.cmd', 'npm') { $cmd = Get-Command $name -ErrorAction SilentlyContinue if ($cmd) { return $cmd.Source } } return $null } # "Not installed" and "installed but broken" need different advice, so the # result says which: @{ Found = $false }, or Found with either Version or Error. function Get-NodeVersion { $node = Get-Command node -ErrorAction SilentlyContinue if (-not $node) { return @{ Found = $false } } $previous = $ErrorActionPreference $ErrorActionPreference = 'Continue' try { $output = & $node.Source -p 'process.versions.node' 2>&1 | ForEach-Object { "$_" } $code = $LASTEXITCODE } finally { $ErrorActionPreference = $previous } $raw = ($output -join "`n").Trim() # stderr is merged in, so a warning Node prints on startup would sit in # front of the version (and could itself mention one). The version is the # last line that is nothing but a version, which is what -p prints. A # nightly reports "25.0.0-nightly..."; only the core counts. $line = $raw -split "`r?`n" | Where-Object { $_.Trim() -match '^\d+\.\d+\.\d+(-\S+)?$' } | Select-Object -Last 1 if ($code -ne 0 -or -not $line) { return @{ Found = $true; Path = $node.Source; Error = $raw } } $core = [regex]::Match($line.Trim(), '^\d+\.\d+\.\d+').Value return @{ Found = $true; Path = $node.Source; Version = [version]$core } } # npm writes progress to stderr; with $ErrorActionPreference = 'Stop' a # native command's stderr line would otherwise terminate the script. function Invoke-Npm([string] $Npm, [string[]] $Arguments) { $previous = $ErrorActionPreference $ErrorActionPreference = 'Continue' try { $output = & $Npm @Arguments 2>&1 | ForEach-Object { "$_" } return @{ ExitCode = $LASTEXITCODE; Output = ($output -join "`n") } } finally { $ErrorActionPreference = $previous } } function Test-OnPath([string] $Dir) { $wanted = $Dir.TrimEnd('\') foreach ($entry in ($env:Path -split ';')) { if ($entry -and ($entry.TrimEnd('\') -ieq $wanted)) { return $true } } return $false } # The user PATH, not the machine one: no elevation, and the change follows # the account. The current session is updated too, so `yagni login` works # right away in this window; other windows pick it up when reopened. function Add-ToUserPath([string] $Dir) { $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') $present = $false foreach ($entry in ($userPath -split ';')) { if ($entry -and ($entry.TrimEnd('\') -ieq $Dir.TrimEnd('\'))) { $present = $true } } if (-not $present) { $joined = if ($userPath) { "$($userPath.TrimEnd(';'));$Dir" } else { $Dir } [Environment]::SetEnvironmentVariable('Path', $joined, 'User') Write-Note "Added $Dir to your PATH." } if (-not (Test-OnPath $Dir)) { $env:Path = "$env:Path;$Dir" } } # The policy the next PowerShell window will enforce, and the scope that sets # it. Scopes in precedence order, skipping Process: that scope dies with this # window, so a `-Scope Process Bypass` set to run the installer must not hide # a Restricted default from Complete-Install. With every scope Undefined the # platform default applies, which Get-ExecutionPolicy reports unless Process # is set; then assume the Windows client default, Restricted. function Get-PersistentPolicy { foreach ($scope in 'MachinePolicy', 'UserPolicy', 'CurrentUser', 'LocalMachine') { $policy = "$(Get-ExecutionPolicy -Scope $scope)" if ($policy -ne 'Undefined') { return @{ Policy = $policy; Scope = $scope } } } if ("$(Get-ExecutionPolicy -Scope Process)" -eq 'Undefined') { return @{ Policy = "$(Get-ExecutionPolicy)"; Scope = 'Default' } } return @{ Policy = 'Restricted'; Scope = 'Default' } } # npm writes yagni, yagni.cmd and yagni.ps1 side by side, and PowerShell runs # the .ps1 for a bare `yagni`. Windows client editions ship with the Restricted # execution policy, which refuses every script file (this installer is exempt: # iex runs it in memory), so a fresh machine installs cleanly and then fails # on `yagni login`. AllSigned refuses the shim too, since npm does not sign it. # The policy is the user's to change, never the installer's: say exactly what # to run, or, when Group Policy sets it, point at the .cmd shim instead. function Complete-Install([string] $BinDir, [bool] $OnPath, [hashtable] $Policy) { Write-Note '' Write-Note 'YAGNI Code installed.' $command = 'yagni login' if ($Policy.Policy -in @('Restricted', 'AllSigned')) { Write-Note '' Write-Note "PowerShell's execution policy here is $($Policy.Policy), which blocks the yagni command" Write-Note 'because npm installs it as a .ps1 script.' if ($Policy.Scope -in @('MachinePolicy', 'UserPolicy')) { Write-Note 'Your organization sets that policy, so use yagni.cmd wherever the docs say yagni,' Write-Note 'or ask IT to allow RemoteSigned scripts for your account.' $command = 'yagni.cmd login' } else { Write-Note 'Allow scripts for your own account (no administrator needed):' Write-Note '' Write-Note ' Set-ExecutionPolicy -Scope CurrentUser RemoteSigned' } } Write-Note '' if ($OnPath) { Write-Note 'Next, run:' } else { Write-Note "Open a new terminal (yagni was installed to $BinDir), then run:" } Write-Note '' Write-Note " $command" Write-Note '' } function Install-YagniCode { $npm = Find-Npm if (-not $npm) { Write-Fail "npm not found. Install Node.js $MinNode or later from https://nodejs.org (or 'winget install OpenJS.NodeJS.LTS'), open a new terminal, then re-run this installer." return $false } $node = Get-NodeVersion if (-not $node.Found) { Write-Fail "node not found next to npm. Install Node.js $MinNode or later from https://nodejs.org, open a new terminal, then re-run this installer." return $false } if (-not $node.Version) { if ($node.Error) { Write-Host $node.Error } Write-Fail "node at $($node.Path) did not start (output above). Repair or reinstall Node.js $MinNode or later from https://nodejs.org, then re-run this installer." return $false } if ($node.Version -lt $MinNode) { Write-Fail "Node.js $MinNode or later is required (found $($node.Version)). Update Node, then re-run this installer." return $false } Write-Note "Installing $Package ..." # --loglevel=error / --no-fund / --no-audit keep npm's informational noise # out of the installer's output; real errors still print. $quiet = @('--loglevel=error', '--no-fund', '--no-audit') $first = Invoke-Npm $npm (@('install', '-g') + $quiet + @($Package)) if ($first.ExitCode -eq 0) { $binDir = (Invoke-Npm $npm @('prefix', '-g')).Output.Trim() if ($binDir -and -not (Test-OnPath $binDir)) { Add-ToUserPath $binDir } Complete-Install $binDir ([bool](Get-Command yagni -ErrorAction SilentlyContinue)) (Get-PersistentPolicy) return $true } if ($first.Output -notmatch 'EACCES|EPERM|permission denied|Access is denied') { Write-Host $first.Output Write-Fail 'npm install failed for a reason other than permissions. See the npm output above. A running yagni session can hold files open; close it and re-run.' return $false } Write-Note "npm's global directory is not writable by your user (common on managed machines)." Write-Note "Switching npm to a per-user prefix at $UserPrefix. No administrator prompt needed." New-Item -ItemType Directory -Force -Path $UserPrefix | Out-Null $configured = Invoke-Npm $npm @('config', 'set', 'prefix', $UserPrefix) if ($configured.ExitCode -ne 0) { Write-Host $configured.Output Write-Fail "could not point npm at $UserPrefix. Please send the output above to support@yagni.app." return $false } # No further fallback, so the full npm output is what the user sends to # support if this fails. $second = Invoke-Npm $npm (@('install', '-g') + $quiet + @($Package)) if ($second.ExitCode -ne 0) { Write-Host $second.Output Write-Fail "npm install failed even after switching to the per-user prefix $UserPrefix. Please send the npm output above to support@yagni.app." return $false } if (-not (Test-Path (Join-Path $UserPrefix 'yagni.cmd'))) { Write-Fail "install finished but $UserPrefix\yagni.cmd is missing. Please report this to support@yagni.app with the output above." return $false } Add-ToUserPath $UserPrefix Complete-Install $UserPrefix ([bool](Get-Command yagni -ErrorAction SilentlyContinue)) (Get-PersistentPolicy) return $true } $ok = Install-YagniCode # Under `irm | iex` there is no script file and `exit` would close the user's # shell; a saved copy run as a file still reports failure to its caller. if (-not $ok -and $MyInvocation.MyCommand.Path) { exit 1 }