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

Ensure "clean" target matches --destination flag #2877

Merged
merged 2 commits into from
Oct 5, 2022
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
14 changes: 7 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const gulp = require('gulp')
const taskListing = require('gulp-task-listing')
const configPaths = require('./config/paths.js')
const taskArguments = require('./tasks/task-arguments')
const { destination } = require('./tasks/task-arguments.js')

// Gulp sub-tasks
require('./tasks/gulp/compile-assets.js')
Expand All @@ -12,7 +12,7 @@ require('./tasks/gulp/watch.js')
const { buildSassdocs } = require('./tasks/sassdoc.js')
const { runNodemon } = require('./tasks/nodemon.js')
const { updateDistAssetsVersion } = require('./tasks/asset-version.js')
const { cleanDist, cleanPackage, cleanPublic } = require('./tasks/clean.js')
const { clean } = require('./tasks/clean.js')
const { npmScriptTask } = require('./tasks/run.js')

/**
Expand All @@ -38,8 +38,8 @@ gulp.task('styles', gulp.series(
* Copies assets to taskArguments.destination (public)
*/
gulp.task('copy:assets', () => {
return gulp.src(configPaths.src + 'assets/**/*')
.pipe(gulp.dest(taskArguments.destination + '/assets/'))
return gulp.src(`${configPaths.src}assets/**/*`)
.pipe(gulp.dest(`${destination}/assets/`))
})

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ gulp.task('serve', gulp.parallel(
* Runs a sequence of tasks on start
*/
gulp.task('dev', gulp.series(
cleanPublic,
clean,
'compile',
'serve'
))
Expand All @@ -77,7 +77,7 @@ gulp.task('dev', gulp.series(
* Prepare package folder for publishing
*/
gulp.task('build:package', gulp.series(
cleanPackage,
clean,
'copy:files',
'js:compile'
))
Expand All @@ -87,7 +87,7 @@ gulp.task('build:package', gulp.series(
* Prepare dist folder for release
*/
gulp.task('build:dist', gulp.series(
cleanDist,
clean,
'compile',
'copy:assets',
updateDistAssetsVersion
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"sass-color-helpers": "^2.1.1",
"sassdoc": "^2.7.4",
"shuffle-seed": "^1.1.6",
"slash": "^3.0.0",
"standard": "^17.0.0",
"stylelint": "^14.13.0",
"stylelint-config-gds": "^0.2.0",
Expand Down
18 changes: 11 additions & 7 deletions tasks/asset-version.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const configPaths = require('../config/paths.js')
const { rename, writeFile } = require('fs/promises')
const { basename, resolve } = require('path')

const configPaths = require('../config/paths.js')
const { destination, isDist } = require('./task-arguments.js')

// Update assets' version numbers
// Uses the version number from `package/package.json` and writes it to various places
async function updateDistAssetsVersion () {
const distFolder = 'dist'
const pkg = require(`../${configPaths.package}package.json`)

const pkg = require('../' + configPaths.package + 'package.json')
if (!isDist) {
throw new Error('Asset versions can only be applied to ./dist')
}

// Files to process
const assetFiles = [
resolve(`${distFolder}/govuk-frontend.min.css`),
resolve(`${distFolder}/govuk-frontend-ie8.min.css`),
resolve(`${distFolder}/govuk-frontend.min.js`)
resolve(`${destination}/govuk-frontend.min.css`),
resolve(`${destination}/govuk-frontend-ie8.min.css`),
resolve(`${destination}/govuk-frontend.min.js`)
]

// Loop through files
Expand All @@ -26,7 +30,7 @@ async function updateDistAssetsVersion () {
})

// Write VERSION.txt file
assetTasks.push(writeFile(resolve(`${distFolder}/VERSION.txt`), pkg.version + '\r\n'))
assetTasks.push(writeFile(resolve(`${destination}/VERSION.txt`), pkg.version + '\r\n'))

// Resolve on completion
return Promise.all(assetTasks)
Expand Down
44 changes: 20 additions & 24 deletions tasks/clean.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
const del = require('del')
const configPaths = require('../config/paths.js')

function cleanDist () {
return del([
`${configPaths.dist}**/*`
])
}
const { destination } = require('./task-arguments.js')

function paths () {
const param = [`${destination}/**/*`]

// Preserve package files
if (destination === 'package') {
param.push(
`!${destination}/`,
`!${destination}/package.json`,
`!${destination}/govuk-prototype-kit.config.json`,
`!${destination}/README.md`
)
}

function cleanPackage () {
return del([
`${configPaths.package}**`,
`!${configPaths.package}`,
`!${configPaths.package}package.json`,
`!${configPaths.package}govuk-prototype-kit.config.json`,
`!${configPaths.package}README.md`
])
return param
}

function cleanPublic () {
return del([
`${configPaths.public}**/*`
])
function clean () {
return del(paths())
}

cleanDist.displayName = 'clean:dist'
cleanPackage.displayName = 'clean:package'
cleanPublic.displayName = 'clean:public'
clean.displayName = `clean:${destination}`

module.exports = {
cleanDist,
cleanPackage,
cleanPublic
paths,
clean
}
36 changes: 36 additions & 0 deletions tasks/clean.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe('Clean task', () => {
beforeEach(() => {
jest.resetModules()
})

it.each(
[
{
destination: 'public',
paths: ['public/**/*']
},
{
destination: 'package',
paths: [
'package/**/*',
'!package/',
'!package/package.json',
'!package/govuk-prototype-kit.config.json',
'!package/README.md'
]
},
{
destination: 'dist',
paths: ['dist/**/*']
},
{
destination: 'custom/location/here',
paths: ['custom/location/here/**/*']
}
]
)('cleans destination "$destination"', async ({ destination, paths }) => {
jest.mock('./task-arguments.js', () => ({ destination }))
const clean = await import('./clean.js')
expect(clean.paths()).toEqual(paths)
})
})
29 changes: 12 additions & 17 deletions tasks/gulp/compile-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ const { componentNameToJavaScriptModuleName } = require('../../lib/helper-functi
const path = require('path')

const gulp = require('gulp')
const configPaths = require('../../config/paths.js')
const sass = require('gulp-sass')(require('node-sass'))
const plumber = require('gulp-plumber')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const rollup = require('gulp-better-rollup')
const taskArguments = require('../task-arguments')
const gulpif = require('gulp-if')
const uglify = require('gulp-uglify')
const eol = require('gulp-eol')
Expand All @@ -23,22 +21,19 @@ const postcsspseudoclasses = require('postcss-pseudo-classes')({
blacklist: [':not(', ':disabled)', ':first-child)', ':last-child)', ':focus)', ':active)', ':hover)']
})

const configPaths = require('../../config/paths.js')
const { destination, isDist, isPublic } = require('../task-arguments.js')

// Compile CSS and JS task --------------
// --------------------------------------

// check if destination flag is public (this is the default)
const isPublic = taskArguments.destination === 'public' || false

// check if destination flag is dist
const isDist = taskArguments.destination === 'dist' || false

// Set the destination
const destinationPath = function () {
// Public & Dist directories do no need namespaced with `govuk`
if (taskArguments.destination === 'dist' || taskArguments.destination === 'public') {
return taskArguments.destination
// Public & Dist directories not namespaced with `govuk`
if (isDist || isPublic) {
return destination
} else {
return `${taskArguments.destination}/govuk/`
return `${destination}/govuk/`
}
}

Expand Down Expand Up @@ -74,7 +69,7 @@ function compileStyles () {
extname: '.min.css'
})
))
.pipe(gulp.dest(taskArguments.destination + '/'))
.pipe(gulp.dest(`${destination}/`))
}

function compileOldIE () {
Expand Down Expand Up @@ -111,7 +106,7 @@ function compileOldIE () {
extname: '.min.css'
})
))
.pipe(gulp.dest(taskArguments.destination + '/'))
.pipe(gulp.dest(`${destination}/`))
}

function compileLegacy () {
Expand All @@ -126,7 +121,7 @@ function compileLegacy () {
// :hover class you can use to simulate the hover state in the review app
postcsspseudoclasses
]))
.pipe(gulp.dest(taskArguments.destination + '/'))
.pipe(gulp.dest(`${destination}/`))
}

function compileLegacyIE () {
Expand All @@ -145,7 +140,7 @@ function compileLegacyIE () {
pseudo: { disable: true }
})
]))
.pipe(gulp.dest(taskArguments.destination + '/'))
.pipe(gulp.dest(`${destination}/`))
}

function compileFullPageStyles () {
Expand All @@ -158,7 +153,7 @@ function compileFullPageStyles () {
location.basename = location.dirname
location.dirname = ''
}))
.pipe(gulp.dest(taskArguments.destination + '/full-page-examples/'))
.pipe(gulp.dest(`${destination}/full-page-examples/`))
}

gulp.task('scss:compile', function (done) {
Expand Down
6 changes: 3 additions & 3 deletions tasks/gulp/copy-to-destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const merge = require('merge-stream')
const rename = require('gulp-rename')

const configPaths = require('../../config/paths.js')
const taskArguments = require('../task-arguments')
const { destination } = require('../task-arguments.js')

gulp.task('copy:files', () => {
return merge(
Expand All @@ -22,7 +22,7 @@ gulp.task('copy:files', () => {
gulp.src([
`${configPaths.src}**/*.mjs`,
`!${configPaths.src}**/*.test.*`
]).pipe(gulp.dest(`${taskArguments.destination}/govuk-esm/`)),
]).pipe(gulp.dest(`${destination}/govuk-esm/`)),

/**
* Copy files to destination with './govuk' suffix
Expand Down Expand Up @@ -77,7 +77,7 @@ gulp.task('copy:files', () => {
basename: 'macro-options',
extname: '.json'
}))
).pipe(gulp.dest(`${taskArguments.destination}/govuk/`))
).pipe(gulp.dest(`${destination}/govuk/`))
)
})

Expand Down
40 changes: 37 additions & 3 deletions tasks/task-arguments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
const argv = require('yargs').argv
const destination = argv.destination ? argv.destination : 'public'
const { dirname, relative, resolve } = require('path')

exports.destination = destination
const slash = require('slash')
const { argv } = require('yargs')

// Defaults for known tasks
const destinations = [
{
task: 'build:package',
Copy link
Contributor

Choose a reason for hiding this comment

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

Part of me wants to do this by parsing the task name, but this is way less fussy and prone to errors!

destination: 'package'
},
{
task: 'build:dist',
destination: 'dist'
}
]

// Non-flag arguments
const { _: tasks } = argv

// Prefer `--destination`, default for known task, or 'public'
const destination = argv.destination || (destinations
.filter(({ task }) => tasks.includes(task))[0]?.destination ?? 'public')

const rootPath = dirname(__dirname)
const destPath = resolve(rootPath, destination)

module.exports = {
argv,

// Normalise slashes (Windows) for gulp.src
destination: slash(relative(rootPath, destPath)),

// Check destination flags
isPackage: destination === 'package',
isPublic: destination === 'public',
isDist: destination === 'dist'
}
Loading