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

Allow for more frontend settings to be overridden #193

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 33 additions & 5 deletions lib/events/generate-govuk-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@ const sass = require('sass')
const rollup = require('rollup')
const commonJs = require('@rollup/plugin-commonjs')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const { ensureSlash } = require('../utils.js')
const { ensureSlash, kebabise } = require('../utils.js')

/**
* @see {@link https://frontend.design-system.service.gov.uk/sass-api-reference/#settings}
*/
const settingKeys = [
'brandColour',
'textColour',
'secondaryTextColour',
'canvasBackgroundColour',
'bodyBackgroundColour',
'focusColour',
'focusTextColour',
'errorColour',
'successColour',
'borderColour',
'linkColour',
'linkVisitedColour',
'linkHoverColour',
'linkActiveColour',
'pageWidth',
'fontFamily'
]

/**
* Generate GOV.UK Frontend assets
Expand All @@ -15,8 +37,15 @@ const { ensureSlash } = require('../utils.js')
* @returns {function}
*/
module.exports = async function (dir, pathPrefix, options) {
const { imagesPath, fontsPath, brandColour, fontFamily } = options
const assetsPath = options.assetsPath || path.join(pathPrefix, 'assets')
let { assetsPath, imagesPath, fontsPath } = options
assetsPath = assetsPath || path.join(pathPrefix, 'assets')

const settings = []
for (const key of settingKeys) {
if (options[key]) {
settings.push(`$govuk-${kebabise(key)}: ${options[key]};`)
}
}

// Get plugin options and set GOV.UK Frontend variables if provided
const inputFilePath = path.join(__dirname, '../govuk.scss')
Expand All @@ -25,8 +54,7 @@ module.exports = async function (dir, pathPrefix, options) {
assetsPath ? `$govuk-assets-path: "${ensureSlash(assetsPath)}";` : [],
fontsPath ? `$govuk-fonts-path: "${ensureSlash(fontsPath)}";` : [],
imagesPath ? `$govuk-images-path: "${ensureSlash(imagesPath)}";` : [],
brandColour ? `$govuk-brand-colour: ${brandColour};` : [],
fontFamily ? `$govuk-font-family: ${fontFamily};` : [],
...settings,
inputFile
].join('\n')

Expand Down
15 changes: 15 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ const ensureSlash = (string) => {
return `${string}/`
}

/**
* Convert camelCase to kebab-case
*
* @param {string} string camelCase string
* @returns kebab-case string
*/
const kebabise = (string) => {
return string.split('').map((letter, index) => {
return letter.toUpperCase() === letter
? `${index !== 0 ? '-' : ''}${letter.toLowerCase()}`
: letter
}).join('')
}

/**
* Normalise value provided to a filter. Checks that a given value exists
* before performing a transformation.
Expand All @@ -34,5 +48,6 @@ const normalise = (value, defaultValue) => {

module.exports = {
ensureSlash,
kebabise,
normalise
}
Loading