Skip to content

Commit

Permalink
Configure static file copy from package build task
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 23, 2023
1 parent 8edda31 commit 2051717
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 71 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"jsdoc": "^4.0.2",
"jsdoc-tsimport-plugin": "^1.0.5",
"map-stream": "^0.0.7",
"merge-stream": "^2.0.0",
"minimatch": "^7.3.0",
"nunjucks": "^3.2.3",
"plugin-error": "^2.0.1",
Expand Down
32 changes: 30 additions & 2 deletions tasks/build/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,30 @@ export default () => gulp.series(
]
}),

// Copy GOV.UK Frontend files
files.copyFiles({
// Copy GOV.UK Frontend template files
files.copy('**/*.{md,njk}', {
srcPath: join(paths.src, 'govuk'),
destPath: join(paths.package, 'govuk'),

// Preserve paths.package README when copying to ./package
// https://github.com/alphagov/govuk-frontend/tree/main/package#readme
ignore: ['**/govuk/README.md']
}),

// Copy GOV.UK Frontend static assets
files.copy('**/*', {
srcPath: join(paths.src, 'govuk/assets'),
destPath: join(paths.package, 'govuk/assets')
}),

// Generate GOV.UK Frontend fixtures.json from ${componentName}.yaml
files.generateFixtures({
srcPath: paths.src,
destPath: paths.package
}),

// Generate GOV.UK Frontend macro-options.json from ${componentName}.yaml
files.generateMacroOptions({
srcPath: paths.src,
destPath: paths.package
}),
Expand Down Expand Up @@ -70,5 +92,11 @@ export default () => gulp.series(
filePath (file) {
return join(file.dir, `${file.name}.json`)
}
}),

// Copy GOV.UK Prototype Kit JavaScript
files.copy('**/*.js', {
srcPath: join(paths.src, 'govuk-prototype-kit'),
destPath: join(paths.package, 'govuk-prototype-kit')
})
)
2 changes: 1 addition & 1 deletion tasks/files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function copy (pattern, { srcPath, destPath, ignore = [] }) {
}

// Include Gulp legacy file tasks
export { copyFiles } from './gulp/copy-to-destination.mjs'
export { generateFixtures, generateMacroOptions } from './gulp/copy-to-destination.mjs'
export { watch } from './gulp/watch.mjs'

/**
Expand Down
112 changes: 48 additions & 64 deletions tasks/gulp/copy-to-destination.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,63 @@ import gulp from 'gulp'
import rename from 'gulp-rename'
import yaml from 'js-yaml'
import map from 'map-stream'
import merge from 'merge-stream'
import nunjucks from 'nunjucks'
import slash from 'slash'

import { paths } from '../../config/index.js'

/**
* Copy files task
* Generate fixtures.json from ${componentName}.yaml
*
* Copies files to destination with './govuk' suffix
* Includes fonts, images, polyfills, component files
* @param {AssetEntry[1]} options - Asset options
* @returns {() => import('stream').Stream} Output file stream
*/
export function generateFixtures ({ srcPath, destPath }) {
const task = () => gulp.src(`${slash(srcPath)}/govuk/components/**/*.yaml`, {
base: slash(srcPath)
})
.pipe(map(async (file, done) => {
try {
done(null, await generateFixture(file))
} catch (error) {
done(error)
}
}))
.pipe(rename({
basename: 'fixtures',
extname: '.json'
}))
.pipe(gulp.dest(slash(destPath)))

task.displayName = 'copy:fixtures'

return task
}

/**
* Generate macro-options.json from ${componentName}.yaml
*
* @param {AssetEntry[1]} options - Asset options
* @returns {() => import('stream').Stream} Output file stream
*/
export function copyFiles ({ srcPath, destPath }) {
const task = () => merge(
gulp.src([
`${slash(srcPath)}/**/*`,

// Exclude files we don't want to publish
'!**/.DS_Store',
'!**/*.mjs',
'!**/*.test.*',
'!**/__snapshots__/',
'!**/__snapshots__/**',
'!**/tsconfig.json',

// Preserve destination README when copying to ./package
// https://github.com/alphagov/govuk-frontend/tree/main/package#readme
`!${slash(srcPath)}/govuk/README.md`,

// Exclude Sass files handled by Gulp 'compile:scss'
`!${slash(srcPath)}/**/*.scss`,

// Exclude source YAML handled by JSON streams below
`!${slash(srcPath)}/govuk/components/**/*.yaml`
]),

// Generate fixtures.json from ${componentName}.yaml
gulp.src(`${slash(srcPath)}/govuk/components/**/*.yaml`, {
base: slash(srcPath)
})
.pipe(map(async (file, done) => {
try {
done(null, await generateFixtures(file))
} catch (error) {
done(error)
}
}))
.pipe(rename({
basename: 'fixtures',
extname: '.json'
})),

// Generate macro-options.json from ${componentName}.yaml
gulp.src(`${slash(srcPath)}/govuk/components/**/*.yaml`, {
base: slash(srcPath)
})
.pipe(map(async (file, done) => {
try {
done(null, await generateMacroOptions(file))
} catch (error) {
done(error)
}
}))
.pipe(rename({
basename: 'macro-options',
extname: '.json'
}))
).pipe(gulp.dest(slash(destPath)))

task.displayName = 'copy:files'
export function generateMacroOptions ({ srcPath, destPath }) {
const task = () => gulp.src(`${slash(srcPath)}/govuk/components/**/*.yaml`, {
base: slash(srcPath)
})
.pipe(map(async (file, done) => {
try {
done(null, await generateMacroOption(file))
} catch (error) {
done(error)
}
}))
.pipe(rename({
basename: 'macro-options',
extname: '.json'
}))
.pipe(gulp.dest(slash(destPath)))

task.displayName = 'copy:macro-options'

return task
}
Expand All @@ -87,7 +71,7 @@ export function copyFiles ({ srcPath, destPath }) {
* @param {import('vinyl')} file - Component data ${componentName}.yaml
* @returns {Promise<import('vinyl')>} Component fixtures.json
*/
async function generateFixtures (file) {
async function generateFixture (file) {
const json = await convertYamlToJson(file)

if (!json?.examples) {
Expand Down Expand Up @@ -131,7 +115,7 @@ async function generateFixtures (file) {
* @param {import('vinyl')} file - Component data ${componentName}.yaml
* @returns {Promise<import('vinyl')>} Component macro-options.json
*/
async function generateMacroOptions (file) {
async function generateMacroOption (file) {
const json = await convertYamlToJson(file)

if (!json?.params) {
Expand Down

0 comments on commit 2051717

Please sign in to comment.