Skip to content

Commit

Permalink
Merge pull request #1194 from ricardo-devis-agullo/available-plugins
Browse files Browse the repository at this point in the history
Available plugins
  • Loading branch information
ricardo-devis-agullo authored Aug 25, 2021
2 parents 788b821 + ceadabf commit 932f47f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/registry/domain/plugins-initialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ module.exports.init = function(pluginsToRegister, callback) {
plugin.register.register(plugin.options || {}, dependencies, err => {
const pluginCallback = plugin.callback || _.noop;
pluginCallback(err);
// Overriding toString so implementation details of plugins do not
// leak to OC consumers
plugin.register.execute.toString = () => plugin.description || '';
registered[plugin.name] = plugin.register.execute;
done(err);
});
Expand Down
20 changes: 9 additions & 11 deletions src/registry/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const ComponentInfoRoute = require('./routes/component-info');
const ComponentPreviewRoute = require('./routes/component-preview');
const IndexRoute = require('./routes');
const PublishRoute = require('./routes/publish');
const settings = require('../resources/settings');
const StaticRedirectorRoute = require('./routes/static-redirector');
const PluginsRoute = require('./routes/plugins');
const settings = require('../resources/settings');

module.exports.create = function(app, conf, repository) {
const routes = {
Expand All @@ -19,7 +20,8 @@ module.exports.create = function(app, conf, repository) {
componentPreview: new ComponentPreviewRoute(conf, repository),
index: new IndexRoute(repository),
publish: new PublishRoute(repository),
staticRedirector: new StaticRedirectorRoute(repository)
staticRedirector: new StaticRedirectorRoute(repository),
plugins: new PluginsRoute(conf)
};

const prefix = conf.prefix;
Expand All @@ -32,11 +34,11 @@ module.exports.create = function(app, conf, repository) {
app.get(`${prefix}oc-client/client.js`, routes.staticRedirector);
app.get(`${prefix}oc-client/oc-client.min.map`, routes.staticRedirector);

app.get(`${prefix}~registry/plugins`, routes.plugins);

if (conf.local) {
app.get(
`${prefix}:componentName/:componentVersion/${
settings.registry.localStaticRedirectorPath
}*`,
`${prefix}:componentName/:componentVersion/${settings.registry.localStaticRedirectorPath}*`,
routes.staticRedirector
);
} else {
Expand All @@ -51,9 +53,7 @@ module.exports.create = function(app, conf, repository) {
app.post(prefix, routes.components);

app.get(
`${prefix}:componentName/:componentVersion${
settings.registry.componentInfoPath
}`,
`${prefix}:componentName/:componentVersion${settings.registry.componentInfoPath}`,
routes.componentInfo
);
app.get(
Expand All @@ -62,9 +62,7 @@ module.exports.create = function(app, conf, repository) {
);

app.get(
`${prefix}:componentName/:componentVersion${
settings.registry.componentPreviewPath
}`,
`${prefix}:componentName/:componentVersion${settings.registry.componentPreviewPath}`,
routes.componentPreview
);
app.get(
Expand Down
18 changes: 18 additions & 0 deletions src/registry/routes/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = function(conf) {
return function(req, res) {
if (conf.discovery) {
const plugins = Object.entries(conf.plugins).map(
([pluginName, pluginFn]) => ({
name: pluginName,
description: pluginFn.toString()
})
);

res.status(200).json(plugins);
} else {
res.status(401);
}
};
};
9 changes: 6 additions & 3 deletions src/registry/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = vm => {
dependencies: require('./partials/components-dependencies')(vm),
history: require('./partials/components-history')(vm),
list: require('./partials/components-list')(vm),
templates: require('./partials/components-templates')(vm)
templates: require('./partials/components-templates')(vm),
plugins: require('./partials/components-plugins')(vm)
};

const indexJS = require('./static/index');
Expand All @@ -20,7 +21,8 @@ module.exports = vm => {
const extraLinks =
` | <a href="#components-history" class="tab-link">History</a>` +
` | <a href="#components-templates" class="tab-link">Available templates</a>` +
` | <a href="#components-dependencies" class="tab-link">Available dependencies</a>`;
` | <a href="#components-dependencies" class="tab-link">Available dependencies</a>` +
` | <a href="#components-plugins" class="tab-link">Available plugins</a>`;

const registryType = isLocal ? 'Local dev registry' : 'On-line registry';

Expand All @@ -40,7 +42,8 @@ module.exports = vm => {
${tabs.list}
${tabs.history}
${tabs.templates}
${tabs.dependencies}`;
${tabs.dependencies}
${tabs.plugins}`;

const scripts = `<script>
var q = "${encodeURIComponent(vm.q)}", componentsList = ${JSON.stringify(
Expand Down
20 changes: 20 additions & 0 deletions src/registry/views/partials/components-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = vm => {
const pluginRow = ([
name,
description
]) => `<div class="componentRow row table">
<p class="release">
<span style="font-weight: bold">${name +
(description ? ':' : '')}</span>${description}
</p>
</div>
`;

const plugins = Object.entries(
vm.availablePlugins
).map(([pluginName, fn]) => [pluginName, fn.toString()]);

return `<div id="components-plugins" class="box">${
plugins.length ? plugins.map(pluginRow).join('') : 'No plugins registered'
}</div>`;
};
6 changes: 6 additions & 0 deletions test/unit/registry-domain-plugins-initialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('registry : domain : plugins-initialiser', () => {
const plugins = [
{
name: 'getValue',
description: 'Function description',
register: {
register: (options, deps, cb) => {
passedOptions = options;
Expand Down Expand Up @@ -142,6 +143,11 @@ describe('registry : domain : plugins-initialiser', () => {
expect(result.isFlagged).to.be.a('function');
});

it('should expose descriptions on the plugin functions if defined', () => {
expect(result.getValue.toString()).to.equal('Function description');
expect(result.isFlagged.toString()).to.equal('');
});

it('should be make the functionality usable', () => {
const a = result.getValue('a');
const flagged = result.isFlagged();
Expand Down
59 changes: 59 additions & 0 deletions test/unit/registry-routes-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const expect = require('chai').expect;
const sinon = require('sinon');

describe('registry : routes : plugins', () => {
const PluginsRoute = require('../../src/registry/routes/plugins');
let resJsonStub, statusStub, pluginsRoute, plugins;

const initialise = function() {
resJsonStub = sinon.stub();
statusStub = sinon.stub().returns({ json: resJsonStub });
const plugin1 = () => {};
plugin1.toString = () => 'Description plugin 1';
const plugin2 = () => {};
plugin2.toString = () => '';
plugins = {
plugin1,
plugin2
};
};

describe('when is not discoverable', () => {
before(() => {
initialise();
const conf = {
plugins,
discovery: false
};
pluginsRoute = new PluginsRoute(conf);

pluginsRoute({ headers: {} }, { conf, status: statusStub });
});

it('should return 401 status code', () => {
expect(statusStub.args[0][0]).to.be.equal(401);
});
});

describe('when is discoverable', () => {
before(() => {
initialise();
const conf = {
plugins,
discovery: true
};
pluginsRoute = new PluginsRoute(conf);

pluginsRoute({ headers: {} }, { conf, status: statusStub });
});

it('should return the list of plugins', () => {
expect(resJsonStub.args[0][0]).to.eql([
{ name: 'plugin1', description: 'Description plugin 1' },
{ name: 'plugin2', description: '' }
]);
});
});
});

0 comments on commit 932f47f

Please sign in to comment.