mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-03-22 03:55:19 +00:00
Remove plugin-loader as we need to rethink the whole thing to make it Windows compatible
This commit is contained in:
parent
e6a982d92c
commit
95e3e7b8b7
@ -2,7 +2,6 @@ _ = require('lodash')
|
||||
resin = require('./resin')
|
||||
packageJSON = require('../package.json')
|
||||
actions = require('./actions')
|
||||
pluginLoader = require('./plugin-loader/plugin-loader')
|
||||
cli = require('./cli/cli')
|
||||
|
||||
cli.setVersion(packageJSON.version)
|
||||
|
@ -1,98 +0,0 @@
|
||||
_ = require('lodash')
|
||||
async = require('async')
|
||||
path = require('path')
|
||||
fs = require('fs')
|
||||
fsPlus = require('fs-plus')
|
||||
resin = require('../resin')
|
||||
|
||||
PLUGINS_LOAD_PARALLEL_LIMIT = 5
|
||||
|
||||
exports.use = (plugin) ->
|
||||
if not _.isFunction(plugin)
|
||||
throw new Error('Plugin should be a function')
|
||||
|
||||
plugin.call(null, resin)
|
||||
|
||||
exports.loadPlugin = (pluginPath, callback) ->
|
||||
pluginPackageJSON = path.join(pluginPath, 'package.json')
|
||||
|
||||
async.waterfall([
|
||||
|
||||
(callback) ->
|
||||
fs.exists pluginPackageJSON, (exists) ->
|
||||
error = new Error("#{pluginPackageJSON} doesn't exist")
|
||||
return callback(if not exists then error)
|
||||
|
||||
(callback) ->
|
||||
fs.stat(pluginPackageJSON, callback)
|
||||
|
||||
(stats, callback) ->
|
||||
error = new Error("#{pluginPackageJSON} is not a file")
|
||||
return callback(if not stats.isFile() then error)
|
||||
|
||||
(callback) ->
|
||||
try
|
||||
packageJSON = require(pluginPackageJSON)
|
||||
catch error
|
||||
return callback(error)
|
||||
|
||||
if not _.isObject(packageJSON)
|
||||
error = new Error('package.json is not a valid JSON file')
|
||||
return callback(error)
|
||||
|
||||
if not packageJSON.main?
|
||||
error = new Error('package.json is missing main')
|
||||
return callback(error)
|
||||
|
||||
mainFilePath = path.join(pluginPath, packageJSON.main)
|
||||
|
||||
try
|
||||
mainFile = require(mainFilePath)
|
||||
catch error
|
||||
return callback(error)
|
||||
|
||||
if not _.isFunction(mainFile)
|
||||
return callback(new Error('Entry point should be a function'))
|
||||
|
||||
return callback(null, mainFile)
|
||||
|
||||
], callback)
|
||||
|
||||
exports.readPluginsDirectory = (directory, callback) ->
|
||||
|
||||
async.waterfall([
|
||||
|
||||
(callback) ->
|
||||
fs.readdir(directory, callback)
|
||||
|
||||
(plugins, callback) ->
|
||||
fullPathPlugins = _.map plugins, (plugin) ->
|
||||
return path.join(directory, plugin)
|
||||
|
||||
async.filter fullPathPlugins, fsPlus.isDirectory, (results) ->
|
||||
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)
|
@ -1,184 +0,0 @@
|
||||
_ = require('lodash')
|
||||
chai = require('chai')
|
||||
chai.use(require('sinon-chai'))
|
||||
chai.use(require('chai-things'))
|
||||
expect = chai.expect
|
||||
path = require('path')
|
||||
sinon = require('sinon')
|
||||
mock = require('../../tests/utils/mock')
|
||||
resin = require('../resin')
|
||||
pluginLoader = require('../plugin-loader/plugin-loader')
|
||||
|
||||
FILESYSTEM =
|
||||
text:
|
||||
name: 'text'
|
||||
contents: 'Hello World'
|
||||
invalidPackage:
|
||||
name: 'invalidPackage'
|
||||
contents: {}
|
||||
invalidPackageWithPackageJSON:
|
||||
name: 'invalidPackageWithPackageJSON'
|
||||
contents:
|
||||
'package.json': ''
|
||||
validPackage:
|
||||
name: 'validPackage'
|
||||
contents:
|
||||
'package.json': JSON.stringify
|
||||
name: 'myPackage'
|
||||
main: 'app.js'
|
||||
'app.js': 'module.exports = function() {};'
|
||||
validPackageNoFunction:
|
||||
name: 'validPackageNoFunction'
|
||||
contents:
|
||||
'package.json': JSON.stringify
|
||||
name: 'myPackage'
|
||||
main: 'app.js'
|
||||
'app.js': 'module.exports = {};'
|
||||
|
||||
FILESYSTEM.pluginsDirectory =
|
||||
name: 'pluginsDirectory'
|
||||
contents:
|
||||
firstPlugin: FILESYSTEM.validPackage.contents
|
||||
secondPlugin: FILESYSTEM.validPackage.contents
|
||||
thirdPlugin: FILESYSTEM.validPackage.contents
|
||||
FILESYSTEM.invalidPluginsDirectory =
|
||||
name: 'invalidPluginsDirectory'
|
||||
contents:
|
||||
firstPlugin: FILESYSTEM.validPackage.contents
|
||||
secondPlugin: FILESYSTEM.validPackage.contents
|
||||
thirdPlugin: 'Hello World'
|
||||
|
||||
compareArrays = (arr1, arr2) ->
|
||||
expect(arr1.length).to.equal(arr2.length)
|
||||
for item in arr2
|
||||
expect(arr1).to.include(item)
|
||||
|
||||
describe 'Plugin Loader:', ->
|
||||
|
||||
beforeEach ->
|
||||
mock.fs.init(FILESYSTEM)
|
||||
|
||||
afterEach ->
|
||||
mock.fs.restore()
|
||||
|
||||
describe '#use()', ->
|
||||
|
||||
it 'should pass the resin object to the function', ->
|
||||
spy = sinon.spy()
|
||||
pluginLoader.use(spy)
|
||||
expect(spy).to.have.been.calledWith(resin)
|
||||
|
||||
it 'should throw an error if plugin is not a function', ->
|
||||
for nonFunction in [
|
||||
undefined
|
||||
null
|
||||
[ 1, 2, 3 ]
|
||||
123
|
||||
'Hello World'
|
||||
{ hello: 'world' }
|
||||
]
|
||||
func = _.partial(pluginLoader.use, nonFunction)
|
||||
expect(func).to.throw(Error)
|
||||
|
||||
describe '#loadPlugin()', ->
|
||||
|
||||
it 'should return an error if path doesn\'t exist', (done) ->
|
||||
pluginLoader.loadPlugin 'foobar', (error, plugin) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(plugin).to.not.exist
|
||||
done()
|
||||
|
||||
it 'should return an error if path is not a directory', (done) ->
|
||||
pluginLoader.loadPlugin FILESYSTEM.text.name, (error, plugin) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(plugin).to.not.exist
|
||||
done()
|
||||
|
||||
it 'should return an error if there is no package.json', (done) ->
|
||||
pluginLoader.loadPlugin FILESYSTEM.invalidPackage.name, (error, plugin) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(plugin).to.not.exist
|
||||
done()
|
||||
|
||||
it 'should return an error if package.json is missing main', (done) ->
|
||||
pluginPackage = FILESYSTEM.invalidPackageWithPackageJSON
|
||||
pluginLoader.loadPlugin pluginPackage.name, (error, plugin) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(plugin).to.not.exist
|
||||
done()
|
||||
|
||||
it 'should return the entry point if package is valid', (done) ->
|
||||
pluginPackage = FILESYSTEM.validPackage
|
||||
pluginLoader.loadPlugin pluginPackage.name, (error, plugin) ->
|
||||
expect(error).to.not.exist
|
||||
expect(_.isFunction(plugin)).to.be.true
|
||||
done()
|
||||
|
||||
it 'should return the entry point is not a function', (done) ->
|
||||
pluginPackage = FILESYSTEM.validPackageNoFunction
|
||||
pluginLoader.loadPlugin pluginPackage.name, (error, plugin) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(plugin).to.not.exist
|
||||
done()
|
||||
|
||||
describe '#readPluginsDirectory()', ->
|
||||
|
||||
it 'should fail if input is not a directory', (done) ->
|
||||
pluginsDirectory = FILESYSTEM.text.name
|
||||
pluginLoader.readPluginsDirectory pluginsDirectory, (error, plugins) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(plugins).to.not.exist
|
||||
done()
|
||||
|
||||
it 'should return a list of all files inside plugins directory', (done) ->
|
||||
pluginsDirectory = FILESYSTEM.pluginsDirectory
|
||||
pluginLoader.readPluginsDirectory pluginsDirectory.name, (error, plugins) ->
|
||||
expect(error).to.not.exist
|
||||
|
||||
expectedPlugins = _.keys(pluginsDirectory.contents)
|
||||
|
||||
expectedPlugins = _.map expectedPlugins, (value) ->
|
||||
return path.join(pluginsDirectory.name, value)
|
||||
|
||||
compareArrays(plugins, expectedPlugins)
|
||||
done()
|
||||
|
||||
it 'should return omit files inside plugins directory', (done) ->
|
||||
pluginsDirectory = FILESYSTEM.invalidPluginsDirectory
|
||||
pluginLoader.readPluginsDirectory pluginsDirectory.name, (error, plugins) ->
|
||||
expect(error).to.not.exist
|
||||
|
||||
expectedPlugins = _.keys _.omit pluginsDirectory.contents, (value, key) ->
|
||||
return _.isString(value)
|
||||
|
||||
expectedPlugins = _.map expectedPlugins, (value) ->
|
||||
return path.join(pluginsDirectory.name, value)
|
||||
|
||||
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()
|
Loading…
x
Reference in New Issue
Block a user