Skip to content

Commit

Permalink
Merge pull request #953 from alphagov/simplify-download-logic
Browse files Browse the repository at this point in the history
Simplify download link logic
  • Loading branch information
36degrees authored Nov 9, 2020
2 parents f9a2821 + 26f6746 commit 36209e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
23 changes: 17 additions & 6 deletions docs/documentation_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const express = require('express')
const marked = require('marked')
const router = express.Router()

// Local dependencies
const utils = require('../lib/utils.js')

// Page routes

// Docs index
Expand All @@ -34,10 +31,24 @@ router.get('/install/:page', function (req, res) {
res.render('install_template', { document: html })
})

// Redirect to the zip of the latest release of the Prototype Kit on GitHub
// When in 'promo mode', redirect to download the current release zip from
// GitHub, based on the version number from package.json
//
// Otherwise, redirect to the latest release page on GitHub, to avoid just
// linking to the same version being run by someone referring to the copy of the
// docs running in their kit
router.get('/download', function (req, res) {
var url = utils.getLatestRelease()
res.redirect(url)
if (req.app.locals.promoMode === 'true') {
const version = require('../package.json').version

res.redirect(
`https://github.com/alphagov/govuk-prototype-kit/archive/v${version}.zip`
)
} else {
res.redirect(
'https://github.com/alphagov/govuk-prototype-kit/releases/latest'
)
}
})

// Examples - examples post here
Expand Down
36 changes: 0 additions & 36 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ const marked = require('marked')
const path = require('path')
const portScanner = require('portscanner')
const inquirer = require('inquirer')
const request = require('sync-request')

// Local dependencies
const config = require('../app/config.js')

// Variables
var releaseUrl = null

// Require core and custom filters, merges to one object
// and then add the methods to Nunjucks environment
exports.addNunjucksFilters = function (env) {
Expand Down Expand Up @@ -131,38 +127,6 @@ exports.forceHttps = function (req, res, next) {
next()
}

// Synchronously get the URL for the latest release on GitHub and cache it
exports.getLatestRelease = function () {
if (releaseUrl !== null) {
// Release URL already exists
console.log('Release url cached:', releaseUrl)
return releaseUrl
} else {
// Release URL doesn't exist
try {
console.log('Getting latest release from GitHub')

var res = request(
'GET',
'https://api.github.com/repos/alphagov/govuk-prototype-kit/releases/latest',
{
headers: { 'user-agent': 'node.js' }
}
)
var data = JSON.parse(res.getBody('utf8'))

// Cache releaseUrl before we return it
releaseUrl = `https://github.com/alphagov/govuk-prototype-kit/archive/${data.name}.zip`

console.log('Release URL is', releaseUrl)
return releaseUrl
} catch (err) {
console.log("Couldn't retrieve release URL")
return 'https://github.com/alphagov/govuk-prototype-kit/releases/latest'
}
}
}

// Try to match a request to a template, for example a request for /test
// would look for /app/views/test.html
// and /app/views/test/index.html
Expand Down

0 comments on commit 36209e1

Please sign in to comment.