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

Move default user template to app/views/layouts/main.html #1752

Merged
merged 8 commits into from
Nov 8, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Breaking changes
- [#1752: Move default user template to app/views/layouts/main.html](https://github.com/alphagov/govuk-prototype-kit/pull/1752)
- [#1640: Fixing govuk frontend until they release update](https://github.com/alphagov/govuk-prototype-kit/pull/1640) We have made the plugin framework powerful enough to handle everything govuk-frontend needs but they haven't yet implemented their side of this, this change allows current and previous versions of govuk-frontend to work with the kit. You'll need to import components before you can use them until govuk-frontend release a version which imports them automatically.
- [#1648: Change default session store to cookie store when in production mode](https://github.com/alphagov/govuk-prototype-kit/pull/1648) When hosted online the kit will now preserve user session data between server restarts by default.
- [#1658: Use file store for session data](https://github.com/alphagov/govuk-prototype-kit/pull/1658)
Expand Down
4 changes: 0 additions & 4 deletions __tests__/spec/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ describe('migrate test prototype', () => {
await sleep(500)
})

afterAll(() => {
fse.removeSync(projectDirectory)
})

it('config.js to config.json', () => {
const config = fse.readJsonSync(path.join(appDirectory, 'config.json'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CYAN = 'rgb(0, 255, 255)'
const MAGENTA = 'rgb(255, 0, 255)'

const extensionBazViewMarkup = `
{% extends "layout.html" %}
{% extends "layouts/main.html" %}

{% block content %}
{% include "baz.njk" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const YELLOW = 'rgb(255, 255, 0)'
const BLUE = 'rgb(0, 0, 255)'

const extensionFooBarCombinedViewMarkup = `
{% extends "layout.html" %}
{% extends "layouts/main.html" %}

{% block content %}
{% set testVar="Hello" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const YELLOW = 'rgb(255, 255, 0)'
const BLUE = 'rgb(0, 0, 255)'

const extensionFooBarSeparatedViewMarkup = `
{% extends "layout.html" %}
{% extends "layouts/main.html" %}

{% block content %}
{% include "bar.njk" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const YELLOW = 'rgb(255, 255, 0)'
const BLUE = 'rgb(0, 0, 255)'

const extensionFooViewMarkup = `
{% extends "layout.html" %}
{% extends "layouts/main.html" %}

{% block content %}
{% include "foo.njk" %}
Expand Down
34 changes: 34 additions & 0 deletions cypress/e2e/dev/7-layout-tests/default-layout.cypress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const path = require('path')

const { waitForApplication } = require('../../utils')

const defaultLayoutFilePath = path.join('app', 'views', 'layouts', 'main.html')
const backupLayoutComment = '<!-- could not find layouts/main.html in prototype, using backup default template -->'

const comments = el => cy.wrap(
[...el.childNodes]
.filter(node => node.nodeName === '#comment')
.map(commentNode => '<!--' + commentNode.data + '-->')
)

after(() => {
cy.task('copyFromStarterFiles', { filename: defaultLayoutFilePath })
waitForApplication()
})

specify('deleting default layout does not cause pages to fail to render', () => {
cy.visit('/')

cy.document().then(doc =>
comments(doc.head).should('not.contain', backupLayoutComment)
)

cy.task('deleteFile', { filename: path.join(Cypress.env('projectFolder'), defaultLayoutFilePath) })

cy.visit('/', { failOnStatusCode: false })
cy.get('body').should('not.contains.text', 'Error: template not found')

cy.document().then(doc =>
comments(doc.head).should('contain', backupLayoutComment)
)
})
2 changes: 1 addition & 1 deletion cypress/fixtures/views/larry-the-cat.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layout.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Larry the cat – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
12 changes: 12 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const waitOn = require('wait-on')
const extract = require('extract-zip')
const https = require('https')

const { starterDir } = require('../../lib/path-utils')
const { sleep } = require('../e2e/utils')

const log = (message) => console.log(`${new Date().toLocaleTimeString()} => ${message}`)
Expand Down Expand Up @@ -193,6 +194,17 @@ module.exports = (on, config) => {
.then(() => sleep(2000)) // pause after the copy
.then(makeSureCypressCanInterpretTheResult),

copyFromStarterFiles: ({ starterFilename = undefined, filename }) => {
const src = path.join(starterDir, starterFilename || filename)
const dest = path.join(config.env.projectFolder, filename)
return createFolderForFile(dest)
.then(() => fsp.copyFile(src, dest))
// The sleep of 2 seconds allows for the file to be copied completely to prevent
// it from not existing when the file is needed in a subsequent step
.then(() => sleep(2000)) // pause after the copy
.then(makeSureCypressCanInterpretTheResult)
},

createFile: ({ filename, data, replace = false }) => createFolderForFile(filename)
.then(() => fsp.writeFile(filename, data, {
flag: replace ? 'w' : '' // Flag of w will overwrite
Expand Down
8 changes: 4 additions & 4 deletions lib/migrator/fileHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ const deleteDirectoryIfEmpty = async (partialPath) => {
return false
}

const copyFileFromStarter = async (partialPath) => {
const src = path.join(packageDir, 'prototype-starter', partialPath)
const dest = path.join(projectDir, partialPath)
const copyFileFromStarter = async (starterPath, newPath) => {
const src = path.join(packageDir, 'prototype-starter', starterPath)
const dest = path.join(projectDir, newPath || starterPath)
await verboseLog(`copying from [${src}] to [${dest}]`)
return fs.copyFile(src, dest)
return fse.copy(src, dest)
.then(successOutput)
.catch(async e => {
await log('error caught')
Expand Down
8 changes: 6 additions & 2 deletions lib/migrator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const directoriesToDeleteIfEmpty = [

const filesToUpdateIfUnchanged = [
'app/assets/javascripts/application.js',
'app/filters.js',
'app/views/layout.html'
'app/filters.js'
]

const filesToDeleteIfUnchanged = [
Expand Down Expand Up @@ -84,6 +83,11 @@ const migrate = async () => {
upgradeIfUnchanged([
...filesToUpdateIfUnchanged.map(filePath => ({ filePath, action: 'copyFromKitStarter' })),
...filesToDeleteIfUnchanged.map(filePath => ({ filePath, action: 'delete' }))
]),
upgradeIfUnchanged([
// Special case app/views/layout.html, as it has moved in prototype
// starter files, but we don't want to move for existing users
{ filePath: 'app/views/layout.html', starterFilePath: 'app/views/layouts/main.html', action: 'copyFromKitStarter' }
])
])

Expand Down
2 changes: 1 addition & 1 deletion lib/migrator/migrationSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module.exports.upgradeIfUnchanged = (configs) => Promise.all(configs.map(async c
return
}
try {
await copyFileFromStarter(config.filePath)
await copyFileFromStarter(config.starterFilePath || config.filePath, config.filePath)
if (config.filePath === 'app/views/layout.html') {
await module.exports.upgradeIfUnchanged([
{
Expand Down
8 changes: 8 additions & 0 deletions lib/nunjucks/layouts/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{# this file exists as a backup in case the user has deleted their `app/views/layouts/main.html` template #}

{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}

{% block meta %}
<!-- could not find layouts/main.html in prototype, using backup default template -->
{{ super() }}
{% endblock %}
2 changes: 1 addition & 1 deletion lib/templates/blank-govuk.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
GOV.UK page template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/check-answers.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Check your answers template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/confirmation.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Confirmation page template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/content.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Content page template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/mainstream-guide.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Mainstream guide example
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/question.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Question page template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/start.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Start page template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/task-list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "govuk-prototype-kit/layouts/govuk-branded.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Task list template – {{ serviceName }} – GOV.UK Prototype Kit
Expand Down
2 changes: 1 addition & 1 deletion prototype-starter/app/views/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "layout.html" %}
{% extends "layouts/main.html" %}

{% block pageTitle %}
Home – {{serviceName}} – GOV.UK Prototype Kit
Expand Down
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ middlewareFunctions.push(sessionUtils.getSessionMiddleware())
middlewareFunctions.forEach(func => app.use(func))

// Set up App
var appViews = extensions.getAppViews([
var appViews = [
path.join(projectDir, '/app/views/')
])
].concat(extensions.getAppViews())

var nunjucksConfig = {
autoescape: true,
Expand Down