Files
skillshare/internal/server/handler_version.go
T
Willie 11add19544 feat(ui): note how Nix updates the CLI in the update dialog (#771)
* feat(ui): show the install's own upgrade command in the update dialog

The update dialog always offered `skillshare upgrade` for the terminal.
On Nix that command only prints advice, and Homebrew users are better
served by `brew upgrade skillshare`, so the right command appeared only
after a click on Update returned an error.

/api/version now detects the install method, reports its command as
upgradeCommand, and looks up the latest version the way the CLI update
notice does for that method. The dialog shows that command, or the
existing "update your Nix configuration and rebuild" text when a Nix
configuration provides skillshare, so no new translations are needed.

* fix(ui): keep `skillshare upgrade` in the update dialog, add only a Nix note

Review of the previous commit found three regressions:

- `skillshare upgrade` already runs `brew upgrade` on Homebrew and also
  updates the built-in skill. Showing `brew upgrade skillshare` (or the
  nix profile command) instead broke the case where only the skill is
  outdated: the shown command updates nothing.
- /api/version looked up the latest version through `brew info` on
  Homebrew, inside the request with no timeout. Homebrew's auto-update
  could stall the dialog, and a server without brew on PATH never showed
  an update.
- The rendered dialog tests only checked shown text, which
  wiki/frontend.md says to leave to screenshots.

The dialog keeps `skillshare upgrade` and the GitHub lookup. /api/version
reports nixUpgradeCommand only for Nix installs, and when a newer CLI is
pending the dialog adds the existing Nix advice under the command. The
decision lives in nixUpdateNote, unit-tested in upgradeError.test.ts.
2026-11-04 16:51:03 +08:00

74 lines
2.2 KiB
Go

package server
import (
"net/http"
"skillshare/internal/config"
versioncheck "skillshare/internal/version"
)
func (s *Server) handleVersionCheck(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
projectRoot := s.projectRoot
s.mu.RUnlock()
isProjectMode := projectRoot != ""
cliVersion := versioncheck.Version
method := detectUIInstallMethod()
cliUpdateAvailable := false
var cliLatest *string
cliDevMode := cliVersion == "" || cliVersion == "dev"
// Dev builds expose a simulated update so the browser update/restart flow can be tested.
if cliDevMode {
latest := "dev-ui-flow"
cliUpdateAvailable = true
cliLatest = &latest
} else if result := versioncheck.Check(cliVersion, versioncheck.InstallDirect); result != nil {
// CLI version check (uses 24h cache)
cliUpdateAvailable = result.UpdateAvailable
cliLatest = &result.LatestVersion
}
// `skillshare upgrade` updates both everywhere but leaves a Nix-store CLI
// to Nix, so the dialog also says how Nix updates it.
var nixUpgradeCommand *string
if method.IsNix() {
command := method.UpgradeCommand()
nixUpgradeCommand = &command
}
// Skill version (local) — always check global source for built-in skill
skillSourceDir := source
if isProjectMode {
if globalCfg, err := config.Load(); err == nil {
skillSourceDir = globalCfg.Source
}
}
skillVersion := versioncheck.ReadLocalSkillVersion(skillSourceDir)
// Skill version (remote) — uses 24h cache, network call with 3s timeout
var skillLatest *string
skillUpdateAvailable := false
if skillVersion != "" {
if remote := versioncheck.CachedRemoteSkillVersion(); remote != "" {
skillLatest = &remote
skillUpdateAvailable = versioncheck.SkillOutdated(skillVersion, remote)
}
}
writeJSON(w, map[string]any{
"cliVersion": cliVersion,
"cliLatest": cliLatest,
"cliUpdateAvailable": cliUpdateAvailable,
"cliDevMode": cliDevMode,
"nixUpgradeCommand": nixUpgradeCommand,
"skillVersion": skillVersion,
"skillLatest": skillLatest,
"skillUpdateAvailable": skillUpdateAvailable,
})
}