forceUpdate.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import type { ForceUpdateConfig } from "./coding-plan-subscription.js";
  2. export interface ForceUpdateRequirement {
  3. currentVersion: string;
  4. minimalVersion: string;
  5. }
  6. interface ParsedSemver {
  7. major: number;
  8. minor: number;
  9. patch: number;
  10. prerelease: string[];
  11. }
  12. function parseSemver(version: string): ParsedSemver | null {
  13. const normalized = version.trim().replace(/^v/i, "");
  14. // 远端配置可能只传主版本号(如 "45")或主次版本号(如 "4.5"),
  15. // 严格 semver(X.Y.Z)解析会返回 null,导致 resolveForceUpdateRequirement
  16. // 永远不触发。这里依次尝试标准版本和补齐版本,兼容服务端简写格式。
  17. const strictMatch = normalized.match(
  18. /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/,
  19. );
  20. if (strictMatch) {
  21. return {
  22. major: Number(strictMatch[1]!),
  23. minor: Number(strictMatch[2]!),
  24. patch: Number(strictMatch[3]!),
  25. prerelease: strictMatch[4]?.split(".") ?? [],
  26. };
  27. }
  28. // 补全:只有主版本号("45" → "45.0.0")
  29. const majorOnly = normalized.match(/^(\d+)$/);
  30. if (majorOnly) {
  31. return {
  32. major: Number(majorOnly[1]!),
  33. minor: 0,
  34. patch: 0,
  35. prerelease: [],
  36. };
  37. }
  38. // 补全:主版本号.次版本号("4.5" → "4.5.0")
  39. const majorMinor = normalized.match(/^(\d+)\.(\d+)$/);
  40. if (majorMinor) {
  41. return {
  42. major: Number(majorMinor[1]!),
  43. minor: Number(majorMinor[2]!),
  44. patch: 0,
  45. prerelease: [],
  46. };
  47. }
  48. return null;
  49. }
  50. function comparePrerelease(left: string[], right: string[]): number {
  51. if (left.length === 0 && right.length === 0) {
  52. return 0;
  53. }
  54. if (left.length === 0) {
  55. return 1;
  56. }
  57. if (right.length === 0) {
  58. return -1;
  59. }
  60. const length = Math.max(left.length, right.length);
  61. for (let index = 0; index < length; index += 1) {
  62. const leftPart = left[index];
  63. const rightPart = right[index];
  64. if (leftPart === undefined) {
  65. return -1;
  66. }
  67. if (rightPart === undefined) {
  68. return 1;
  69. }
  70. const leftNumeric = /^\d+$/.test(leftPart);
  71. const rightNumeric = /^\d+$/.test(rightPart);
  72. if (leftNumeric && rightNumeric) {
  73. const delta = Number(leftPart) - Number(rightPart);
  74. if (delta !== 0) {
  75. return delta > 0 ? 1 : -1;
  76. }
  77. continue;
  78. }
  79. if (leftNumeric !== rightNumeric) {
  80. return leftNumeric ? -1 : 1;
  81. }
  82. const delta = leftPart.localeCompare(rightPart);
  83. if (delta !== 0) {
  84. return delta > 0 ? 1 : -1;
  85. }
  86. }
  87. return 0;
  88. }
  89. export function compareSemverVersions(leftVersion: string, rightVersion: string): number | null {
  90. const left = parseSemver(leftVersion);
  91. const right = parseSemver(rightVersion);
  92. if (!left || !right) {
  93. return null;
  94. }
  95. for (const key of ["major", "minor", "patch"] as const) {
  96. const delta = left[key] - right[key];
  97. if (delta !== 0) {
  98. return delta > 0 ? 1 : -1;
  99. }
  100. }
  101. return comparePrerelease(left.prerelease, right.prerelease);
  102. }
  103. export function resolveForceUpdateRequirement(params: {
  104. currentVersion: string;
  105. forceUpdate?: ForceUpdateConfig | null;
  106. }): ForceUpdateRequirement | null {
  107. const minimalVersion = params.forceUpdate?.minimalVersion.trim();
  108. if (!minimalVersion) {
  109. return null;
  110. }
  111. const comparison = compareSemverVersions(params.currentVersion, minimalVersion);
  112. if (comparison === null || comparison >= 0) {
  113. return null;
  114. }
  115. return {
  116. currentVersion: params.currentVersion,
  117. minimalVersion,
  118. };
  119. }