diff --git a/terraform/releases.js b/terraform/releases.js index 6114e3c40..0d9c7e4e8 100644 --- a/terraform/releases.js +++ b/terraform/releases.js @@ -1,58 +1,41 @@ 'use strict'; async function getDistributables() { - try { - // Fetch the Terraform releases JSON - const response = await fetch( - 'https://releases.hashicorp.com/terraform/index.json', - { - method: 'GET', - headers: { Accept: 'application/json' }, - }, + let response = await fetch( + 'https://releases.hashicorp.com/terraform/index.json', + { headers: { Accept: 'application/json' } }, + ); + if (!response.ok) { + throw new Error( + `Failed to fetch releases: HTTP ${response.status} - ${response.statusText}`, ); + } - // Validate the HTTP response - if (!response.ok) { - throw new Error( - `Failed to fetch releases: HTTP ${response.status} - ${response.statusText}`, - ); - } - - // Parse the JSON response - const releases = await response.json(); - - let all = { - releases: [], - download: '', // Full URI provided in response body - }; - - function getBuildsForVersion(version) { - releases.versions[version].builds.forEach(function (build) { - let r = { - version: build.version, - download: build.url, - // These are generic enough for the autodetect, - // and the per-file logic has proven to get outdated sooner - //os: convert[build.os], - //arch: convert[build.arch], - //channel: 'stable|-rc|-beta|-alpha', - }; - all.releases.push(r); - }); + let releases = await response.json(); + let all = { + releases: [], + download: '', + }; + + let allVersions = Object.keys(releases.versions); + allVersions.reverse(); // Releases are listed chronologically, we want the latest first. + + for (let version of allVersions) { + for (let build of releases.versions[version].builds) { + let r = { + version: build.version, + download: build.url, + // These are generic enough for the autodetect, + // and the per-file logic has proven to get outdated sooner + //os: convert[build.os], + //arch: convert[build.arch], + //channel: 'stable|-rc|-beta|-alpha', + }; + all.releases.push(r); } - - // Releases are listed chronologically, we want the latest first. - const allVersions = Object.keys(releases.versions).reverse(); - - allVersions.forEach(function (version) { - getBuildsForVersion(version); - }); - - return all; - } catch (err) { - console.error('Error fetching Terraform releases:', err.message); - return { releases: [], download: '' }; } + + return all; } module.exports = getDistributables;