Skip to content

Commit

Permalink
Namespace tasks for import into workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 22, 2023
1 parent 508bd64 commit b05e66c
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 155 deletions.
2 changes: 1 addition & 1 deletion config/jest/browser/open.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import setup from 'jest-environment-puppeteer/setup'

import { download } from '../../../tasks/browser/download.mjs'
import { download } from '../../../tasks/browser.mjs'

/**
* Open browser
Expand Down
23 changes: 13 additions & 10 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import gulp from 'gulp'

import { paths } from './config/index.js'
import * as build from './tasks/build/index.mjs'
import { compileJavaScripts } from './tasks/compile-javascripts.mjs'
import { compileStylesheets } from './tasks/compile-stylesheets.mjs'
import { watch } from './tasks/gulp/watch.mjs'
import { npmScriptTask } from './tasks/run.mjs'
import { browser, files, scripts, styles, npm } from './tasks/index.mjs'

/**
* Umbrella scripts tasks (for watch)
* Runs JavaScript code quality checks, documentation, compilation
*/
gulp.task('scripts', gulp.series(
compileJavaScripts('all.mjs', {
scripts.compile('all.mjs', {
srcPath: join(paths.src, 'govuk'),
destPath: join(paths.public, 'javascripts'),

Expand All @@ -23,15 +20,15 @@ gulp.task('scripts', gulp.series(
}
}),

npmScriptTask('build:jsdoc')
npm.run('build:jsdoc')
))

/**
* Umbrella styles tasks (for watch)
* Runs Sass code quality checks, documentation, compilation
*/
gulp.task('styles', gulp.series(
compileStylesheets('**/[!_]*.scss', {
styles.compile('**/[!_]*.scss', {
srcPath: join(paths.app, 'src/stylesheets'),
destPath: join(paths.public, 'stylesheets'),

Expand All @@ -40,7 +37,7 @@ gulp.task('styles', gulp.series(
}
}),

npmScriptTask('build:sassdoc')
npm.run('build:sassdoc')
))

/**
Expand All @@ -56,6 +53,12 @@ gulp.task('build:dist', build.dist())
*/
gulp.task('dev', gulp.series(
'build:app',
watch,
npmScriptTask('serve', ['--workspace', 'app'])
files.watch,
npm.run('serve', ['--workspace', 'app'])
))

/**
* Screenshots task
* Sends screenshots to Percy for visual regression testing
*/
gulp.task('screenshots', browser.screenshots)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"postbuild:dist": "jest --color --selectProjects \"Build tasks\" --testMatch \"**/*dist.test*\"",
"pretest": "npm run build:app",
"test": "jest --color --ignoreProjects \"Build tasks\" --maxWorkers=50%",
"test:screenshots": "node ./tasks/screenshot-components.mjs",
"test:screenshots": "gulp screenshots",
"lint": "npm run lint:editorconfig && npm run lint:prettier && npm run lint:js && npm run lint:scss",
"lint:editorconfig": "npm run lint:editorconfig:cli",
"lint:editorconfig:cli": "editorconfig-checker",
Expand Down
2 changes: 1 addition & 1 deletion tasks/compile-assets.mjs → tasks/assets.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { dirname } from 'path'
* @param {AssetOutput} result - Generated or minified bundle
* @returns {Promise<void>}
*/
export async function writeAsset (filePath, result) {
export async function write (filePath, result) {
await mkdir(dirname(filePath), { recursive: true })

const writeTasks = []
Expand Down
63 changes: 43 additions & 20 deletions tasks/screenshot-components.mjs → tasks/browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,44 @@ import { join } from 'path'

import percySnapshot from '@percy/puppeteer'
import { isPercyEnabled } from '@percy/sdk-utils'
import { launch } from 'puppeteer'
import puppeteer from 'puppeteer'

import { paths } from '../config/index.js'
import { filterPath, getDirectories, getListing } from '../lib/file-helper.js'
import { goToComponent, goToExample } from '../lib/puppeteer-helpers.js'
import configPuppeteer from '../puppeteer.config.js'

import { download } from './browser/download.mjs'
/**
* Puppeteer browser downloader
*/
export async function download () {
const fetcher = puppeteer.createBrowserFetcher({
path: join(configPuppeteer.cacheDirectory, 'chrome')
})

// Downloaded versions
const versions = fetcher.localRevisions()

// Download latest browser (unless cached)
if (!versions.includes(puppeteer.defaultBrowserRevision)) {
await fetcher.download(puppeteer.defaultBrowserRevision)

// Remove outdated browser versions
for (const version of versions) {
await fetcher.remove(version)
}
}
}

/**
* Puppeteer browser launcher
*/
export async function launch () {
await download()

// Open browser
return puppeteer.launch()
}

/**
* Send screenshots in concurrent batches to Percy
Expand All @@ -17,7 +48,13 @@ import { download } from './browser/download.mjs'
* @returns {Promise<void>}
*/
export async function screenshots () {
if (!await isPercyEnabled()) {
throw new Error('Percy healthcheck failed')
}

const browser = await launch()
const componentNames = await getDirectories(join(paths.src, 'govuk/components'))
const exampleNames = ['text-alignment', 'typography']

// Screenshot stack
const input = []
Expand Down Expand Up @@ -55,13 +92,16 @@ export async function screenshots () {
* @returns {Promise<void>}
*/
export async function screenshotComponent (page, componentName) {
const componentFiles = await getListing(join(paths.src, 'govuk/components', componentName))

// Navigate to component
await goToComponent(page, componentName)

// Screenshot preview page (with JavaScript)
await percySnapshot(page, `js: ${componentName}`)

// Check for "JavaScript enabled" components
if (componentsFiles.some(filterPath([`**/${componentName}.mjs`]))) {
if (componentFiles.some(filterPath([`**/${componentName}.mjs`]))) {
await page.setJavaScriptEnabled(false)

// Screenshot preview page (without JavaScript)
Expand Down Expand Up @@ -98,20 +138,3 @@ export async function screenshotExample (page, exampleName) {
// Close page
return page.close()
}

if (!await isPercyEnabled()) {
throw new Error('Percy healthcheck failed')
}

const [componentNames, componentsFiles] = await Promise.all([
getDirectories(join(paths.src, 'govuk/components')),
getListing(join(paths.src, 'govuk/components')),
download() // Download browser
])

const exampleNames = [
'text-alignment',
'typography'
]

await screenshots() // Take screenshots
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import puppeteer from 'puppeteer'

import { download } from './download.mjs'
import { download } from './browser.mjs'

jest.mock('puppeteer')

Expand Down
27 changes: 0 additions & 27 deletions tasks/browser/download.mjs

This file was deleted.

7 changes: 3 additions & 4 deletions tasks/build/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { join } from 'path'
import gulp from 'gulp'

import { paths } from '../../config/index.js'
import { clean } from '../clean.mjs'
import { copyAssets } from '../gulp/copy-to-destination.mjs'
import * as files from '../files.mjs'

/**
* Build review app task
Expand All @@ -13,12 +12,12 @@ import { copyAssets } from '../gulp/copy-to-destination.mjs'
* @returns {() => import('gulp').TaskFunction} Task function
*/
export default () => gulp.series(
clean('**/*', {
files.clean('**/*', {
destPath: paths.public
}),

// Copy GOV.UK Frontend static assets
copyAssets('**/*', {
files.copyAssets('**/*', {
srcPath: join(paths.src, 'govuk/assets'),
destPath: join(paths.public, 'assets')
}),
Expand Down
18 changes: 8 additions & 10 deletions tasks/build/dist.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { join } from 'path'
import gulp from 'gulp'

import { paths, pkg } from '../../config/index.js'
import { clean } from '../clean.mjs'
import { compileJavaScripts } from '../compile-javascripts.mjs'
import { compileStylesheets } from '../compile-stylesheets.mjs'
import { version } from '../file.mjs'
import { copyAssets } from '../gulp/copy-to-destination.mjs'
import * as files from '../files.mjs'
import * as scripts from '../scripts.mjs'
import * as styles from '../styles.mjs'

/**
* Build dist task
Expand All @@ -16,18 +14,18 @@ import { copyAssets } from '../gulp/copy-to-destination.mjs'
* @returns {() => import('gulp').TaskFunction} Task function
*/
export default () => gulp.series(
clean('**/*', {
files.clean('**/*', {
destPath: paths.dist
}),

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

// Compile GOV.UK Frontend JavaScript
compileJavaScripts('all.mjs', {
scripts.compile('all.mjs', {
srcPath: join(paths.src, 'govuk'),
destPath: paths.dist,

Expand All @@ -37,7 +35,7 @@ export default () => gulp.series(
}),

// Compile GOV.UK Frontend Sass
compileStylesheets('**/[!_]*.scss', {
styles.compile('**/[!_]*.scss', {
srcPath: join(paths.src, 'govuk'),
destPath: paths.dist,

Expand All @@ -47,7 +45,7 @@ export default () => gulp.series(
}),

// Update GOV.UK Frontend version
version('VERSION.txt', {
files.version('VERSION.txt', {
destPath: paths.dist
})
)
23 changes: 11 additions & 12 deletions tasks/build/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { join } from 'path'
import gulp from 'gulp'

import { paths } from '../../config/index.js'
import { clean } from '../clean.mjs'
import { compileConfig } from '../compile-configs.mjs'
import { compileJavaScripts } from '../compile-javascripts.mjs'
import { compileStylesheets } from '../compile-stylesheets.mjs'
import { copyAssets, copyFiles } from '../gulp/copy-to-destination.mjs'
import * as configs from '../configs.mjs'
import * as files from '../files.mjs'
import * as scripts from '../scripts.mjs'
import * as styles from '../styles.mjs'

/**
* Build package task
Expand All @@ -16,7 +15,7 @@ import { copyAssets, copyFiles } from '../gulp/copy-to-destination.mjs'
* @returns {() => import('gulp').TaskFunction} Task function
*/
export default () => gulp.series(
clean('**/*', {
files.clean('**/*', {
destPath: paths.package,
ignore: [
'**/package.json',
Expand All @@ -25,19 +24,19 @@ export default () => gulp.series(
}),

// Copy GOV.UK Frontend files
copyFiles({
files.copyFiles({
srcPath: paths.src,
destPath: paths.package
}),

// Copy GOV.UK Frontend JavaScript (ES modules)
copyAssets('**/!(*.test).mjs', {
files.copyAssets('**/!(*.test).mjs', {
srcPath: join(paths.src, 'govuk'),
destPath: join(paths.package, 'govuk-esm')
}),

// Compile GOV.UK Frontend JavaScript (AMD modules)
compileJavaScripts('**/!(*.test).mjs', {
scripts.compile('**/!(*.test).mjs', {
srcPath: join(paths.src, 'govuk'),
destPath: join(paths.package, 'govuk'),

Expand All @@ -47,7 +46,7 @@ export default () => gulp.series(
}),

// Apply CSS prefixes to GOV.UK Frontend Sass
compileStylesheets('**/*.scss', {
styles.compile('**/*.scss', {
srcPath: join(paths.src, 'govuk'),
destPath: join(paths.package, 'govuk'),

Expand All @@ -57,7 +56,7 @@ export default () => gulp.series(
}),

// Apply CSS prefixes to GOV.UK Prototype Kit Sass
compileStylesheets('init.scss', {
styles.compile('init.scss', {
srcPath: join(paths.src, 'govuk-prototype-kit'),
destPath: join(paths.package, 'govuk-prototype-kit'),

Expand All @@ -67,7 +66,7 @@ export default () => gulp.series(
}),

// Compile GOV.UK Prototype Kit config
compileConfig('govuk-prototype-kit.config.mjs', {
configs.compile('govuk-prototype-kit.config.mjs', {
srcPath: join(paths.src, 'govuk-prototype-kit'),
destPath: paths.package,

Expand Down
Loading

0 comments on commit b05e66c

Please sign in to comment.