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 14, 2023
1 parent b4ae779 commit a04c67b
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 115 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
25 changes: 15 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: paths.public,

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'),
destPath: paths.public,

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

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

/**
Expand All @@ -56,6 +53,14 @@ gulp.task('build:dist', build.dist())
*/
gulp.task('dev', gulp.series(
'build:public',
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', gulp.series(
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:public",
"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
48 changes: 38 additions & 10 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,6 +48,10 @@ 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()

// Screenshot stack
Expand Down Expand Up @@ -99,19 +134,12 @@ export async function screenshotExample (page, exampleName) {
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
getListing(join(paths.src, 'govuk/components'))
])

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.

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
7 changes: 3 additions & 4 deletions tasks/build/public.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 public 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
23 changes: 0 additions & 23 deletions tasks/clean.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion tasks/compile-configs.mjs → tasks/configs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { basename, parse, join } from 'path'
* @param {AssetEntry[1]} options - Asset options
* @returns {() => Promise<void>} Prepared compile task
*/
export function compileConfig (modulePath, { srcPath, destPath, filePath }) {
export function compile (modulePath, { srcPath, destPath, filePath }) {
const configPath = join(destPath, filePath ? filePath(parse(modulePath)) : modulePath)

const task = async () => {
Expand Down
Loading

0 comments on commit a04c67b

Please sign in to comment.