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

Switch from CommonJS to ES6 modules #9

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 0 additions & 2 deletions .labrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

module.exports = {
verbose: true,
coverage: true,
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/health/airbrake.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

class AirbrakeController {
static async index (req, _h) {
// First section tests connecting to Airbrake through a manual notification
Expand All @@ -18,4 +16,4 @@ class AirbrakeController {
}
}

module.exports = AirbrakeController
export default AirbrakeController
6 changes: 2 additions & 4 deletions app/controllers/health/database.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const DatabaseHealthCheckService = require('../../services/database_health_check.service.js')
import DatabaseHealthCheckService from '../../services/database_health_check.service.js'

class DatabaseController {
static async index (_req, h) {
Expand All @@ -10,4 +8,4 @@ class DatabaseController {
}
}

module.exports = DatabaseController
export default DatabaseController
6 changes: 2 additions & 4 deletions app/controllers/root.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const ServiceStatusService = require('../services/service_status.service')
import ServiceStatusService from '../services/service_status.service.js'

class RootController {
static async index (_req, _h) {
Expand All @@ -25,4 +23,4 @@ class RootController {
}
}

module.exports = RootController
export default RootController
10 changes: 4 additions & 6 deletions app/lib/base_notifier.lib.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

/**
* @module BaseNotifierLib
*/

const { Notifier } = require('@airbrake/node')
const Pino = require('pino')
import { Notifier } from '@airbrake/node'
import Pino from 'pino'

const AirbrakeConfig = require('../../config/airbrake.config.js')
import AirbrakeConfig from '../../config/airbrake.config.js'

/**
* Based class for combined logging and Airbrake (Errbit) notification managers
Expand Down Expand Up @@ -164,4 +162,4 @@ class BaseNotifierLib {
}
}

module.exports = BaseNotifierLib
export default BaseNotifierLib
8 changes: 3 additions & 5 deletions app/lib/boom_notifier.lib.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

/**
* @module BoomNotifierLib
*/

const RequestNotifierLib = require('./request_notifier.lib.js')
import RequestNotifierLib from './request_notifier.lib.js'

const Boom = require('@hapi/boom')
import Boom from '@hapi/boom'

/**
* A combined logging and Airbrake (Errbit) notification manager which extends RequestNotifierLib to also throw a Boom
Expand All @@ -31,4 +29,4 @@ class BoomNotifierLib extends RequestNotifierLib {
}
}

module.exports = BoomNotifierLib
export default BoomNotifierLib
6 changes: 2 additions & 4 deletions app/lib/request_notifier.lib.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'

/**
* @module RequestNotifierLib
*/

const BaseNotifierLib = require('./base_notifier.lib.js')
import BaseNotifierLib from './base_notifier.lib.js'

/**
* A combined logging and Airbrake (Errbit) notification manager for actions that take place within a
Expand Down Expand Up @@ -89,4 +87,4 @@ class RequestNotifierLib extends BaseNotifierLib {
}
}

module.exports = RequestNotifierLib
export default RequestNotifierLib
8 changes: 3 additions & 5 deletions app/plugins/airbrake.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* @module AirbrakePlugin
*/
Expand All @@ -17,8 +15,8 @@
* {@link https://github.com/DEFRA/charging-module-api/blob/master/app/plugins/airbrake.js}
*/

const { Notifier } = require('@airbrake/node')
const AirbrakeConfig = require('../../config/airbrake.config.js')
import { Notifier } from '@airbrake/node'
import AirbrakeConfig from '../../config/airbrake.config.js'

const AirbrakePlugin = {
name: 'airbrake',
Expand Down Expand Up @@ -57,4 +55,4 @@ const AirbrakePlugin = {
}
}

module.exports = AirbrakePlugin
export default AirbrakePlugin
6 changes: 2 additions & 4 deletions app/plugins/blipp.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Plugin to output the routes table to console at startup.
*
Expand All @@ -24,7 +22,7 @@
* @module BlippPlugin
*/

const Blipp = require('blipp')
import Blipp from 'blipp'

const BlippPlugin = {
plugin: Blipp,
Expand All @@ -34,4 +32,4 @@ const BlippPlugin = {
}
}

module.exports = BlippPlugin
export default BlippPlugin
4 changes: 1 addition & 3 deletions app/plugins/error_pages.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Add an `onPreResponse` listener to return error pages
*
Expand Down Expand Up @@ -39,4 +37,4 @@ const ErrorPagesPlugin = {
}
}

module.exports = ErrorPagesPlugin
export default ErrorPagesPlugin
6 changes: 2 additions & 4 deletions app/plugins/hapi_pino.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Plugin that handles logging for the application
*
Expand All @@ -9,7 +7,7 @@
*
* @module HapiPinoPlugin
*/
const HapiPino = require('hapi-pino')
import HapiPino from 'hapi-pino'

/**
* Return test configuration options for the logger
Expand Down Expand Up @@ -54,4 +52,4 @@ const HapiPinoPlugin = logInTest => {
}
}

module.exports = HapiPinoPlugin
export default HapiPinoPlugin
6 changes: 2 additions & 4 deletions app/plugins/request_notifier.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Add an instance of `RequestNotifierLib` to the `request.app` as part of every request.
*
Expand All @@ -9,7 +7,7 @@
* @module RequestNotifierPlugin
*/

const RequestNotifierLib = require('../lib/request_notifier.lib.js')
import RequestNotifierLib from '../lib/request_notifier.lib.js'

const RequestNotifierPlugin = {
name: 'Notifier',
Expand All @@ -22,4 +20,4 @@ const RequestNotifierPlugin = {
}
}

module.exports = RequestNotifierPlugin
export default RequestNotifierPlugin
16 changes: 7 additions & 9 deletions app/plugins/router.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Our router plugin which pulls in the various routes we have defined ready to be registered with the Hapi server
* (server.js).
Expand All @@ -11,13 +9,13 @@
* @module RouterPlugin
*/

const FilterRoutesService = require('../services/plugins/filter_routes.service.js')
const AirbrakeConfig = require('../../config/airbrake.config.js')
import FilterRoutesService from '../services/plugins/filter_routes.service.js'
import AirbrakeConfig from '../../config/airbrake.config.js'

const AirbrakeRoutes = require('../routes/airbrake.routes.js')
const AssetRoutes = require('../routes/assets.routes.js')
const DatabaseRoutes = require('../routes/database.routes.js')
const RootRoutes = require('../routes/root.routes.js')
import AirbrakeRoutes from '../routes/airbrake.routes.js'
import AssetRoutes from '../routes/assets.routes.js'
import DatabaseRoutes from '../routes/database.routes.js'
import RootRoutes from '../routes/root.routes.js'

const routes = [
...RootRoutes,
Expand All @@ -37,4 +35,4 @@ const RouterPlugin = {
}
}

module.exports = RouterPlugin
export default RouterPlugin
4 changes: 1 addition & 3 deletions app/plugins/stop.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Handle `SIGTERM` and `SIGINT` calls to the app
*
Expand Down Expand Up @@ -55,4 +53,4 @@ const StopPlugin = {
}
}

module.exports = StopPlugin
export default StopPlugin
20 changes: 11 additions & 9 deletions app/plugins/views.plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

/**
* Our views plugin which serves views using nunjucks and govuk-frontend.
*
Expand All @@ -10,15 +8,19 @@
* @module ViewsPlugin
*/

import path from 'path'
import nunjucks from 'nunjucks'
import Vision from '@hapi/vision'

import ServerConfig from '../../config/server.config.js'

const SERVICE_NAME = 'Manage your water abstraction or impoundment licence'

const path = require('path')
const nunjucks = require('nunjucks')
const pkg = require('../../package.json')
const ServerConfig = require('../../config/server.config.js')
// thanks to https://stackoverflow.com/a/66651120/19939610
const __dirname = new URL('.', import.meta.url).pathname

const ViewsPlugin = {
plugin: require('@hapi/vision'),
plugin: Vision,
options: {
engines: {
// The 'engine' is the file extension this applies to; in this case, .njk
Expand Down Expand Up @@ -48,12 +50,12 @@ const ViewsPlugin = {
// layout.njk as the path to get static assets like client-side javascript. These are added to or overridden in the
// h.view() call in a controller.
context: {
appVersion: pkg.version,
appVersion: process.env.npm_package_version,
assetPath: '/assets',
serviceName: SERVICE_NAME,
pageTitle: `${SERVICE_NAME} - GOV.UK`
}
}
}

module.exports = ViewsPlugin
export default ViewsPlugin
6 changes: 2 additions & 4 deletions app/routes/airbrake.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const AirbrakeController = require('../controllers/health/airbrake.controller.js')
import AirbrakeController from '../controllers/health/airbrake.controller.js'

const routes = [
{
Expand All @@ -14,4 +12,4 @@ const routes = [
}
]

module.exports = routes
export default routes
4 changes: 1 addition & 3 deletions app/routes/assets.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const routes = [{
method: 'GET',
path: '/robots.txt',
Expand All @@ -26,4 +24,4 @@ const routes = [{
}
}]

module.exports = routes
export default routes
6 changes: 2 additions & 4 deletions app/routes/database.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const DatabaseController = require('../controllers/health/database.controller.js')
import DatabaseController from '../controllers/health/database.controller.js'

const routes = [
{
Expand All @@ -14,4 +12,4 @@ const routes = [
}
]

module.exports = routes
export default routes
6 changes: 2 additions & 4 deletions app/routes/root.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const RootController = require('../controllers/root.controller.js')
import RootController from '../controllers/root.controller.js'

const routes = [
{
Expand Down Expand Up @@ -37,4 +35,4 @@ const routes = [
}
]

module.exports = routes
export default routes
29 changes: 14 additions & 15 deletions app/server.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
'use strict'
import Hapi from '@hapi/hapi'
import Inert from '@hapi/inert'

const Hapi = require('@hapi/hapi')
import ServerConfig from '../config/server.config.js'
import TestConfig from '../config/test.config.js'

const ServerConfig = require('../config/server.config.js')
const TestConfig = require('../config/test.config.js')

const AirbrakePlugin = require('./plugins/airbrake.plugin.js')
const BlippPlugin = require('./plugins/blipp.plugin.js')
const ErrorPagesPlugin = require('./plugins/error_pages.plugin.js')
const HapiPinoPlugin = require('./plugins/hapi_pino.plugin.js')
const RequestNotifierPlugin = require('./plugins/request_notifier.plugin.js')
const RouterPlugin = require('./plugins/router.plugin.js')
const StopPlugin = require('./plugins/stop.plugin.js')
const ViewsPlugin = require('./plugins/views.plugin.js')
import AirbrakePlugin from './plugins/airbrake.plugin.js'
import BlippPlugin from './plugins/blipp.plugin.js'
import ErrorPagesPlugin from './plugins/error_pages.plugin.js'
import HapiPinoPlugin from './plugins/hapi_pino.plugin.js'
import RequestNotifierPlugin from './plugins/request_notifier.plugin.js'
import RouterPlugin from './plugins/router.plugin.js'
import StopPlugin from './plugins/stop.plugin.js'
import ViewsPlugin from './plugins/views.plugin.js'

const registerPlugins = async (server) => {
// Register the remaining plugins
await server.register(StopPlugin)
await server.register(require('@hapi/inert'))
await server.register(Inert)
await server.register(RouterPlugin)
await server.register(HapiPinoPlugin(TestConfig.logInTest))
await server.register(AirbrakePlugin)
Expand Down Expand Up @@ -53,4 +52,4 @@ process.on('unhandledRejection', err => {
process.exit(1)
})

module.exports = { init, start }
export { init, start }
Loading