mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-07 19:40:47 +00:00
Implement pluginLoader.readPluginsDirectory()
This commit is contained in:
parent
4a75a12828
commit
8bf8cff436
@ -13,7 +13,7 @@ exports.use = (plugin) ->
|
|||||||
exports.loadPackage = (pluginPath, callback) ->
|
exports.loadPackage = (pluginPath, callback) ->
|
||||||
pluginPackageJSON = path.join(pluginPath, 'package.json')
|
pluginPackageJSON = path.join(pluginPath, 'package.json')
|
||||||
|
|
||||||
async.waterfall [
|
async.waterfall([
|
||||||
|
|
||||||
(callback) ->
|
(callback) ->
|
||||||
fs.exists pluginPackageJSON, (exists) ->
|
fs.exists pluginPackageJSON, (exists) ->
|
||||||
@ -53,4 +53,25 @@ exports.loadPackage = (pluginPath, callback) ->
|
|||||||
|
|
||||||
return callback(null, mainFile)
|
return callback(null, mainFile)
|
||||||
|
|
||||||
], callback
|
], callback)
|
||||||
|
|
||||||
|
isDirectory = (directory, callback) ->
|
||||||
|
fs.stat directory, (error, stats) ->
|
||||||
|
return callback(false) if error?
|
||||||
|
return callback(stats.isDirectory())
|
||||||
|
|
||||||
|
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, isDirectory, (results) ->
|
||||||
|
return callback(null, results)
|
||||||
|
|
||||||
|
], callback)
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
_ = require('lodash')
|
_ = require('lodash')
|
||||||
chai = require('chai')
|
chai = require('chai')
|
||||||
chai.use(require('sinon-chai'))
|
chai.use(require('sinon-chai'))
|
||||||
|
chai.use(require('chai-things'))
|
||||||
expect = chai.expect
|
expect = chai.expect
|
||||||
|
path = require('path')
|
||||||
sinon = require('sinon')
|
sinon = require('sinon')
|
||||||
mock = require('../../tests/utils/mock')
|
mock = require('../../tests/utils/mock')
|
||||||
resin = require('../resin')
|
resin = require('../resin')
|
||||||
@ -33,6 +35,24 @@ FILESYSTEM =
|
|||||||
main: 'app.js'
|
main: 'app.js'
|
||||||
'app.js': 'module.exports = {};'
|
'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:', ->
|
describe 'Plugin Loader:', ->
|
||||||
|
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@ -100,3 +120,39 @@ describe 'Plugin Loader:', ->
|
|||||||
expect(error).to.be.an.instanceof(Error)
|
expect(error).to.be.an.instanceof(Error)
|
||||||
expect(plugin).to.not.exist
|
expect(plugin).to.not.exist
|
||||||
done()
|
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()
|
||||||
|
@ -13,6 +13,8 @@ config =
|
|||||||
dataPrefix: path.join(process.env.HOME, '.resin')
|
dataPrefix: path.join(process.env.HOME, '.resin')
|
||||||
sshKeyWidth: 43
|
sshKeyWidth: 43
|
||||||
|
|
||||||
|
config.pluginsDirectory = path.join(config.dataPrefix, 'plugins')
|
||||||
|
|
||||||
config.urls =
|
config.urls =
|
||||||
signup: '/signup'
|
signup: '/signup'
|
||||||
preferences: '/preferences'
|
preferences: '/preferences'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user