diff --git a/lib/plugins.js b/lib/plugins.js index f643918..48eb147 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -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 */ diff --git a/test/brokenDependency/default.yaml b/test/brokenDependency/default.yaml new file mode 100644 index 0000000..9cced78 --- /dev/null +++ b/test/brokenDependency/default.yaml @@ -0,0 +1,6 @@ +order: + - 'first' +plugins: + './test/plugins/loadMeFirst.js': + _dependencies: + - './test/plugins/bigfoot.js' diff --git a/test/server-test.js b/test/server-test.js index 83bf7b6..db41274 100644 --- a/test/server-test.js +++ b/test/server-test.js @@ -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` });