# Instalador do comando aio (MozOut All in One AI) para Windows. Gerado pelo build das docs. # Uso, no PowerShell: irm https://docs.mozout.com/cli/install.ps1 | iex # Para remover: & ([scriptblock]::Create((irm https://docs.mozout.com/cli/install.ps1))) -Uninstall # Nunca pede direitos de administrador: instala em %LOCALAPPDATA%\aio e junta essa pasta ao PATH do utilizador. # O ficheiro publicado s\u00f3 tem caracteres ASCII: o build troca as letras com acento por \uXXXX, e a fun\u00e7\u00e3o Msg devolve-as. param([switch]$Uninstall) & { param([bool]$Uninstall) $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' $From = 'https://docs.mozout.com/cli' $Version = '1.0.0' $Dir = Join-Path $env:LOCALAPPDATA 'aio' $UsePt = ((Get-Culture).Name -like 'pt*') -or ((Get-UICulture).Name -like 'pt*') function Msg([string]$pt, [string]$en) { if ($UsePt) { return [regex]::Unescape($pt) } else { return $en } } function Say([string]$pt, [string]$en, [object[]]$f = @()) { Write-Host ((Msg $pt $en) -f $f) } function Fail([string]$pt, [string]$en, [object[]]$f = @()) { Write-Host ((Msg 'Erro: ' 'Error: ') + ((Msg $pt $en) -f $f)) -ForegroundColor Red throw 'aio-install-failed' } try { $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if ($null -eq $userPath) { $userPath = '' } if ($Uninstall) { foreach ($n in @('aio.js', 'aio.cmd', 'aio.ps1')) { $p = Join-Path $Dir $n if (Test-Path $p) { Remove-Item -Force $p } } if ((Test-Path $Dir) -and -not (Get-ChildItem $Dir -Force)) { Remove-Item -Force $Dir } $parts = $userPath.Split(';') | Where-Object { $_ -and ($_.TrimEnd('\') -ne $Dir.TrimEnd('\')) } [Environment]::SetEnvironmentVariable('Path', ($parts -join ';'), 'User') Say 'O aio foi removido de {0}.' 'aio was removed from {0}.' @($Dir) Say 'A configura\u00e7\u00e3o com a chave fica em {0}. Pode apagar essa pasta \u00e0 m\u00e3o.' 'Your settings and key stay in {0}. You can delete that folder by hand.' @((Join-Path $env:APPDATA 'aio')) return } Say 'A instalar o comando aio {0} ({1} All in One AI)...' 'Installing the aio command {0} ({1} All in One AI)...' @($Version, 'MozOut') # 1. Node.js 18 ou mais recente $node = Get-Command node -ErrorAction SilentlyContinue if (-not $node) { Fail 'o aio precisa do Node.js 18 ou mais recente, e o Node.js n\u00e3o foi encontrado. Instale-o em https://nodejs.org e volte a correr este comando.' 'aio needs Node.js 18 or later, and Node.js was not found. Install it from https://nodejs.org and run this command again.' } $nodeVersion = (& node --version) -replace '^v', '' $major = 0 [void][int]::TryParse(($nodeVersion -split '\.')[0], [ref]$major) if ($major -lt 18) { Fail 'o aio precisa do Node.js 18 ou mais recente, e este computador tem o {0}. Actualize-o em https://nodejs.org e volte a correr este comando.' 'aio needs Node.js 18 or later, and this computer has {0}. Update it from https://nodejs.org and run this command again.' @($nodeVersion) } # 2. Descarregar por https (TLS 1.2 tamb\u00e9m no Windows PowerShell 5.1) [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $tmp = Join-Path ([IO.Path]::GetTempPath()) ('aio-' + [Guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $tmp | Out-Null try { $js = Join-Path $tmp 'aio.js' $sums = Join-Path $tmp 'SHA256SUMS' try { Invoke-WebRequest -UseBasicParsing -Uri "$From/aio.js" -OutFile $js Invoke-WebRequest -UseBasicParsing -Uri "$From/SHA256SUMS" -OutFile $sums } catch { Fail 'n\u00e3o foi poss\u00edvel descarregar o aio de {0}.' 'could not download aio from {0}.' @($From) } # 3. Conferir o SHA256 $expected = '' foreach ($line in Get-Content $sums) { $c = $line -split '\s+' if ($c.Count -ge 2 -and ($c[1] -eq 'aio.js' -or $c[1] -eq '*aio.js')) { $expected = $c[0].ToLower() } } $actual = (Get-FileHash -Algorithm SHA256 -Path $js).Hash.ToLower() if (-not $expected -or $expected -ne $actual) { Fail 'o SHA256 do ficheiro descarregado n\u00e3o confere. Nada foi instalado. Tente de novo dentro de alguns minutos.' 'the SHA256 of the downloaded file does not match. Nothing was installed. Try again in a few minutes.' } # 4. Instalar em %LOCALAPPDATA%\aio New-Item -ItemType Directory -Force -Path $Dir | Out-Null Copy-Item -Force $js (Join-Path $Dir 'aio.js') Set-Content -Path (Join-Path $Dir 'aio.cmd') -Encoding ASCII -Value "@echo off`r`nnode `"%~dp0aio.js`" %*" } finally { Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue } $inPath = $userPath.Split(';') | Where-Object { $_.TrimEnd('\') -eq $Dir.TrimEnd('\') } if (-not $inPath) { $novo = if ($userPath) { $userPath.TrimEnd(';') + ';' + $Dir } else { $Dir } [Environment]::SetEnvironmentVariable('Path', $novo, 'User') } if (-not (($env:Path -split ';') -contains $Dir)) { $env:Path = $env:Path.TrimEnd(';') + ';' + $Dir } Say 'Pronto: o aio ficou instalado em {0}.' 'Done: aio is installed in {0}.' @($Dir) Say 'Para come\u00e7ar: aio auth login (nos terminais que j\u00e1 estavam abertos, feche-os e abra-os de novo).' 'To start: aio auth login (close and reopen any terminals that were already open).' } catch { if ("$_" -ne 'aio-install-failed') { Write-Host ("$_") -ForegroundColor Red } } } $Uninstall.IsPresent