Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ module.exports = function compile(options) {
var modelInstructions = buildAllModelInstructions(
modelsRootDir, modelsConfig, modelSources);

var afterBoot = options.afterBoot || function() {};
assertIsValidAfterBoot(afterBoot);

// When executor passes the instruction to loopback methods,
// loopback modifies the data. Since we are loading the data using `require`,
// such change affects also code that calls `require` for the same file.
Expand All @@ -66,10 +69,16 @@ module.exports = function compile(options) {
models: modelInstructions,
files: {
boot: bootScripts
}
},
afterBoot: afterBoot
});
};

function assertIsValidAfterBoot(cb) {
assert(cb && typeof cb === 'function',
'afterBoot callback must be a function');
}

function assertIsValidConfig(name, config) {
if(config) {
assert(typeof config === 'object',
Expand Down
27 changes: 19 additions & 8 deletions lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,24 @@ function forEachKeyedObject(obj, fn) {
});
}

function runScripts(app, list) {
if (!list || !list.length) return;
list.forEach(function(filepath) {
var exports = tryRequire(filepath);
if (typeof exports === 'function')
exports(app);
});
function runScripts(app, list, idx, callback) {
if (!list || !list.length) return;

for (idx; idx<list.length; idx ++){
var exports = tryRequire(list[idx]);
if (typeof exports === 'function'){
if (exports.waitForCallback && exports.waitForCallback === true) {
exports(app, function() { runScripts(app, list, idx + 1, callback); });
return;
}
else {
exports(app);
}
}
}
if (callback && typeof callback === 'function'){
callback();
}
}

function tryRequire(modulePath) {
Expand All @@ -199,7 +210,7 @@ function tryRequire(modulePath) {
}

function runBootScripts(app, instructions) {
runScripts(app, instructions.files.boot);
runScripts(app, instructions.files.boot, 0, instructions.afterBoot);
}

function enableAnonymousSwagger(app, instructions) {
Expand Down