Skip to content

Commit

Permalink
Get scripts and assets from config
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Aug 30, 2023
1 parent 6403a8d commit 3837bf8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
16 changes: 11 additions & 5 deletions lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { doubleCsrf } = require('csrf-csrf')
const config = require('./config')
const plugins = require('./plugins/plugins')
const { exec } = require('./exec')
const { prototypeAppScripts } = require('./utils')
const { prototypeAppScripts, getScriptsAndAssetsConfig, getInternalGovukFrontendDir } = require('./utils')
const { projectDir, packageDir, appViewsDir } = require('./utils/paths')
const nunjucksConfiguration = require('./nunjucks/nunjucksConfiguration')
const syncChanges = require('./sync-changes')
Expand Down Expand Up @@ -103,7 +103,7 @@ function postPasswordHandler (req, res) {
const password = config.getConfig().password
const submittedPassword = req.body.password
const providedUrl = req.body.returnURL
const processedRedirectUrl = (!providedUrl || providedUrl.startsWith('/manage-prototype/password')) ? '/' : providedUrl
const processedRedirectUrl = (!providedUrl || providedUrl.startsWith(`${contextPath}/password`)) ? '/' : providedUrl

if (submittedPassword === password) {
// see lib/middleware/authentication.js for explanation
Expand All @@ -115,15 +115,21 @@ function postPasswordHandler (req, res) {
})
res.redirect(processedRedirectUrl)
} else {
res.redirect('/manage-prototype/password?error=wrong-password&returnURL=' + encodeURIComponent(processedRedirectUrl))
res.redirect(`${contextPath}/password?error=wrong-password&returnURL=` + encodeURIComponent(processedRedirectUrl))
}
}

function managePluginsMiddleware (req, res, next) {
// ToDo: Retrieve the actual location here from the internal config
const { scripts: frontEndScripts } = getScriptsAndAssetsConfig(getInternalGovukFrontendDir())
const { scripts: kitScripts } = getScriptsAndAssetsConfig(packageDir)
res.locals.managePlugins = {
scripts: [
{ src: '/manage-prototype/dependencies/govuk-frontend/govuk/all.js' }
...frontEndScripts.map((script) => {
return { src: `${contextPath}/dependencies/govuk-frontend${script.src || script}`, type: script.type }
}),
...kitScripts.map((script) => {
return { src: `${contextPath}/dependencies/govuk-prototype-kit${script.src || script}`, type: script.type }
})
]
}
next()
Expand Down
20 changes: 16 additions & 4 deletions lib/manage-prototype-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path')

// npm dependencies
const express = require('express')
const fse = require('fs-extra')

const {
contextPath,
Expand Down Expand Up @@ -32,17 +31,30 @@ const {
postPluginsHandler
} = require('./manage-prototype-handlers')

const { getInternalGovukFrontendDir } = require('./utils')
const { getScriptsAndAssetsConfig } = require('./utils/managePlugins')
const { getInternalGovukFrontendDir, getScriptsAndAssetsConfig } = require('./utils')
const { packageDir } = require('./utils/paths')

const router = require('../index').requests.setupRouter(contextPath)

router.use(managePluginsMiddleware)

getPartialGovukFrontendUrls().forEach(url => {
function getAssetUrls (pluginDir) {
const { scripts, assets } = getScriptsAndAssetsConfig(pluginDir)

return [
...assets,
...scripts.map(script => script.src || script)
]
}

getAssetUrls(getInternalGovukFrontendDir()).forEach(url => {
router.use(`/dependencies/govuk-frontend${url}`, express.static(path.join(getInternalGovukFrontendDir(), url)))
})

getAssetUrls(packageDir).forEach(url => {
router.use(`/dependencies/govuk-prototype-kit${url}`, express.static(path.join(packageDir, url)))
})

router.get('/csrf-token', getCsrfTokenHandler)

// Indicates page has loaded
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,23 @@ function getInternalGovukFrontendDir () {
return internalGovukFrontendDir
}

function getScriptsAndAssetsConfig (pluginDir) {
let {
assets = [],
scripts = []
} = fse.readJsonSync(path.join(pluginDir, 'govuk-prototype-kit.config.json'))

if (!Array.isArray(assets)) {
assets = [assets]
}

if (!Array.isArray(scripts)) {
scripts = [scripts]
}

return { assets, scripts }
}

function sortByObjectKey (key) {
return function (a, b) {
if (a[key] > b[key]) {
Expand Down Expand Up @@ -291,5 +308,6 @@ module.exports = {
searchAndReplaceFiles,
recursiveDirectoryContentsSync,
getInternalGovukFrontendDir,
getScriptsAndAssetsConfig,
sortByObjectKey
}
34 changes: 0 additions & 34 deletions lib/utils/managePlugins.js

This file was deleted.

0 comments on commit 3837bf8

Please sign in to comment.