should-upgrade.sh 1.1 KB

123456789101112131415161718192021222324
  1. #!/usr/bin/env bash
  2. # 版本比较:判断 latest 是否严格高于 current(semver 语义)
  3. # 输出:1(应升级)/ 0(无需升级)
  4. #
  5. # Version comparison: whether latest is strictly newer than current (semver semantics)
  6. # Output: 1 (should upgrade) / 0 (no upgrade needed)
  7. # Usage: should-upgrade.sh <current> <latest>
  8. set -euo pipefail
  9. CURRENT="$1"
  10. LATEST="$2"
  11. [ "$CURRENT" = "$LATEST" ] && { echo 0; exit 0; }
  12. # npx semver 无参时按优先级升序打印;latest 排最后则严格更新
  13. # simp: 借 npm 生态 semver 做纯排序,不用 range。>current 默认只匹配同元组
  14. # prerelease(0.1.1-rc.2 不满足 >0.1.0-rc.7),正是自动升级卡住的原因
  15. #
  16. # npx semver with no flags prints versions in ascending precedence; latest is
  17. # strictly newer if it sorts last. simp: reuse npm semver as a pure sort, not a
  18. # range. >current by default only matches same-tuple prereleases (0.1.1-rc.2
  19. # does not satisfy >0.1.0-rc.7 — why auto-upgrade stalled)
  20. HIGHER="$(npx --yes semver@7 "$CURRENT" "$LATEST" 2>/dev/null | tail -n1 || true)"
  21. [ "$HIGHER" = "$LATEST" ] && echo 1 || echo 0