Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify download link logic #953

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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