$ErrorActionPreference = 'Stop' $scribeSiteUrl = 'https://scribe.happenings.social' $scribeCliHome = if ($env:SCRIBE_CLI_HOME) { $env:SCRIBE_CLI_HOME } else { Join-Path $env:LOCALAPPDATA 'ScribeCLI' } $installDir = Join-Path $scribeCliHome 'current' $archiveUrl = if ($env:SCRIBE_CLI_ARCHIVE_URL) { $env:SCRIBE_CLI_ARCHIVE_URL } else { "https://scribe.happenings.social/api/cli/archive" } function Fail([string]$Message) { throw $Message } function Need-Command([string]$Name) { if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) { Fail "Missing required dependency: $Name" } } function Get-BinDir { if ($env:SCRIBE_CLI_BIN_DIR) { return $env:SCRIBE_CLI_BIN_DIR } return (Join-Path $env:USERPROFILE '.local\bin') } function Add-BinDirToPath([string]$BinDir) { # Read and write the raw registry value. [Environment]::SetEnvironmentVariable # would save the expanded PATH as REG_SZ, flattening %USERPROFILE%-style entries. $key = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey('Environment') try { $currentUserPath = [string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $entries = @($currentUserPath.Split(';') | Where-Object { $_ }) if ($entries | Where-Object { [Environment]::ExpandEnvironmentVariables($_) -eq $BinDir }) { return $false } $key.SetValue('Path', (@($entries + $BinDir) -join ';'), [Microsoft.Win32.RegistryValueKind]::ExpandString) } finally { $key.Close() } # Setting any user variable broadcasts WM_SETTINGCHANGE, so new terminals see the PATH. [Environment]::SetEnvironmentVariable('SCRIBE_CLI_PATH_REFRESH', '1', 'User') [Environment]::SetEnvironmentVariable('SCRIBE_CLI_PATH_REFRESH', $null, 'User') return $true } Need-Command node Need-Command npm Need-Command Expand-Archive # The CLI runs through `node --import` (tsx), which needs Node.js 20+. $nodeMajor = [int]((& node -p "process.versions.node.split('.')[0]") | Select-Object -First 1) if ($nodeMajor -lt 20) { Fail ("Scribe CLI needs Node.js 20 or newer (found " + (& node --version) + "). Install the current LTS from https://nodejs.org and run this again.") } $tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString()) $null = New-Item -ItemType Directory -Path $tmpDir -Force try { Write-Host "Installing Scribe CLI from $archiveUrl" $archivePath = Join-Path $tmpDir 'scribe-cli.zip' Invoke-WebRequest -UseBasicParsing -Uri $archiveUrl -OutFile $archivePath Expand-Archive -Path $archivePath -DestinationPath $tmpDir -Force $sourceRoot = Join-Path $tmpDir 'scribe-cli' if (-not (Test-Path $sourceRoot)) { Fail 'Failed to unpack the Scribe CLI archive' } $webDir = Join-Path $sourceRoot 'web' if (-not (Test-Path (Join-Path $webDir 'package.json'))) { Fail 'Downloaded archive is missing web/package.json' } Write-Host 'Installing CLI runtime dependencies...' Push-Location $webDir try { npm install --omit=dev --ignore-scripts --no-fund --no-audit | Out-Host } finally { Pop-Location } $null = New-Item -ItemType Directory -Path $scribeCliHome -Force if (Test-Path $installDir) { Remove-Item -Recurse -Force $installDir } Move-Item -Path $sourceRoot -Destination $installDir $binDir = Get-BinDir $null = New-Item -ItemType Directory -Path $binDir -Force $wrapperPath = Join-Path $binDir 'scribe.cmd' $wrapperLines = @( '@echo off', # Parenthesised: in PowerShell the comma binds tighter than +, so a bare # 'a' + $x + 'b', inside @() splits into separate lines of the .cmd file. ('set "SCRIBE_CLI_INSTALL_ROOT=' + $installDir + '"'), ('set "SCRIBE_CLI_INSTALL_SITE_URL=' + $scribeSiteUrl + '"'), ('set "SCRIBE_CLI_HOME=' + $scribeCliHome + '"'), ('set "SCRIBE_CLI_BIN_DIR=' + $binDir + '"'), ('call "' + (Join-Path $installDir 'scribe.cmd') + '" %*') ) Set-Content -Path $wrapperPath -Value ($wrapperLines -join "`r`n") -Encoding ascii $addedToPath = Add-BinDirToPath $binDir if ($env:Path) { $pathEntries = $env:Path.Split(';') | Where-Object { $_ } if ($pathEntries -notcontains $binDir) { $env:Path = "$binDir;$env:Path" } } else { $env:Path = $binDir } Write-Host "Configuring Scribe CLI for $scribeSiteUrl" & $wrapperPath config set-base-url $scribeSiteUrl | Out-Null $configCheckRaw = & $wrapperPath config show | Out-String $configCheck = $configCheckRaw | ConvertFrom-Json if ($configCheck.effectiveBaseUrl -ne $scribeSiteUrl) { Fail 'Installed CLI self-check failed while saving base URL' } Write-Host 'Installing global Scribe skill...' $skillInstallerPath = Join-Path $installDir 'web\bin\install-skill.js' & node $skillInstallerPath | Out-Host if ($LASTEXITCODE -ne 0) { Write-Warning 'Scribe CLI installed, but the global agent skill could not be installed.' $global:LASTEXITCODE = 0 } Write-Host '' Write-Host 'Scribe CLI installed.' Write-Host " Binary: $wrapperPath" Write-Host " App URL: $scribeSiteUrl" Write-Host '' Write-Host 'Next commands:' Write-Host ' scribe auth login' Write-Host ' scribe endpoints' Write-Host ' scribe update' Write-Host '' if ($addedToPath) { Write-Host "Added $binDir to your user PATH. Open a new terminal if 'scribe' is not available immediately." } } finally { if (Test-Path $tmpDir) { Remove-Item -Recurse -Force $tmpDir } }