Files
zero/scripts/install.ps1
Kevin Codex 530563a0bb feat(npm): publish as @twigpine/zero (#1142)
* feat(npm): publish as @twigpine/zero

The npm package moves from @gitlawb/zero to @twigpine/zero, and everything
that finds Zero's GitHub repository by name now names Twigpine/zero.

Package name
- package.json, the lockfile and release-please use @twigpine/zero.
  `repository` names Twigpine/zero: npm checks provenance against it, so a
  publish from this repo with the old URL fails with E422. The homepage is
  zero.twigpine.com.
- The name lives in package.json only. build-platform-packages.mjs names the
  wrapper, platform versions, alias names and alias targets from it, and
  bin/zero.js reads its own package.json for the alias it resolves and for
  every reinstall hint. The Go constant npmPackageName is held equal to
  package.json by a test.

Repository defaults
- The update check (DefaultRepository), install.sh, install.ps1, the npm
  wrapper's fallback downloader, and the GitHub Action's zero-repo default
  all used Gitlawb/zero. That name now belongs to an unrelated GitHub org, and
  these paths download and run what they find there, so they point at
  Twigpine/zero. Docs, README and issue-template links follow. The Go module
  path (and so `go install github.com/Gitlawb/zero/...`) is unchanged here.

Publishing
- The two inline publish steps become scripts/npm/publish-packages.sh, which
  reads the name from the assembled payloads, refuses to start when the
  wrapper's aliases do not match the platform payloads, skips versions
  already on the registry (so re-running a half-finished job is safe), and
  publishes with --ignore-scripts. The dist-tag rules are unchanged.
  internal/npmwrapper/publish_test.go runs it against a fake registry.

Docs explain the switch for existing installs: uninstall @gitlawb/zero first,
because both packages own the `zero` bin and npm refuses the second (EEXIST).

* feat(npm): publish 0.10.0 once more as @gitlawb/zero to point users at the new name

Deprecating @gitlawb/zero alone would strand every existing npm install.
0.9.0's `zero upgrade` runs `npm install -g @gitlawb/zero@latest`; with
nothing newer under that name npm installs nothing, prints no deprecation
warning (it only warns when it installs something), and Zero then reports
"updated via npm to 0.10.0" while staying on 0.9.0.

So this release goes out once more under the old name, as a bridge:

- The publish job assembles the same verified artifacts a second time with
  the new `build-platform-packages.mjs --package-name @gitlawb/zero` and
  publishes them with publish-packages.sh, after the @twigpine/zero publish.
  A gate runs it only while @gitlawb/zero's latest is still 0.9.0 (or is
  this version, on a re-run), so it happens once.
- The build knows its name from the package.json beside it. The Go side
  reports InstallMethodNpmLegacy: `zero upgrade` refuses with the switch
  steps before any release check (so it works offline too), and
  `zero update --check` adds them whether or not an update is out.
- bin/zero.js prints the same steps after zero exits (the TUI would hide a
  notice printed first), only when stderr is a terminal, and not for
  `update`/`upgrade`, which print it themselves.
- Updating from 0.9.0 now installs a real version, so npm also shows the
  deprecation message.

The steps say to uninstall @gitlawb/zero before installing @twigpine/zero:
both own the `zero` bin, npm refuses the second install with EEXIST, and a
forced install loses the bin when the old package is later removed (checked
against npm 11.16).

docs/NPM_PACKAGING.md has the maintainer runbook: bootstrap and trust the new
package, retarget the old one's trusted publisher for the bridge, then
deprecate @gitlawb/zero, revoke its publisher, and delete the bridge steps.

* fix(npm): only skip a publish when npm says the version is not there

publish-packages.sh treated any failed `npm view <name>@<version>` as "not
published". A transient network, auth or registry error on the re-run that the
skip exists for therefore published a version that was already out, and npm
rejected it with EPUBLISHCONFLICT; with the registry unreachable it published
blind. npm 11 reports every kind of missing (no such package, no such version,
including platform versions) as E404, so only E404 now means "not published".
Any other failure is retried three times and then stops the job before it
publishes anything, with npm's own error shown.

The test fake now answers a missing version with E404 the way npm does (it
returned an empty success before), and can fail lookups with a network error.
The two new tests fail on the old lookup: one with EPUBLISHCONFLICT after
transient failures, the other by publishing while the registry is unreachable.

Also renames the unused retry variable in the legacy-name gate step to `_`
(shellcheck SC2034), matching the script.
2026-10-15 11:05:51 +08:00

185 lines
5.7 KiB
PowerShell

[CmdletBinding()]
param(
[string]$Version = $env:ZERO_VERSION,
[string]$Repository = $(if ($env:ZERO_REPO) { $env:ZERO_REPO } else { "Twigpine/zero" }),
[string]$InstallDir = $env:ZERO_INSTALL_DIR,
[string]$GitHubApi = $(if ($env:ZERO_GITHUB_API) { $env:ZERO_GITHUB_API } else { "https://api.github.com" }),
[string]$GitHubBaseUrl = $(if ($env:ZERO_GITHUB_BASE_URL) { $env:ZERO_GITHUB_BASE_URL } else { "https://github.com" })
)
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrWhiteSpace($Version)) {
$Version = "latest"
}
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
$InstallDir = Join-Path $env:LOCALAPPDATA "zero\bin"
}
function Get-ZeroLatestTag {
param([string]$Repository, [string]$GitHubApi)
$apiBase = $GitHubApi.TrimEnd([char[]]"/")
$release = Invoke-RestMethod `
-Uri "$apiBase/repos/$Repository/releases/latest" `
-Headers @{ Accept = "application/vnd.github+json" } `
-TimeoutSec 15
if ([string]::IsNullOrWhiteSpace($release.tag_name)) {
throw "GitHub release response did not include tag_name"
}
return [string]$release.tag_name
}
function Get-ZeroArch {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
switch ($arch) {
"X64" { return "x64" }
"Arm64" { return "arm64" }
default { throw "Unsupported architecture: $arch" }
}
}
function Find-ZeroExtractedFile {
param(
[string]$Root,
[string]$FileName
)
$candidate = Join-Path $Root $FileName
if (Test-Path $candidate -PathType Leaf) {
return $candidate
}
$matches = @(Get-ChildItem -Path $Root -Filter $FileName -File -Recurse)
if ($matches.Count -eq 1) {
return $matches[0].FullName
}
throw "Release archive did not contain exactly one $FileName"
}
function Test-ZeroPathContainsDir {
param(
[string]$PathValue,
[string]$Dir
)
if ([string]::IsNullOrEmpty($PathValue)) {
return $false
}
return @($PathValue -split [System.IO.Path]::PathSeparator) -contains $Dir
}
function Find-ZeroOptionalExtractedDirectory {
param(
[string]$Root,
[string]$DirectoryName
)
$candidate = Join-Path $Root $DirectoryName
if (Test-Path $candidate -PathType Container) {
return $candidate
}
$matches = @(
Get-ChildItem -Path $Root -Directory | ForEach-Object {
$child = Join-Path $_.FullName $DirectoryName
if (Test-Path $child -PathType Container) {
Get-Item $child
}
}
)
if ($matches.Count -eq 0) {
return $null
}
if ($matches.Count -eq 1) {
return $matches[0].FullName
}
throw "Release archive contained multiple $DirectoryName directories"
}
if ($Version -eq "latest") {
$tag = Get-ZeroLatestTag -Repository $Repository -GitHubApi $GitHubApi
} elseif ($Version.StartsWith("v")) {
$tag = $Version
} else {
$tag = "v$Version"
}
$releaseVersion = $tag -replace "^v", ""
$arch = Get-ZeroArch
$archiveName = "zero-v$releaseVersion-windows-$arch.zip"
$checksumName = "$archiveName.sha256"
$releaseBase = $GitHubBaseUrl.TrimEnd([char[]]"/")
$releaseUrl = "$releaseBase/$Repository/releases/download/$tag"
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("zero-install-" + [System.Guid]::NewGuid().ToString("N"))
$extractDir = Join-Path $tempDir "extract"
$archivePath = Join-Path $tempDir $archiveName
$checksumPath = Join-Path $tempDir $checksumName
try {
New-Item -ItemType Directory -Path $tempDir, $extractDir -Force | Out-Null
Write-Host "Installing Zero $tag for windows-$arch"
Invoke-WebRequest -Uri "$releaseUrl/$archiveName" -OutFile $archivePath -UseBasicParsing -TimeoutSec 300
Invoke-WebRequest -Uri "$releaseUrl/$checksumName" -OutFile $checksumPath -UseBasicParsing -TimeoutSec 300
$checksumLine = Get-Content -Path $checksumPath -TotalCount 1
$expectedChecksum = ($checksumLine -split "\s+")[0].ToLowerInvariant()
$actualChecksum = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($expectedChecksum -ne $actualChecksum) {
throw "Checksum mismatch for $archiveName"
}
Expand-Archive -Path $archivePath -DestinationPath $extractDir -Force
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
$requiredFiles = @(
"zero.exe",
"zero-windows-command-runner.exe",
"zero-windows-sandbox-setup.exe"
)
foreach ($fileName in $requiredFiles) {
$sourcePath = Find-ZeroExtractedFile -Root $extractDir -FileName $fileName
Copy-Item -Path $sourcePath -Destination (Join-Path $InstallDir $fileName) -Force
}
$helpersPath = Find-ZeroOptionalExtractedDirectory -Root $extractDir -DirectoryName "helpers"
if ($null -ne $helpersPath) {
$targetHelpersPath = Join-Path $InstallDir "helpers"
if (Test-Path $targetHelpersPath) {
Remove-Item -Path $targetHelpersPath -Recurse -Force
}
Copy-Item -Path $helpersPath -Destination $targetHelpersPath -Recurse -Force
}
$targetPath = Join-Path $InstallDir "zero.exe"
Write-Host "Installed $targetPath"
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if (-not (Test-ZeroPathContainsDir -PathValue $userPath -Dir $InstallDir)) {
try {
$newUserPath = if ([string]::IsNullOrEmpty($userPath)) { $InstallDir } else { "$userPath;$InstallDir" }
[Environment]::SetEnvironmentVariable("PATH", $newUserPath, "User")
Write-Host "Added $InstallDir to your user PATH. Restart your terminal to use 'zero'."
} catch {
Write-Warning "Could not update your user PATH automatically: $_"
Write-Warning "Add $InstallDir to PATH manually to run zero from any directory."
}
}
if (-not (Test-ZeroPathContainsDir -PathValue $env:PATH -Dir $InstallDir)) {
$env:PATH = "$env:PATH;$InstallDir"
}
} finally {
if (Test-Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force
}
}