export async function getLatestCoreToolsRelease()

in src/core/func-core-tools.ts [122:155]


export async function getLatestCoreToolsRelease(targetVersion: number): Promise<CoreToolsRelease> {
  try {
    const response = await fetch(RELEASES_FEED_URL);
    const feed = (await response.json()) as { releases: Record<string, Record<string, CoreToolsZipInfo[]>> };
    const platform = getPlatform();

    const matchingVersions = Object.keys(feed.releases)
      .reverse() // JSON has newest versions first; we want the latest first; potential improvement: sort by semver
      .filter(
        (version) =>
          version.match(/^\d+\.\d+\.\d+$/) && // only stable versions
          version.startsWith(`${targetVersion}.`), // only matching major versions
      );

    for (const version of matchingVersions) {
      const matchingDistribution = feed.releases[version].coreTools?.find(
        (dist) =>
          dist.OS === platform && // Distribution for matching platform
          dist.size === "full", // exclude minified releases
      );
      if (matchingDistribution) {
        return {
          version,
          url: matchingDistribution.downloadLink,
          sha2: matchingDistribution.sha2,
        };
      }
    }

    throw new Error(`Cannot find download package for ${platform}`);
  } catch (error: unknown) {
    throw new Error(`Error fetching Function Core Tools releases: ${(error as Error).message}`);
  }
}