From 1f4926bb6ad8b3b27f5bbc6b02c7c2e9dfc43fa6 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 28 Nov 2014 11:10:01 -0400 Subject: [PATCH] Implement loadPluginsDirectory --- lib/plugin-loader/plugin-loader.coffee | 25 ++++++++++++++++++++ lib/plugin-loader/plugin-loader.spec.coffee | 26 +++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/plugin-loader/plugin-loader.coffee b/lib/plugin-loader/plugin-loader.coffee index f1455fa6..5028561a 100644 --- a/lib/plugin-loader/plugin-loader.coffee +++ b/lib/plugin-loader/plugin-loader.coffee @@ -4,6 +4,8 @@ path = require('path') fs = require('fs') resin = require('../resin') +PLUGINS_LOAD_PARALLEL_LIMIT = 5 + exports.use = (plugin) -> if not _.isFunction(plugin) throw new Error('Plugin should be a function') @@ -75,3 +77,26 @@ exports.readPluginsDirectory = (directory, callback) -> return callback(null, results) ], callback) + +exports.loadPluginsDirectory = (directory, callback, limit) -> + + limit ?= PLUGINS_LOAD_PARALLEL_LIMIT + + async.waterfall([ + + (callback) -> + exports.readPluginsDirectory(directory, callback) + + (plugins, callback) -> + async.mapLimit(plugins, limit, exports.loadPlugin, callback) + + (loadedPlugins, callback) -> + for plugin in loadedPlugins + try + exports.use(plugin) + catch error + return callback(error) + + return callback() + + ], callback) diff --git a/lib/plugin-loader/plugin-loader.spec.coffee b/lib/plugin-loader/plugin-loader.spec.coffee index cc59ff4c..b8e4d130 100644 --- a/lib/plugin-loader/plugin-loader.spec.coffee +++ b/lib/plugin-loader/plugin-loader.spec.coffee @@ -156,3 +156,29 @@ describe 'Plugin Loader:', -> compareArrays(plugins, expectedPlugins) done() + + describe '#loadPluginsDirectory()', -> + + it 'should not return an error for all valid plugins', (done) -> + pluginsDirectory = FILESYSTEM.pluginsDirectory + pluginLoader.loadPluginsDirectory pluginsDirectory.name, (error) -> + expect(error).to.not.exist + done() + + it 'should call use for all plugins', (done) -> + pluginsDirectory = FILESYSTEM.pluginsDirectory + numberOfPlugins = _.keys(pluginsDirectory.contents).length + + mock.fs.restore() + useSpy = sinon.spy(pluginLoader, 'use') + mock.fs.init(FILESYSTEM) + + pluginLoader.loadPluginsDirectory pluginsDirectory.name, (error) -> + expect(error).to.not.exist + expect(useSpy.callCount).to.equal(numberOfPlugins) + + for arg in _.flatten(useSpy.args) + expect(_.isFunction(arg)).to.be.true + + useSpy.restore() + done()