Skip to content

Commit

Permalink
Support backup GOV.UK Frontend if plugin is uninstalled
Browse files Browse the repository at this point in the history
Adds an option param to `getNunjucksAppEnv()` to append backup view directories
  • Loading branch information
colinrotherham committed Oct 6, 2023
1 parent 9a0f669 commit bb1cebb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
12 changes: 4 additions & 8 deletions lib/errorServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,13 @@ function runErrorServer (error) {
res.setHeader('Content-Type', 'text/html')
res.writeHead(500)

// Get GOV.UK Frontend (internal) views
const govukFrontendNunjucksPaths = [govukFrontendInternal.config.nunjucksPaths].flat()
.map(nunjucksPath => path.join(govukFrontendInternal.baseDir, nunjucksPath))

const fileContentsParts = []

try {
const nunjucksAppEnv = getNunjucksAppEnv([
path.join(__dirname, 'nunjucks'),
...govukFrontendNunjucksPaths
])
const nunjucksAppEnv = getNunjucksAppEnv(
[path.join(__dirname, 'nunjucks')],
govukFrontendInternal // Add GOV.UK Frontend paths to Nunjucks views
)
res.end(nunjucksAppEnv.render('views/error-handling/server-error', {
govukFrontendInternal, // Add GOV.UK Frontend paths to Nunjucks context
...getErrorModel(error)
Expand Down
31 changes: 28 additions & 3 deletions lib/nunjucks/nunjucksConfiguration.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
const path = require('path')
const nunjucks = require('nunjucks')
const { Environment } = require('nunjucks')
const NunjucksLoader = require('./nunjucksLoader')
const { stopWatchingNunjucks } = NunjucksLoader
const { startPerformanceTimer, endPerformanceTimer } = require('../utils/performance')

function getNunjucksAppEnv (appViews) {
return new nunjucks.Environment(new NunjucksLoader(appViews))
/**
* Create Nunjucks environment
*
* Provide an optional GOV.UK Frontend paths object to append
* backup view directories if the plugin version is uninstalled
*
* @param {string[]} appViews
* @param {GOVUKFrontendPaths} [govukFrontend]
* @returns {import('nunjucks').Environment}
*/
function getNunjucksAppEnv (appViews, govukFrontend) {
const nunjucksViews = [...appViews]

if (govukFrontend) {
const { baseDir, config } = govukFrontend

// Combine with backup GOV.UK Frontend views
nunjucksViews.push(...[config.nunjucksPaths].flat()
.map(nunjucksPath => path.join(baseDir, nunjucksPath))
)
}

return new Environment(new NunjucksLoader(nunjucksViews))
}

function expressNunjucks (env, app) {
Expand Down Expand Up @@ -33,3 +54,7 @@ function expressNunjucks (env, app) {
}

module.exports = { NunjucksLoader, getNunjucksAppEnv, expressNunjucks, stopWatchingNunjucks }

/**
* @typedef {import('../govukFrontendPaths').GOVUKFrontendPaths} GOVUKFrontendPaths
*/
5 changes: 4 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ if (config.isDevelopment) {

nunjucksConfig.express = app

// Finds GOV.UK Frontend via `getAppViews()` only if installed
// but uses the internal package as a backup if uninstalled
const nunjucksAppEnv = getNunjucksAppEnv(
plugins.getAppViews([appViewsDir, finalBackupNunjucksDir])
plugins.getAppViews([appViewsDir, finalBackupNunjucksDir]),
govukFrontendInternal
)

expressNunjucks(nunjucksAppEnv, app)
Expand Down

0 comments on commit bb1cebb

Please sign in to comment.