Skip to content

Commit

Permalink
Hapi v17 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine AFFOUARD committed Feb 7, 2018
1 parent 3c9c424 commit 0de89ef
Show file tree
Hide file tree
Showing 13 changed files with 5,512 additions and 212 deletions.
29 changes: 8 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const _ = require('lodash');
const async = require('async');
const hoek = require('hoek');
const utils = require('./utils');
const Package = require('../package');

const internals = {
defaultsOptions: {
Expand All @@ -12,25 +11,13 @@ const internals = {
}
};

exports.register = (server, options, next) => {
exports.register = async (server, options) => {
const opts = hoek.applyToDefaults(internals.defaultsOptions, options);

async.auto({
methods: callback => {
return utils.register('methods', opts.methods, server, callback);
},
handlers: ['methods', (data, callback) => {
return utils.register('handlers', opts.handlers, server, callback);
}],
routes: ['handlers', (data, callback) => {
return utils.register('routes', opts.routes, server, callback);
}],
decorators: ['methods', 'handlers', (stack, callback) => {
return utils.register('decorators', opts.decorators, server, callback);
}]
}, next);
await utils.register('methods', opts.methods, server);
await utils.register('handlers', opts.handlers, server);
await utils.register('routes', opts.routes, server);
await utils.register('decorators', opts.decorators, server);
};

exports.register.attributes = {
pkg: require('../package.json')
};
exports.name = Package.name;
exports.version = Package.version;
6 changes: 3 additions & 3 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const joi = require('joi');

exports.glob = joi.object().keys({
cwd: joi.string()
.default(process.cwd())
.label('directory')
.description('methods directory'),
.default(process.cwd())
.label('directory')
.description('methods directory'),
pattern: joi.string().default('*.js'),
ignore: joi.string()
});
Expand Down
83 changes: 31 additions & 52 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const joi = require('joi');
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const async = require('async');
const schemas = require('./schemas');

const internals = {};
Expand All @@ -12,28 +11,23 @@ const globOptions = joi.object().keys({
cwd: joi.string().required()
}).unknown();

internals.loadFilesFromDir = exports.loadFilesFromDir = (pattern, options, done) => {
async.auto({
glob: callback => joi.validate(options, globOptions, callback),
stats: ['glob', (stack, callback) => fs.stat(options.cwd, callback)],
directory: ['stats', (stack, callback) => {
if (!stack.stats.isDirectory()) {
return callback(new Error('options.cwd: must be a directory.'));
}

return callback(null, true);
}],
load: ['directory', (stack, callback) => glob(pattern, options, callback)],
files: ['load', (stack, callback) => {
const files = _.map(stack.load, file => `${options.cwd}/${file}`);
exports.loadFilesFromDir = async (pattern, options) => {
joi.validate(options, globOptions);
const stats = fs.statSync(options.cwd);
if (!stats.isDirectory()) {
throw Error('options.cwd: must be a directory.');
}
const stack = glob.sync(pattern, options);
const files = _.map(stack, file => `${options.cwd}/${file}`);

return callback(null, files);
}]
}, done);
return {files};
};
internals.loadFilesFromDir = exports.loadFilesFromDir;

internals.extractObjectsFromFiles = exports.extractObjectsFromFiles = (files, done) => {
async.map(files, (file, next) => {
exports.extractObjectsFromFiles = async files => {
let allObjects = [];

files.forEach(file => {
const fileName = path.basename(file).replace(path.extname(file), '');
const objects = _.map(require(file), (val, key) => {
const prefix = (val.prefix) ? val.prefix : fileName;
Expand All @@ -45,16 +39,19 @@ internals.extractObjectsFromFiles = exports.extractObjectsFromFiles = (files, do
});
});

return next(null, objects);
}, done);
allObjects.push(objects);
});

return allObjects;
};
internals.extractObjectsFromFiles = exports.extractObjectsFromFiles;

internals.apply = {
methods: (server, obj) => {
server.method(`${obj.prefix}.${obj.name}`, obj.method, obj.options);
},
handlers: (server, obj) => {
server.handler(_.camelCase(`${obj.prefix}_${obj.name}`), obj.method);
server.decorate('handler', _.camelCase(`${obj.prefix}_${obj.name}`), obj.method);
},
routes: (server, obj) => {
if (obj.method) {
Expand All @@ -69,35 +66,17 @@ internals.apply = {
}
};

exports.register = (type, glob, server, done) => {
async.auto({
glob: callback => joi.validate(glob, schemas.glob, callback),
load: ['glob', (data, callback) => {
const pattern = data.glob.pattern;
const globOptions = data.glob;

return internals.loadFilesFromDir(pattern, globOptions, callback);
}],
objects: ['load', (data, callback) => {
const files = data.load.files;

return internals.extractObjectsFromFiles(files, callback);
}],
validate: ['objects', (data, callback) => {
const objects = data.objects;

return async.map(_.flattenDeep(objects), (obj, next) => {
return joi.validate(obj, schemas[type], next);
}, callback);
}],
register: ['validate', (data, callback) => {
const objects = data.validate;
exports.register = async (type, glob, server) => {
const globData = joi.validate(glob, schemas.glob);
const loadData = await internals.loadFilesFromDir(globData.value.pattern, globData.value);
const objects = await internals.extractObjectsFromFiles(loadData.files);

return async.map(objects, (obj, next) => {
internals.apply[type](server, obj);
let validatedData = [];
_.flattenDeep(objects).forEach(obj => {
validatedData.push(joi.validate(obj, schemas[type]));
});

return next();
}, callback);
}]
}, done);
validatedData.forEach(obj => {
internals.apply[type](server, obj.value);
});
};
Loading

0 comments on commit 0de89ef

Please sign in to comment.