Skip to content

Commit

Permalink
Upgrades HAPI to v17 and uses async/await (#68)
Browse files Browse the repository at this point in the history
* Started with async/await/promise and hapi v17 migration

* ESLint is needed

* Test against 8 and 10 at Travis

* Making tests async

* Tests should also be async

* Tests are finally starting to run and most of them pass

* Proper database fixture loading and response messages

* Take care of linting and mocha exiting when done

* Getting correct status codes when invalid id used

* Why you mix tabs and spaces?

* Fix merge issues

* Headers were handled twice

* Whitespace 🎅
  • Loading branch information
paazmaya authored and joeyciechanowicz committed May 22, 2019
1 parent 2f371fe commit 2d8a9a0
Show file tree
Hide file tree
Showing 25 changed files with 681 additions and 772 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const config = module.exports = require('pa11y-lint-config/eslint/es6');
const config = module.exports = require('pa11y-lint-config/eslint/es2017');

// NOTE: we have to override here because we're using
// a `for of` loop somewhere. Once this repo moves to
Expand Down
8 changes: 4 additions & 4 deletions Makefile.node
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,23 @@ test: test-unit-coverage verify-coverage test-integration
@$(TASK_DONE)

test-unit:
@if [ -d test/unit ]; then mocha test/unit --recursive && $(TASK_DONE); fi
@if [ -d test/unit ]; then mocha test/unit --exit --recursive && $(TASK_DONE); fi

test-unit-coverage:
@if [ -d test/unit ]; then \
if [ -x $(NPM_BIN)/nyc ]; then \
nyc --reporter=text --reporter=html $(NPM_BIN)/_mocha test/unit --recursive && $(TASK_DONE); \
nyc --reporter=text --reporter=html $(NPM_BIN)/_mocha test/unit --exit --recursive && $(TASK_DONE); \
else \
if [ -x $(NPM_BIN)/istanbul ]; then \
istanbul cover $(NPM_BIN)/_mocha -- test/unit --recursive && $(TASK_DONE); \
istanbul cover $(NPM_BIN)/_mocha -- test/unit --exit --recursive && $(TASK_DONE); \
else \
make test-unit; \
fi \
fi \
fi

test-integration:
@if [ -d test/integration ]; then mocha test/integration --timeout $(INTEGRATION_TIMEOUT) --slow $(INTEGRATION_SLOW) && $(TASK_DONE); fi
@if [ -d test/integration ]; then mocha test/integration --exit --timeout $(INTEGRATION_TIMEOUT) --slow $(INTEGRATION_SLOW) && $(TASK_DONE); fi


# Tooling tasks
Expand Down
20 changes: 10 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,25 @@
// along with Pa11y Webservice. If not, see <http://www.gnu.org/licenses/>.
'use strict';

var async = require('async');
var Hapi = require('hapi');
var MongoClient = require('mongodb').MongoClient;
const async = require('async');
const Hapi = require('hapi');
const MongoClient = require('mongodb').MongoClient;

module.exports = initApp;

// Initialise the application
function initApp(config, callback) {

var app = module.exports = {
server: new Hapi.Server(),
const app = module.exports = {
server: new Hapi.Server({
host: config.host,
port: config.port
}),
database: null,
model: {},
config: config
};

app.server.connection({
host: config.host,
port: config.port
});

async.series([

function(next) {
Expand Down Expand Up @@ -91,6 +89,8 @@ function initApp(config, callback) {
require('./route/tasks')(app);
require('./route/task')(app);
app.server.start(next);

console.log(`Server running at: ${app.server.info.uri}`);
}

], function(error) {
Expand Down
6 changes: 3 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// along with Pa11y Webservice. If not, see <http://www.gnu.org/licenses/>.
'use strict';

var fs = require('fs');
var jsonPath = './config/' + (process.env.NODE_ENV || 'development') + '.json';
const fs = require('fs');
const jsonPath = './config/' + (process.env.NODE_ENV || 'development') + '.json';

if (fs.existsSync(jsonPath)) {
var jsonConfig = require(jsonPath);
Expand All @@ -38,6 +38,6 @@ if (fs.existsSync(jsonPath)) {
}

function env(name, defaultValue) {
var value = process.env[name];
const value = process.env[name];
return (typeof value === 'string' ? value : defaultValue);
}
2 changes: 1 addition & 1 deletion data/fixture/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

// Clone the main config
var config = module.exports = JSON.parse(JSON.stringify(require('../../.eslintrc')));
const config = module.exports = JSON.parse(JSON.stringify(require('../../.eslintrc')));

// Disable max line length/statements
config.rules['max-len'] = 'off';
Expand Down
53 changes: 17 additions & 36 deletions data/fixture/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,36 @@
// along with Pa11y Webservice. If not, see <http://www.gnu.org/licenses/>.
'use strict';

var app = require('../../app');
var async = require('async');
const application = require('../../app');

module.exports = loadFixtures;

function loadFixtures(env, config, done) {
env = (env || 'development');
var fixtures = {
const fixtures = {
results: require('./' + env + '/results.js'),
tasks: require('./' + env + '/tasks.js')
};

config.dbOnly = true;

app(config, function(error, app) {
application(config, async function(error, app) {
if (error) {
done(error);
}
async.series([
clearDatabase.bind(null, app),
insertFixtures.bind(null, app, fixtures)
], function() {
app.db.close();
done();
});
// Clear existing content
await app.model.result.collection.remove();
await app.model.task.collection.remove();

// Insert new content
await Promise.all(fixtures.tasks.map(function(task) {
return app.model.task.create(task);
}));
await Promise.all(fixtures.results.map(function(result) {
return app.model.result.create(result);
}));

await app.db.close();
done();
});
}

function clearDatabase(app, done) {
async.parallel([
app.model.result.collection.remove.bind(app.model.result.collection),
app.model.task.collection.remove.bind(app.model.task.collection)
], done);
}

function insertFixtures(app, fixtures, done) {
async.series([

function(next) {
async.parallel(fixtures.tasks.map(function(task) {
return app.model.task.create.bind(null, task);
}), next);
},

function(next) {
async.parallel(fixtures.results.map(function(result) {
return app.model.result.create.bind(null, result);
}), next);
}

], done);
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// along with Pa11y Webservice. If not, see <http://www.gnu.org/licenses/>.
'use strict';

var chalk = require('chalk');
var config = require('./config');
const chalk = require('chalk');
const config = require('./config');

process.on('SIGINT', function() {
console.log('\nGracefully shutting down from SIGINT (Ctrl-C)');
Expand Down

0 comments on commit 2d8a9a0

Please sign in to comment.