-
Notifications
You must be signed in to change notification settings - Fork 334
/
watch.mjs
99 lines (86 loc) · 2.73 KB
/
watch.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { join } from 'path'
import { paths } from '@govuk-frontend/config'
import { npm, task } from '@govuk-frontend/tasks'
import gulp from 'gulp'
import slash from 'slash'
import { scripts, styles } from './index.mjs'
/**
* Watch task
*
* During development, this task will:
*
* - lint and run `gulp styles` when `.scss` files change
* - lint and run `gulp scripts` when `.{cjs,js,mjs}` files change
*
* These tasks respond to `gulp watch` output from GOV.UK Frontend:
* {@link file://./../../govuk-frontend/tasks/watch.mjs}
*
* @type {import('@govuk-frontend/tasks').TaskFunction}
*/
export const watch = (options) => gulp.parallel(...getTasks(options))
/**
* Compute the lists of tasks to be run in parallel
*
* @param {import('@govuk-frontend/tasks').TaskOptions} options
* @returns {any[]} The list of tasks to run in parallel
*/
function getTasks(options) {
const tasks = {
'lint:scss watch': disabledBy('GOVUK_DS_FRONTEND_NO_LINTING', 'scss', () =>
gulp.watch(
'**/*.scss',
{ cwd: options.srcPath },
// Run Stylelint checks
npm.script('lint:scss:cli', [
slash(join(options.workspace, '**/*.scss'))
])
)
),
'compile:scss watch': () =>
gulp.watch(
['**/*.scss', join(paths.package, 'dist/govuk/index.scss')],
{
awaitWriteFinish: true,
cwd: options.srcPath,
// Prevent early Sass compile by ignoring delete (unlink) event
// when GOV.UK Frontend runs the `clean` script before build
events: ['add', 'change']
},
// Run Sass compile
styles(options)
),
'lint:js watch': disabledBy('GOVUK_DS_FRONTEND_NO_LINTING', 'js', () =>
gulp.watch(
'**/*.{cjs,js,mjs}',
{ cwd: options.srcPath, ignored: ['**/*.test.*'] },
gulp.parallel(
// Run TypeScript compiler
npm.script('build:types', ['--incremental', '--pretty'], options),
// Run ESLint checks
npm.script('lint:js:cli', [
slash(join(options.workspace, '**/*.{cjs,js,mjs}'))
])
)
)
),
'compile:js watch': () =>
gulp.watch(
'javascripts/**/*.mjs',
{ cwd: options.srcPath },
// Run JavaScripts compile
scripts(options)
)
}
return Object.entries(tasks)
.filter(([taskName, fn]) => !!fn)
.map(([taskName, fn]) => task.name(taskName, fn))
}
function disabledBy(envVariableName, value, fn) {
// Split by comma to ensure we'll match full options
// avoiding `css` to match `scss` if we were looking in the whole string
const disabledValues = process.env[envVariableName]?.split?.(',')
if (disabledValues?.includes?.(value)) {
return null
}
return fn
}