|
// AUTHOR: Heath Morrison | <firstname>@govright.org | http://github.com/doublemarked |
|
// QUESTIONS? Write me or join us on Gitter: https://gitter.im/strongloop/loopback |
|
// |
|
// This boot script will load module components on app initialization. Stick this file in server/boot. |
|
// Components are looked for in the directory common/models/model-name. The script will load recursively any |
|
// script from that directory, passing it the Model handle as a parameter (just like model.js loads). |
|
// Conveniently, since this is occuring during the boot phase, those model scripts will execute at a later |
|
// phase than model.js, meaning PersistedModel is already initialized and all peer model handles are already usable. |
|
// Note - it's necessary for you to still maintain a model.js file for Loopback to load the model at all. |
|
|
|
module.exports = function(app) { |
|
Object.keys(app.models).forEach(function (key) { |
|
loadComponents(app.models[key]); |
|
}); |
|
}; |
|
|
|
function loadComponents(Model, options) { |
|
options = options || {}; |
|
var componentsPath = options.path || Model.settings.components || 'common/models/' + slugify(Model.modelName); |
|
|
|
try { |
|
recursiveRequire(appRoot + '/' + componentsPath); |
|
} catch (e) { |
|
// anything other than 'file not found' should be a hard fail |
|
if (e.code !== 'ENOENT') { |
|
console.error('Failure loading component path:', appRoot + '/' + componentsPath); |
|
process.exit(1); |
|
} |
|
} |
|
|
|
function recursiveRequire(sourcePath) { |
|
fs.readdirSync(sourcePath).forEach(function (f) { |
|
var filePath = sourcePath + '/' + f; |
|
if (fs.lstatSync(filePath).isDirectory()) { |
|
recursiveRequire(filePath); |
|
} else if (path.extname(f) === '.js') { |
|
require(filePath)(Model); |
|
} |
|
}); |
|
} |
|
} |
|
|
|
// Works for 99% of cases. For others, set explicit path via options/config. |
|
function slugify(name) { |
|
name = name.replace(/^[A-Z]+/, function (s) { return s.toLowerCase(); }); |
|
return name.replace(/[A-Z]/g, function (s) { return '-' + s.toLowerCase(); }); |
|
} |
ReferenceError: appRoot is not defined
Where should appRoot be defined?