diff --git a/lib/utils.js b/lib/utils.js index cc6c747daf..8acb28a9fd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,7 @@ var basicAuth = require('basic-auth'), prompt = require('prompt'), portScanner = require('portscanner'), + releaseUrl = null, request = require('sync-request'); /** @@ -55,22 +56,34 @@ exports.matchRoutes = function(req, res) { }; -// Synchronously get the url for the latest release on github +// Synchronously get the url for the latest release on github and store exports.getLatestRelease = function() { + var url = ""; - var options = { - headers: {'user-agent': 'node.js'} - }; - var gitHubUrl = 'https://api.github.com/repos/alphagov/govuk_prototype_kit/releases/latest'; - try { - res = request('GET', gitHubUrl, options); - var data = JSON.parse(res.getBody('utf8')); - url = data.zipball_url; + + // Release url already exists + if (releaseUrl !== null){ + console.log("Release url cached:", releaseUrl); + return releaseUrl; } - catch(err) { - url = "https://github.com/alphagov/govuk_prototype_kit/releases/latest"; + // Release url doesn't exist + else { + var options = { + headers: {'user-agent': 'node.js'} + }; + var gitHubUrl = 'https://api.github.com/repos/alphagov/govuk_prototype_kit/releases/latest'; + try { + res = request('GET', gitHubUrl, options); + var data = JSON.parse(res.getBody('utf8')); + url = data.zipball_url; + releaseUrl = url; + console.log("Release url is:", releaseUrl); + } + catch(err) { + url = "https://github.com/alphagov/govuk_prototype_kit/releases/latest"; + console.log("Couldn't retrieve release url"); + } } - console.log("Release url is", url); return url; };