From 14cd2055f5b869876d8faf04b8bc603362fae9a3 Mon Sep 17 00:00:00 2001 From: Steve Calvert Date: Fri, 6 Aug 2021 12:57:55 -0700 Subject: [PATCH] feat: Convert to using external middleware reporter package --- index.js | 22 ++++++--------- node-tests/setup-middleware-test.js | 11 ++++++-- package.json | 1 + setup-middleware.js | 43 ----------------------------- yarn.lock | 25 +++++++++++++++++ 5 files changed, 44 insertions(+), 58 deletions(-) delete mode 100644 setup-middleware.js diff --git a/index.js b/index.js index ec49c976..13253118 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,9 @@ const path = require('path'); const fs = require('fs'); const VersionChecker = require('ember-cli-version-checker'); const validatePeerDependencies = require('validate-peer-dependencies'); -const setupMiddleware = require('./setup-middleware'); +const { + setupMiddlewareHooks, +} = require('@scalvert/ember-setup-middleware-reporter'); const CliOptionsFilter = require('./cli-options-filter'); // The different types/area for which we have content for. @@ -64,18 +66,6 @@ module.exports = { } }, - serverMiddleware(startOptions) { - setupMiddleware(startOptions.app, { - root: this.project.root, - }); - }, - - testemMiddleware(app) { - setupMiddleware(app, { - root: this.project.root, - }); - }, - /** * Allow the option to enable a11y audit and middelware reporter using environmental * variables. If set, environmental variable values are exposed to the browser @@ -86,4 +76,10 @@ module.exports = { const processedTree = new CliOptionsFilter(tree); return this._super.treeForAddonTestSupport.call(this, processedTree); }, + + ...setupMiddlewareHooks({ + name: 'ember-a11y-testing', + urlPath: 'report-violations', + reportDir: 'ember-a11y-report', + }), }; diff --git a/node-tests/setup-middleware-test.js b/node-tests/setup-middleware-test.js index be94e1d5..6f3af98e 100644 --- a/node-tests/setup-middleware-test.js +++ b/node-tests/setup-middleware-test.js @@ -4,7 +4,9 @@ const fs = require('fs'); const tmp = require('tmp'); const express = require('express'); const readJSONSync = require('fs-extra').readJSONSync; -const setupMiddleware = require('../setup-middleware'); +const { + setupMiddleware, +} = require('@scalvert/ember-setup-middleware-reporter'); const violationsFixture = require('./fixtures/violations'); function createTmpDir() { @@ -32,7 +34,12 @@ QUnit.module('setupMiddleware', function (hooks) { tmpDir = createTmpDir(); app = express(); - setupMiddleware(app, { root: tmpDir }); + setupMiddleware(app, { + root: tmpDir, + name: 'ember-a11y-testing', + urlPath: 'report-violations', + reportDir: 'ember-a11y-report', + }); server = app.listen(3000); }); diff --git a/package.json b/package.json index cf72ad94..eb80c1a8 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "dependencies": { "@ember/test-waiters": "^2.4.3", + "@scalvert/ember-setup-middleware-reporter": "^0.1.1", "axe-core": "^4.1.4", "body-parser": "^1.19.0", "broccoli-persistent-filter": "^3.1.2", diff --git a/setup-middleware.js b/setup-middleware.js deleted file mode 100644 index 517ee47c..00000000 --- a/setup-middleware.js +++ /dev/null @@ -1,43 +0,0 @@ -'use strict'; - -const bodyParser = require('body-parser').json({ limit: '50mb' }); -const path = require('path'); -const date = require('date-and-time'); -const { ensureDirSync, writeJsonSync } = require('fs-extra'); - -let outputDir; - -function reportViolations(req, res) { - const REPORT_TIMESTAMP = date.format(new Date(), 'YYYY-MM-DD-HH_mm_ss'); - let outputPath = path.resolve( - path.join(outputDir, `${REPORT_TIMESTAMP}.json`) - ); - - writeJsonSync(outputPath, req.body); - - res.send({ - outputPath, - }); -} - -function logError(err, req, res, next) { - console.error(err.stack); - next(err); -} - -function setupMiddleware(app, options) { - outputDir = path.join(options.root, 'ember-a11y-report'); - - ensureDirSync(outputDir); - - app.post( - '/report-violations', - bodyParser, - (req, res) => { - reportViolations(req, res); - }, - logError - ); -} - -module.exports = setupMiddleware; diff --git a/yarn.lock b/yarn.lock index 9094b2cd..94444e7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1506,6 +1506,16 @@ dependencies: "@octokit/openapi-types" "^8.1.4" +"@scalvert/ember-setup-middleware-reporter@^0.1.1": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@scalvert/ember-setup-middleware-reporter/-/ember-setup-middleware-reporter-0.1.1.tgz#bdd74c19d99feeef8807dea9c9ee2d272b6c1923" + integrity sha512-C5DHU6YlKaISB5utGQ+jpsMB57ZtY0uZ8UkD29j855BjqG6eJ98lhA2h/BoJbyPw89RKLP1EEXroy9+5JPoyVw== + dependencies: + "@types/fs-extra" "^9.0.12" + body-parser "^1.19.0" + errorhandler "^1.5.1" + fs-extra "^10.0.0" + "@simple-dom/interface@^1.4.0": version "1.4.0" resolved "https://registry.yarnpkg.com/@simple-dom/interface/-/interface-1.4.0.tgz#e8feea579232017f89b0138e2726facda6fbb71f" @@ -1835,6 +1845,13 @@ dependencies: "@types/node" "*" +"@types/fs-extra@^9.0.12": + version "9.0.12" + resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.12.tgz#9b8f27973df8a7a3920e8461517ebf8a7d4fdfaf" + integrity sha512-I+bsBr67CurCGnSenZZ7v94gd3tc3+Aj2taxMT4yu4ABLuOgOjeFxX3dokG24ztSRg5tnT00sL8BszO7gSMoIw== + dependencies: + "@types/node" "*" + "@types/glob@*", "@types/glob@^7.1.1": version "7.1.3" resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183" @@ -6446,6 +6463,14 @@ error@^7.0.0: dependencies: string-template "~0.2.1" +errorhandler@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/errorhandler/-/errorhandler-1.5.1.tgz#b9ba5d17cf90744cd1e851357a6e75bf806a9a91" + integrity sha512-rcOwbfvP1WTViVoUjcfZicVzjhjTuhSMntHh6mW3IrEiyE6mJyXvsToJUJGlGlw/2xU9P5whlWNGlIDVeCiT4A== + dependencies: + accepts "~1.3.7" + escape-html "~1.0.3" + es-abstract@^1.17.0-next.1, es-abstract@^1.17.5: version "1.17.6" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.17.6.tgz#9142071707857b2cacc7b89ecb670316c3e2d52a"