Skip to content

Commit

Permalink
feat: add 'plugins:available' command that lists trusted, uninstalled…
Browse files Browse the repository at this point in the history
… plugins (twilio#193)

Fixes twilio#117
  • Loading branch information
childish-sambino committed Jun 15, 2020
1 parent 5b04c41 commit ef04bad
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 34 deletions.
16 changes: 16 additions & 0 deletions src/commands/plugins/available.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { BaseCommand } = require('@twilio/cli-core').baseCommands;
const { getAvailablePlugins } = require('../../services/plugins');

class PluginsAvailable extends BaseCommand {
async run() {
await super.run();

getAvailablePlugins(this.config)
.forEach(pluginName => this.logger.info(pluginName));
}
}

PluginsAvailable.description = 'list available plugins for installation';
PluginsAvailable.flags = null;

module.exports = PluginsAvailable;
20 changes: 1 addition & 19 deletions src/hooks/command-not-found.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
const Plugins = require('@oclif/plugin-plugins').default;
const { logger } = require('@twilio/cli-core').services.logging;

const PLUGIN_COMMANDS = {
'@twilio-labs/plugin-flex': ['flex'],
'@twilio-labs/plugin-rtc': ['rtc'],
'@twilio-labs/plugin-serverless': ['serverless'],
'@twilio-labs/plugin-token': ['token'],
'@twilio-labs/plugin-watch': ['watch'],
'@dabblelab/plugin-autopilot': ['autopilot']
};

const getSupportedPlugin = commandId => {
return Object.keys(PLUGIN_COMMANDS).find(plugin => {
return PLUGIN_COMMANDS[plugin].find(command => commandId === command || commandId.startsWith(command + ':'));
});
};

const isPluginInstalled = (config, pluginName) => {
return config.plugins.find(p => p.name === pluginName);
};
const { getSupportedPlugin, isPluginInstalled } = require('../services/plugins');

const installPlugin = async (config, pluginName) => {
const plugins = new Plugins(config);
Expand Down
17 changes: 2 additions & 15 deletions src/hooks/plugin-install.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
const { logger } = require('@twilio/cli-core').services.logging;
const ALLOWED_ORGS = [
'@twilio/',
'@twilio-labs/',
'@dabblelab/plugin-autopilot'
];

function isTwilioPlugin(options) {
if (options.plugin.name === undefined) {
return false;
}
return ALLOWED_ORGS.find(function (org) {
return options.plugin.name.startsWith(org);
});
}
const { isTwilioPlugin } = require('../services/plugins');

module.exports = async function (options) {
if (!isTwilioPlugin(options)) {
if (!isTwilioPlugin(options.plugin.name)) {
logger.warn('WARNING!!! You are attempting to install a plugin from an untrusted source.');
logger.warn('It could contain malicious software or in other ways compromise your system.');

Expand Down
36 changes: 36 additions & 0 deletions src/services/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const ALLOWED_ORGS = [
'@twilio/',
'@twilio-labs/',
'@dabblelab/plugin-autopilot'
];

const PLUGIN_COMMANDS = {
'@twilio-labs/plugin-flex': ['flex'],
'@twilio-labs/plugin-rtc': ['rtc'],
'@twilio-labs/plugin-serverless': ['serverless'],
'@twilio-labs/plugin-token': ['token'],
'@twilio-labs/plugin-watch': ['watch'],
'@dabblelab/plugin-autopilot': ['autopilot']
};

exports.isTwilioPlugin = pluginName => {
if (pluginName === undefined) {
return false;
}
return ALLOWED_ORGS.find(org => pluginName.startsWith(org));
};

exports.isPluginInstalled = (config, pluginName) => {
return config.plugins.find(p => p.name === pluginName);
};

exports.getSupportedPlugin = commandId => {
return Object.keys(PLUGIN_COMMANDS).find(plugin => {
return PLUGIN_COMMANDS[plugin].find(command => commandId === command || commandId.startsWith(command + ':'));
});
};

exports.getAvailablePlugins = config => {
return Object.keys(PLUGIN_COMMANDS)
.filter(pluginName => !exports.isPluginInstalled(config, pluginName));
};
24 changes: 24 additions & 0 deletions test/commands/plugins/available.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { expect, test } = require('@twilio/cli-test');
const PluginsAvailable = require('../../../src/commands/plugins/available');

describe('commands', () => {
describe('plugins', () => {
describe('available', () => {
const testConfig = test
.twilioCliEnv()
.stderr()
.do(ctx => {
ctx.fakeConfig.plugins = [{ name: '@twilio-labs/plugin-watch' }];
})
.twilioCommand(PluginsAvailable, []);

testConfig.it('should print out available plugins', ctx => {
expect(ctx.stderr).to.contain('@twilio-labs/plugin-serverless');
});

testConfig.it('should not print out installed plugins', ctx => {
expect(ctx.stderr).to.not.contain('@twilio-labs/plugin-watch');
});
});
});
});

0 comments on commit ef04bad

Please sign in to comment.