Skip to content

Commit 099ecbd

Browse files
committed
fix: only versions with stable builds can be latest
1 parent f5852ed commit 099ecbd

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/utils/versions.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,52 @@ const manifest: Manifest = await fetch("https://piston-meta.mojang.com/mc/game/v
2424
export const LATEST_MC_RELEASE = manifest.latest.release;
2525

2626
interface Project {
27+
project: { id: string; name: string };
2728
versions: Record<string, string[]>;
2829
}
2930

30-
const fetchFillVersions = async (id: string): Promise<string[]> => {
31-
const project: Project = await fetch(`https://fill.papermc.io/v3/projects/${id}`).then((r) => r.json());
31+
const fetchProject = async (id: string): Promise<Project> => {
32+
return fetch(`https://fill.papermc.io/v3/projects/${id}`).then((r) => r.json());
33+
};
34+
35+
interface Build {
36+
id: number;
37+
channel: "ALPHA" | "BETA" | "STABLE" | "RECOMMENDED";
38+
}
39+
40+
const fetchBuilds = async ({ project }: Project, version: string): Promise<Build[]> => {
41+
return fetch(`https://fill.papermc.io/v3/projects/${project.id}/versions/${version}/builds`).then((r) => r.json());
42+
};
43+
44+
const findLatest = async (project: Project): Promise<string> => {
45+
const versions = Object.values(project.versions).flat();
46+
47+
// find the newest version with at least one stable build
48+
for (const version of versions) {
49+
const builds = await fetchBuilds(project, version);
50+
if (builds.some((b) => b.channel === "STABLE")) {
51+
return version;
52+
}
53+
}
3254

33-
return Object.values(project.versions).flat();
55+
return versions[0];
3456
};
3557

36-
const paperVersions = await fetchFillVersions("paper");
58+
const paperProject = await fetchProject("paper");
3759

38-
export const LATEST_PAPER_RELEASE = paperVersions[0];
60+
export const LATEST_PAPER_RELEASE = await findLatest(paperProject);
3961

40-
const velocityVersions = await fetchFillVersions("velocity");
62+
const velocityProject = await fetchProject("velocity");
4163

42-
export const LATEST_VELOCITY_RELEASE = velocityVersions[0];
64+
export const LATEST_VELOCITY_RELEASE = await findLatest(velocityProject);
4365

44-
const foliaVersions = await fetchFillVersions("folia");
66+
const foliaProject = await fetchProject("folia");
4567

46-
export const LATEST_FOLIA_RELEASE = foliaVersions[0];
68+
export const LATEST_FOLIA_RELEASE = await findLatest(foliaProject);
4769

48-
const waterfallVersions = await fetchFillVersions("waterfall");
70+
const waterfallProject = await fetchProject("waterfall");
4971

50-
export const LATEST_WATERFALL_RELEASE = waterfallVersions[0];
72+
export const LATEST_WATERFALL_RELEASE = await findLatest(waterfallProject);
5173

5274
interface Tag {
5375
name: string;

0 commit comments

Comments
 (0)