-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from johnmanko/2.0.0
Updated library to work with Hapi v17
- Loading branch information
Showing
5 changed files
with
2,075 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,59 @@ | ||
// Load modules | ||
|
||
var Hoek = require('hoek'); | ||
var Joi = require('joi'); | ||
var Mongoose = require('mongoose'); | ||
var Async = require('async'); | ||
var Path = require('path'); | ||
var Glob = require('glob'); | ||
const Hoek = require('hoek'); | ||
const Joi = require('joi'); | ||
const Mongoose = require('mongoose'); | ||
const Path = require('path'); | ||
const globby = require('globby'); | ||
|
||
|
||
// Declare internals | ||
|
||
var internals = { | ||
schema: Joi.object({ | ||
globPattern: Joi.string().required(), | ||
globOptions: Joi.object({ | ||
cwd: Joi.string().required() | ||
}).unknown(true).required() | ||
}) | ||
}; | ||
|
||
|
||
exports.register = function (server, options, next) { | ||
|
||
options = Hoek.applyToDefaults({}, options); | ||
var results = Joi.validate(options, internals.schema); | ||
Hoek.assert(!results.error, results.error); | ||
var settings = results.value; | ||
|
||
Glob(settings.globPattern, settings.globOptions, function (err, files) { | ||
const internals = {}; | ||
|
||
Hoek.assert(!err, err); | ||
internals.schema = Joi.object({ | ||
globPattern: Joi.string().required(), | ||
globOptions: Joi.object({ | ||
cwd: Joi.string().required() | ||
}).unknown(true).required() | ||
}); | ||
|
||
Async.each(files, function (file, callback) { | ||
|
||
var path = Path.resolve(settings.globOptions.cwd, file); | ||
var module = require(path); | ||
var model = module; | ||
internals.pkg = require('../package.json'); | ||
|
||
internals.mapFileToPromise = (cwd, file, server) => { | ||
return new Promise((resolve) => { | ||
let path = Path.resolve(cwd, file); | ||
let module = require(path); | ||
let model = module; | ||
if (module.hasOwnProperty('load') && typeof(module.load) === 'function') { | ||
model = module.load(server); | ||
} | ||
|
||
if (model.schema instanceof Mongoose.Schema) { | ||
server.expose(model.modelName, model); | ||
server.log(['hapi-mongoose-models', 'plugin'], 'Model `' + model.modelName + '` loaded'); | ||
resolve(`Model '${model.modelName}' loaded`); | ||
} | ||
else { | ||
server.log(['hapi-mongoose-models', 'plugin'], 'File `' + file + '` not ' + | ||
'contains a mongoose model, will be ignored') | ||
resolve(`File '${file}' does not contains a mongoose model, will be ignored`); | ||
} | ||
|
||
return Hoek.nextTick(callback)(); | ||
}, next); | ||
|
||
}); | ||
}; | ||
|
||
|
||
exports.register.attributes = { | ||
pkg: require('../package.json') | ||
module.exports = { | ||
pkg: internals.pkg, | ||
register: async (server, options) => { | ||
options = Hoek.applyToDefaults({}, options); | ||
let results = Joi.validate(options, internals.schema); | ||
Hoek.assert(!results.error, results.error); | ||
let settings = results.value; | ||
return globby(settings.globPattern, settings.globOptions) | ||
.then((files) => { | ||
return Promise.all(files.map(file => internals.mapFileToPromise(settings.globOptions.cwd, file, server))); | ||
}).then((messages) => { | ||
messages.forEach(message => server.log([internals.pkg.name, 'plugin'], message)); | ||
}).catch((err) => { | ||
server.log([internals.pkg.name, 'plugin'], err); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.