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

Remove example files govuk frontend #890

Merged
merged 6 commits into from
Jul 12, 2018
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
7 changes: 5 additions & 2 deletions app/__tests__/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const requestParamsHomepage = {
}

const requestParamsExampleAllComponents = {
url: `http://localhost:${PORT}/examples/all-components`,
url: `http://localhost:${PORT}/components/all`,
headers: {
'Content-Type': 'text/plain'
}
Expand Down Expand Up @@ -90,7 +90,10 @@ describe('frontend app', () => {
request.get(requestParamsHomepage, (err, res) => {
let $ = cheerio.load(res.body)
let componentsList = $('li a[href^="/components/"]').get()
expect(componentsList.length).toEqual(lib.allComponents.length)
// Since we have an 'all' component link that renders the default example of all
// components, there will always be one more expected link.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let expectedComponentLinks = lib.allComponents.length + 1
expect(componentsList.length).toEqual(expectedComponentLinks)
done(err)
})
})
Expand Down
32 changes: 23 additions & 9 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const nunjucks = require('nunjucks')
const util = require('util')
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')

const readdir = util.promisify(fs.readdir)

Expand Down Expand Up @@ -66,16 +65,31 @@ module.exports = (options) => {
// Whenever the route includes a :component parameter, read the component data
// from its YAML file
app.param('component', function (req, res, next, componentName) {
let yamlPath = path.join(configPaths.components, componentName, `${componentName}.yaml`)
res.locals.componentData = fileHelper.getComponentData(componentName)
next()
})

// All components view
app.get('/components/all', function (req, res, next) {
const components = fileHelper.allComponents

try {
res.locals.componentData = yaml.safeLoad(
fs.readFileSync(yamlPath, 'utf8'), { json: true }
res.locals.componentData = components.map(componentName => {
let componentData = fileHelper.getComponentData(componentName)
let defaultExample = componentData.examples.find(
example => example.name === 'default'
)
next()
} catch (e) {
next(new Error('failed to load component YAML file'))
}
return {
componentName,
examples: [defaultExample]
}
})
res.render(`all-components`, function (error, html) {
if (error) {
next(error)
} else {
res.send(html)
}
})
})

// Component 'README' page
Expand Down
36 changes: 0 additions & 36 deletions app/views/examples/all-components/index.njk

This file was deleted.

27 changes: 27 additions & 0 deletions app/views/layouts/all-components.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "full-width.njk" %}

{% from "back-link/macro.njk" import govukBackLink %}
{% from "macros/showExamples.njk" import showExamples %}

{% set bodyClasses %}
language-markup
{% endset %}

{% block beforeContent %}
{{ govukBackLink({
"href": "/"
}) }}
{% endblock %}

{% block content %}
<div class="govuk-width-container">
<h1 class="govuk-heading-xl">
All components
</h1>
</div>
{% for data in componentData %}
{% set componentName = data.componentName %}
{% set componentNameHuman = componentName | replace("-", " ") %}
{{ showExamples(componentName, componentNameHuman, data) }}
{% endfor %}
{% endblock %}
7 changes: 4 additions & 3 deletions app/views/layouts/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<h2 class="govuk-heading-m">Components</h2>

<ul class="govuk-list">
{% for componentName in componentsDirectory | sort %}
<li><a href="/components/{{ componentName }}" class="govuk-link">{{ componentName | replace("-", " ") | capitalize }}</a></li>
{% endfor %}
<li><a href="/components/all" class="govuk-link">All</a></li>
{% for componentName in componentsDirectory | sort %}
<li><a href="/components/{{ componentName }}" class="govuk-link">{{ componentName | replace("-", " ") | capitalize }}</a></li>
{% endfor %}
</ul>
</div>

Expand Down
14 changes: 14 additions & 0 deletions lib/file-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')

const configPaths = require('../config/paths.json')

Expand All @@ -20,3 +21,16 @@ const readFileContents = filePath => {
}

exports.readFileContents = readFileContents

const getComponentData = componentName => {
try {
let yamlPath = path.join(configPaths.components, componentName, `${componentName}.yaml`)
return yaml.safeLoad(
fs.readFileSync(yamlPath, 'utf8'), { json: true }
)
} catch (error) {
return new Error(error)
}
}

exports.getComponentData = getComponentData
6 changes: 0 additions & 6 deletions src/components/back-link/back-link.njk

This file was deleted.

14 changes: 0 additions & 14 deletions src/components/breadcrumbs/breadcrumbs.njk

This file was deleted.

6 changes: 0 additions & 6 deletions src/components/button/button.njk

This file was deleted.

27 changes: 0 additions & 27 deletions src/components/checkboxes/checkboxes.njk

This file was deleted.

23 changes: 0 additions & 23 deletions src/components/date-input/date-input.njk

This file was deleted.

6 changes: 0 additions & 6 deletions src/components/details/details.njk

This file was deleted.

5 changes: 0 additions & 5 deletions src/components/error-message/error-message.njk

This file was deleted.

17 changes: 0 additions & 17 deletions src/components/error-summary/error-summary.njk

This file was deleted.

13 changes: 0 additions & 13 deletions src/components/fieldset/fieldset.njk

This file was deleted.

9 changes: 0 additions & 9 deletions src/components/file-upload/file-upload.njk

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/footer/footer.njk

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/header/header.njk

This file was deleted.

6 changes: 0 additions & 6 deletions src/components/hint/hint.njk

This file was deleted.

9 changes: 0 additions & 9 deletions src/components/input/input.njk

This file was deleted.

5 changes: 0 additions & 5 deletions src/components/inset-text/inset-text.njk

This file was deleted.

5 changes: 0 additions & 5 deletions src/components/label/label.njk

This file was deleted.

6 changes: 0 additions & 6 deletions src/components/panel/panel.njk

This file was deleted.

7 changes: 0 additions & 7 deletions src/components/phase-banner/phase-banner.njk

This file was deleted.

27 changes: 0 additions & 27 deletions src/components/radios/radios.njk

This file was deleted.

Loading