mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-21 06:33:28 +00:00
Implement loadPluginsDirectory
This commit is contained in:
parent
1d18329c29
commit
1f4926bb6a
@ -4,6 +4,8 @@ path = require('path')
|
|||||||
fs = require('fs')
|
fs = require('fs')
|
||||||
resin = require('../resin')
|
resin = require('../resin')
|
||||||
|
|
||||||
|
PLUGINS_LOAD_PARALLEL_LIMIT = 5
|
||||||
|
|
||||||
exports.use = (plugin) ->
|
exports.use = (plugin) ->
|
||||||
if not _.isFunction(plugin)
|
if not _.isFunction(plugin)
|
||||||
throw new Error('Plugin should be a function')
|
throw new Error('Plugin should be a function')
|
||||||
@ -75,3 +77,26 @@ exports.readPluginsDirectory = (directory, callback) ->
|
|||||||
return callback(null, results)
|
return callback(null, results)
|
||||||
|
|
||||||
], callback)
|
], 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)
|
||||||
|
@ -156,3 +156,29 @@ describe 'Plugin Loader:', ->
|
|||||||
|
|
||||||
compareArrays(plugins, expectedPlugins)
|
compareArrays(plugins, expectedPlugins)
|
||||||
done()
|
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user