Files
notifications-admin/gulpfile.js

133 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-08-08 14:32:33 -06:00
const { src, dest, series } = require('gulp');
const rollup = require('@rollup/stream');
const rollupPluginCommonjs = require('@rollup/plugin-commonjs');
const rollupPluginNodeResolve = require('@rollup/plugin-node-resolve');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
2024-08-08 14:01:05 -06:00
const gulpMerge = require('gulp-merge');
const uswds = require("@uswds/compile");
const plugins = {};
plugins.addSrc = require('gulp-add-src');
plugins.babel = require('gulp-babel');
plugins.cleanCSS = require('gulp-clean-css');
plugins.concat = require('gulp-concat');
plugins.jshint = require('gulp-jshint');
plugins.prettyerror = require('gulp-prettyerror');
plugins.uglify = require('gulp-uglify');
const paths = {
src: 'app/assets/',
dist: 'app/static/',
npm: 'node_modules/',
toolkit: 'node_modules/govuk_frontend_toolkit/',
govuk_frontend: 'node_modules/govuk-frontend/'
};
const javascripts = () => {
2024-08-08 14:32:33 -06:00
const vendored = rollup({
input: paths.src + 'javascripts/modules/all.mjs',
plugins: [
rollupPluginNodeResolve({
mainFields: ['module', 'main']
}),
rollupPluginCommonjs({
include: 'node_modules/**'
})
],
output: {
format: 'iife',
name: 'GOVUK'
}
})
.pipe(source('all.mjs'))
.pipe(buffer())
.pipe(plugins.addSrc.prepend([
paths.npm + 'hogan.js/dist/hogan-3.0.2.js',
paths.npm + 'jquery/dist/jquery.min.js',
paths.npm + 'query-command-supported/dist/queryCommandSupported.min.js',
paths.npm + 'timeago/jquery.timeago.js',
Support registering a new authenticator This adds Yubico's FIDO2 library and two APIs for working with the "navigator.credentials.create()" function in JavaScript. The GET API uses the library to generate options for the "create()" function, and the POST API decodes and verifies the resulting credential. While the options and response are dict-like, CBOR is necessary to encode some of the byte-level values, which can't be represented in JSON. Much of the code here is based on the Yubico library example [1][2]. Implementation notes: - There are definitely better ways to alert the user about failure, but window.alert() will do for the time being. Using location.reload() is also a bit jarring if the page scrolls, but not a major issue. - Ideally we would use window.fetch() to do AJAX calls, but we don't have a polyfill for this, and we use $.ajax() elsewhere [3]. We need to do a few weird tricks [6] to stop jQuery trashing the data. - The FIDO2 server doesn't serve web requests; it's just a "server" in the sense of WebAuthn terminology. It lives in its own module, since it needs to be initialised with the app / config. - $.ajax returns a promise-like object. Although we've used ".fail()" elsewhere [3], I couldn't find a stub object that supports it, so I've gone for ".catch()", and used a Promise stub object in tests. - WebAuthn only works over HTTPS, but there's an exception for "localhost" [4]. However, the library is a bit too strict [5], so we have to disable origin verification to avoid needing HTTPS for dev work. [1]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/examples/server/server.py [2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/examples/server/static/register.html [3]: https://github.com/alphagov/notifications-admin/blob/91453d36395b7a0cf2998dfb8a5f52cc9e96640f/app/assets/javascripts/updateContent.js#L33 [4]: https://stackoverflow.com/questions/55971593/navigator-credentials-is-null-on-local-server [5]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/rpid.py#L69 [6]: https://stackoverflow.com/questions/12394622/does-jquery-ajax-or-load-allow-for-responsetype-arraybuffer
2021-05-07 18:10:07 +01:00
paths.npm + 'textarea-caret/index.js',
2024-06-05 14:56:22 -07:00
paths.npm + 'cbor-js/cbor.js',
paths.npm + 'd3/dist/d3.min.js'
]));
const local = src([
paths.toolkit + 'javascripts/govuk/modules.js',
paths.toolkit + 'javascripts/govuk/show-hide-content.js',
paths.src + 'javascripts/copyToClipboard.js',
paths.src + 'javascripts/autofocus.js',
paths.src + 'javascripts/enhancedTextbox.js',
paths.src + 'javascripts/fileUpload.js',
paths.src + 'javascripts/radioSelect.js',
paths.src + 'javascripts/updateContent.js',
paths.src + 'javascripts/listEntry.js',
paths.src + 'javascripts/liveSearch.js',
paths.src + 'javascripts/errorTracking.js',
paths.src + 'javascripts/preventDuplicateFormSubmissions.js',
paths.src + 'javascripts/fullscreenTable.js',
paths.src + 'javascripts/colourPreview.js',
paths.src + 'javascripts/templateFolderForm.js',
paths.src + 'javascripts/collapsibleCheckboxes.js',
paths.src + 'javascripts/radioSlider.js',
paths.src + 'javascripts/updateStatus.js',
paths.src + 'javascripts/errorBanner.js',
paths.src + 'javascripts/homepage.js',
paths.src + 'javascripts/timeoutPopup.js',
paths.src + 'javascripts/date.js',
2024-02-27 11:53:51 -05:00
paths.src + 'javascripts/loginAlert.js',
paths.src + 'javascripts/main.js',
paths.src + 'javascripts/totalMessagesChart.js',
paths.src + 'javascripts/activityChart.js',
])
2022-07-21 18:25:23 -07:00
.pipe(plugins.prettyerror())
.pipe(plugins.babel({
presets: ['@babel/preset-env']
}));
2024-08-08 14:01:05 -06:00
return gulpMerge(vendored, local)
.pipe(plugins.uglify())
.pipe(plugins.concat('all.js'))
2024-08-08 14:32:33 -06:00
.pipe(dest(paths.dist + 'javascripts/'));
};
2024-08-09 09:41:13 -06:00
// Task to copy `gtm_head.js`
2024-08-09 09:18:50 -06:00
const copyGtmHead = () => {
return src(paths.src + 'js/gtm_head.js')
.pipe(dest(paths.dist + 'js/'));
};
2024-08-23 14:01:51 -07:00
// Task to copy `setTimezone.js`
const copySetTimezone = () => {
return src(paths.src + 'js/setTimezone.js')
.pipe(dest(paths.dist + 'js/'));
};
2024-08-09 09:41:13 -06:00
// Task to copy images
const copyImages = () => {
return src(paths.src + 'images/**/*', { encoding: false })
2024-08-09 09:41:13 -06:00
.pipe(dest(paths.dist + 'images/'));
};
2024-08-09 09:41:13 -06:00
// Configure USWDS paths
2023-04-24 14:57:35 -04:00
uswds.settings.version = 3;
2024-08-09 07:40:58 -06:00
uswds.paths.dist.css = paths.dist + 'css';
uswds.paths.dist.js = paths.dist + 'js';
uswds.paths.dist.img = paths.dist + 'img';
uswds.paths.dist.fonts = paths.dist + 'fonts';
uswds.paths.dist.theme = paths.src + 'sass/uswds';
2024-08-09 09:41:13 -06:00
// Task to compile USWDS styles
2024-08-09 07:40:58 -06:00
const styles = async () => {
await uswds.compile();
};
2023-04-24 14:57:35 -04:00
2024-08-09 09:41:13 -06:00
// Task to copy USWDS assets
2024-08-09 07:40:58 -06:00
const copyAssets = async () => {
await uswds.copyAssets();
};
2023-04-24 14:57:35 -04:00
2024-08-23 14:01:51 -07:00
exports.default = series(styles, javascripts, copyGtmHead, copySetTimezone, copyImages, copyAssets);