Skip to content

Commit 08ac5a1

Browse files
authored
Merge pull request #900 from rondonjon/fix/899
Fix 'Error fetching Function Core Tools releases: Cannot find downloa…
2 parents c24e67d + 7f0990c commit 08ac5a1

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/core/func-core-tools.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,35 @@ function getPlatform() {
121121

122122
export async function getLatestCoreToolsRelease(targetVersion: number): Promise<CoreToolsRelease> {
123123
try {
124-
const response = await fetch(RELEASES_FEED_URL);
125-
const feed = (await response.json()) as { releases: any; tags: any };
126-
const tag = feed.tags[`v${targetVersion}`];
127-
if (!tag || tag.hidden) {
128-
throw new Error(`Cannot find the latest version for v${targetVersion}`);
129-
}
130-
131-
const release = feed.releases[tag.release];
132-
if (!release) {
133-
throw new Error(`Cannot find release for ${tag.release}`);
134-
}
135-
136-
const coreTools = release.coreTools.filter((t: CoreToolsZipInfo) => t.size === "full");
137-
const platform = getPlatform();
138-
const info = coreTools.find((t: CoreToolsZipInfo) => t.OS === platform);
139-
if (!info) {
140-
throw new Error(`Cannot find download package for ${platform}`);
141-
}
142-
143-
return {
144-
version: tag.release,
145-
url: info.downloadLink,
146-
sha2: info.sha2,
147-
};
148-
} catch (error: unknown) {
124+
const response = await fetch(RELEASES_FEED_URL);
125+
const feed = (await response.json()) as { releases: Record<string, Record<string, CoreToolsZipInfo[]>>; };
126+
const platform = getPlatform();
127+
128+
const matchingVersions = Object.keys(feed.releases)
129+
.reverse() // JSON has newest versions first; we want the latest first; potential improvement: sort by semver
130+
.filter(
131+
(version) =>
132+
version.match(/^\d+\.\d+\.\d+$/) && // only stable versions
133+
version.startsWith(`${targetVersion}.`), // only matching major versions
134+
);
135+
136+
for (const version of matchingVersions) {
137+
const matchingDistribution = feed.releases[version].coreTools?.find(
138+
(dist) =>
139+
dist.OS === platform && // Distribution for matching platform
140+
dist.size === "full", // exclude minified releases
141+
);
142+
if (matchingDistribution) {
143+
return {
144+
version,
145+
url: matchingDistribution.downloadLink,
146+
sha2: matchingDistribution.sha2,
147+
};
148+
}
149+
}
150+
151+
throw new Error(`Cannot find download package for ${platform}`);
152+
} catch (error: unknown) {
149153
throw new Error(`Error fetching Function Core Tools releases: ${(error as Error).message}`);
150154
}
151155
}

0 commit comments

Comments
 (0)