Skip to content

Commit fa3c81b

Browse files
committed
Rewrite for Hapi 17
1 parent 537a4fe commit fa3c81b

File tree

7 files changed

+84
-137
lines changed

7 files changed

+84
-137
lines changed

.eslintrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@aptoma/eslint-config",
3+
"parserOptions": {
4+
"ecmaVersion": 2017
5+
},
6+
"env": {
7+
"node": true,
8+
"mocha": true,
9+
"es6": true
10+
}
11+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ npm-debug.log
33
coverage
44
config/config.json
55
config/config.test.json
6+
package-lock.json

.jshintrc

-49
This file was deleted.

.travis.yml

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
language: node_js
22

33
node_js:
4-
- 0.12
5-
- 4.0
6-
7-
addons:
8-
apt:
9-
sources:
10-
- ubuntu-toolchain-r-test
11-
packages:
12-
- g++-4.8
13-
14-
cache:
15-
directories:
16-
- node_modules
4+
- 8
175

186
script:
197
- npm run ci

index.js

+36-37
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
11
'use strict';
2+
const promisify = require('util').promisify;
3+
const fs = require('fs');
4+
const readFile = promisify(fs.readFile);
25

3-
var fs = require('fs');
6+
module.exports = {
7+
name: 'route-status',
8+
async register(server, options) {
9+
const data = {version: options.version};
10+
options.pre = options.pre || [];
411

5-
exports.register = function (plugin, options, next) {
6-
var data = {version: options.version};
7-
options.pre = options.pre || [];
8-
9-
if (!options.revisionFile) {
10-
return done(data);
11-
}
12+
if (!options.revisionFile) {
13+
return done(data);
14+
}
1215

13-
fs.exists(options.revisionFile, function (exists) {
14-
if (!exists) {
16+
const ex = await exists(options.revisionFile);
17+
if (!ex) {
1518
return done(data);
1619
}
1720

18-
fs.readFile(options.revisionFile, {encoding: 'utf-8'}, function (err, rev) {
19-
if (err) {
20-
return next(err);
21-
}
21+
const rev = await readFile(options.revisionFile, {encoding: 'utf-8'});
22+
data.revision = rev.trim();
2223

23-
data.revision = rev.trim();
24-
done(data);
25-
});
26-
});
27-
28-
function done(data) {
29-
plugin.route({
30-
method: 'GET',
31-
path: '/status',
32-
config: {
33-
auth: false,
34-
handler: function (req, reply) {
35-
reply(data);
36-
},
37-
pre: options.pre,
38-
description: 'Show status',
39-
tags: ['api']
40-
}
41-
});
24+
return done(data);
4225

43-
next();
26+
function done(data) {
27+
server.route({
28+
method: 'GET',
29+
path: '/status',
30+
config: {
31+
auth: false,
32+
handler() {
33+
return data;
34+
},
35+
pre: options.pre,
36+
description: 'Show status',
37+
tags: ['api']
38+
}
39+
});
40+
}
4441
}
4542
};
4643

47-
exports.register.attributes = {
48-
name: 'route-status'
49-
};
44+
const exists = (path) => new Promise((resolve) => {
45+
fs.exists(path, (exists) => {
46+
resolve(exists);
47+
});
48+
});

package.json

+19-17
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@
99
"url": "[email protected]:aptoma/hapi-route-status.git"
1010
},
1111
"scripts": {
12-
"test": "NODE_ENV=test jscs test/ *.js && jshint test/ *.js && istanbul cover -i '*.js' _mocha -- -u exports -R spec 'test/**/*.test.js'",
13-
"watch": "mocha --watch 'test/**/*.js' --timeout 500",
14-
"ci": "NODE_ENV=test npm test --coverage && istanbul report cobertura",
15-
"release": "npm test && release-it -n -i patch",
16-
"release:minor": "npm test && release-it -n -i minor",
17-
"release:major": "npm test && release-it -n -i major"
12+
"lint": "eslint --ext '.js' *.js",
13+
"watch": "mocha --watch 'test/**/*.js' '*.js' --timeout 500",
14+
"test": "npm run lint && NODE_ENV=test istanbul cover -i '*.js' _mocha -- -u exports -R spec --timeout 3000 'test/**/*.test.js'",
15+
"mocha-only-detect": "mocha-only-detector-glob **/*.test.js",
16+
"ci": "npm test --coverage && istanbul report cobertura",
17+
"changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:\" * %s\"",
18+
"release": "npm run ci && release-it -n -i patch",
19+
"release:minor": "npm run ci && release-it -n -i minor",
20+
"release:major": "npm run ci && release-it -n -i major"
21+
},
22+
"engines": {
23+
"node": ">=8.x.x"
1824
},
1925
"license": "UNLICENSED",
2026
"devDependencies": {
21-
"hapi": "^10.1.0",
22-
"istanbul": "^0.3.8",
23-
"jscs": "^1.11.3",
24-
"jshint": "^2.6.3",
25-
"mocha": "^2.2.1",
27+
"hapi": "^17.1.1",
28+
"istanbul": "^0.4.5",
29+
"mocha": "^4.0.1",
2630
"release-it": "^2.0.3",
27-
"should": "^7.0.1"
31+
"should": "^13.1.3"
2832
},
29-
"jscsConfig": {
30-
"preset": "yandex",
31-
"disallowMultipleVarDecl": "exceptUndefined",
32-
"maximumLineLength": null,
33-
"validateIndentation": "\t"
33+
"dependencies": {
34+
"@aptoma/eslint-config": "^7.0.1",
35+
"eslint": "^4.12.1"
3436
}
3537
}

test/index.test.js

+16-21
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
'use strict';
2-
var Hapi = require('hapi');
3-
var status = require('../');
4-
var should = require('should');
2+
const Hapi = require('hapi');
3+
const plugin = require('../');
4+
require('should');
55

6-
describe('Status Route Plugin', function () {
7-
var server = new Hapi.Server();
8-
server.connection();
6+
describe('Status Route Plugin', () => {
7+
const server = new Hapi.Server({debug: {request: '*'}});
98

10-
it('should successfully load', function (done) {
11-
server.register({
12-
register: status,
9+
10+
it('should successfully load', async () => {
11+
await server.register({
12+
plugin,
1313
options: {
1414
version: '1.0.0'
1515
}
16-
}, function (err) {
17-
should.not.exist(err);
18-
done();
1916
});
2017
});
2118

22-
it('should register routes', function () {
23-
var table = server.connections[0].table();
19+
it('should register routes', () => {
20+
const table = server.table();
2421
table.should.have.length(1);
2522
table[0].path.should.equal('/status');
2623
});
2724

28-
it('should return version', function (done) {
29-
var options = {
25+
it('should return version', async () => {
26+
const options = {
3027
method: 'GET',
3128
url: '/status'
3229
};
33-
server.inject(options, function (res) {
34-
res.statusCode.should.equal(200);
35-
res.result.should.have.property('version', '1.0.0');
36-
done();
37-
});
30+
const res = await server.inject(options);
31+
res.statusCode.should.equal(200);
32+
res.result.should.have.property('version', '1.0.0');
3833
});
3934
});

0 commit comments

Comments
 (0)