A music player that connects to your cloud/distributed storage.
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: be mindful of prereleases

+55 -3
+55 -3
src/common/pages/version-upgrade.js
··· 87 87 } 88 88 89 89 /** 90 - * @param {{ version: string, cid: string }[]} artifacts 90 + * @param {Record<string, { version: string, cid: string }>} artifacts 91 + * @param {{ includePrerelease?: boolean }} [options] 91 92 * @returns {{ version: string, cid: string } | null} 93 + * 94 + * @example Returns null for an empty artifact list 95 + * ```js 96 + * import { getLatestArtifact } from "~/common/pages/version-upgrade.js"; 97 + * 98 + * if (getLatestArtifact({}) !== null) throw new Error("empty artifacts should return null"); 99 + * ``` 100 + * 101 + * @example Returns the highest semver artifact 102 + * ```js 103 + * import { getLatestArtifact } from "~/common/pages/version-upgrade.js"; 104 + * 105 + * const artifacts = { 106 + * a: { cid: "a", version: "4.0.0" }, 107 + * b: { cid: "b", version: "4.1.0" }, 108 + * c: { cid: "c", version: "3.9.0" }, 109 + * }; 110 + * if (getLatestArtifact(artifacts)?.cid !== "b") throw new Error("should return highest version"); 111 + * ``` 112 + * 113 + * @example Ignores non-semver versions 114 + * ```js 115 + * import { getLatestArtifact } from "~/common/pages/version-upgrade.js"; 116 + * 117 + * const artifacts = { 118 + * a: { cid: "a", version: "4.0.0" }, 119 + * b: { cid: "b", version: "some-branch" }, 120 + * }; 121 + * if (getLatestArtifact(artifacts)?.cid !== "a") throw new Error("should ignore non-semver versions"); 122 + * ``` 123 + * 124 + * @example Excludes prerelease artifacts when includePrerelease is false 125 + * ```js 126 + * import { getLatestArtifact } from "~/common/pages/version-upgrade.js"; 127 + * 128 + * const artifacts = { 129 + * a: { cid: "a", version: "4.1.0" }, 130 + * b: { cid: "b", version: "4.2.0-nightly.1" }, 131 + * }; 132 + * if (getLatestArtifact(artifacts, { includePrerelease: false })?.cid !== "a") { 133 + * throw new Error("should exclude prerelease artifacts"); 134 + * } 135 + * if (getLatestArtifact(artifacts, { includePrerelease: true })?.cid !== "b") { 136 + * throw new Error("should include prerelease artifacts when opted in"); 137 + * } 138 + * ``` 92 139 */ 93 - function getLatestArtifact(artifacts) { 140 + export function getLatestArtifact(artifacts, { includePrerelease = true } = {}) { 94 141 return Object.values(artifacts).reduce( 95 142 /** @param {{ version: string, cid: string } | null} max */ 96 143 (max, artifact) => { 97 144 if (!canParse(artifact.version)) return max; 145 + if (!includePrerelease && parseSemver(artifact.version).prerelease?.length) return max; 98 146 if (!max) return artifact; 99 147 return greaterThan(parseSemver(artifact.version), parseSemver(max.version)) 100 148 ? artifact ··· 151 199 { with: { type: "json" } } 152 200 ).catch(() => ({ default: {} })); 153 201 154 - const lastArtifact = getLatestArtifact(artifacts); 202 + const currentIsStable = 203 + canParse(versionOrCid) && !parseSemver(versionOrCid).prerelease?.length; 204 + const lastArtifact = getLatestArtifact(artifacts, { 205 + includePrerelease: !currentIsStable, 206 + }); 155 207 const isLatest = checkIsLatest(versionOrCid, lastArtifact); 156 208 157 209 document.querySelectorAll("#status").forEach((status) => {