Skip to content

throw err if dependency not listed #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,35 @@ module.exports = async (server, config, log, requireCwd) => {
// use deptree to resolve the order in which the plugins load
// based on their _dependencies:
const deptree = new Deptree();
// use these to guarantee all deps are present:
let dependencies = [];
const names = [];
pluginArr.forEach((plugin) => {
// priority is deprecated:
if (plugin._priority) {
throw new Error(`"_priority" field used by ${plugin._name} is deprecated, please migrate your plugins to use the _dependencies format`);
}
if (plugin._dependencies) {
dependencies = dependencies.concat(plugin._dependencies);
}
names.push(plugin._name);
deptree.add(plugin._name, plugin._dependencies ? plugin._dependencies : []);
});
// ensure all dependencies exist:
for (let i = 0; i < dependencies.length; i++) {
if (!names.includes(dependencies[i])) {
throw new Error(`Missing plugin dependency ${dependencies[i]}`);
}
}
pluginArr = deptree.resolve().reduce((memo, pluginName) => {
for (let i = 0; i < pluginArr.length; i++) {
if (pluginName === pluginArr[i]._name) {
memo.push(pluginArr[i]);
const plugin = pluginArr[i];
if (pluginName === plugin._name) {
memo.push(plugin);
return memo;
}
}
return memo;
}, []);

/* eslint-disable no-await-in-loop */
Expand Down
6 changes: 6 additions & 0 deletions test/brokenDependency/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
order:
- 'first'
plugins:
'./test/plugins/loadMeFirst.js':
_dependencies:
- './test/plugins/bigfoot.js'
10 changes: 10 additions & 0 deletions test/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ lab.test('error for circular plugin dependencies ', async() => {
lab.fail();
});

lab.test('error for broken plugin dependencies ', async() => {
try {
await hapiconfi(Hapi, { configPath: `${__dirname}/brokenDependency` });
} catch (e) {
code.expect(e.toString()).to.include('Missing plugin dependency ./test/plugins/bigfoot.js');
return;
}
lab.fail();
});

lab.test('will throw an error if deprecated _priority field still used', async() => {
try {
await hapiconfi(Hapi, { configPath: `${__dirname}/deprecated` });
Expand Down