forked from twilio/twilio-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prompt to install known plugin when command is not found (twili…
…o#191) Added a known plugin-command list which enables installing a plugin when a command is attempted and the plugin is not yet installed.
- Loading branch information
childish-sambino
committed
Jun 9, 2020
1 parent
bd8f030
commit f64acf5
Showing
5 changed files
with
177 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 installPlugin = async (config, pluginName) => { | ||
const plugins = new Plugins(config); | ||
|
||
await config.runHook('plugins:preinstall', { plugin: { name: pluginName } }); | ||
|
||
return plugins.install(pluginName); | ||
}; | ||
|
||
module.exports = async function (options) { | ||
const pluginName = getSupportedPlugin(options.id); | ||
|
||
if (pluginName && !isPluginInstalled(options.config, pluginName)) { | ||
logger.warn(`Plugin ${pluginName} not installed for command "${options.id}"`); | ||
|
||
const prompt = require('inquirer').prompt; | ||
const response = await prompt([{ | ||
type: 'confirm', | ||
name: 'install', | ||
message: 'Would you like to install the plugin?', | ||
default: false | ||
}]); | ||
if (response.install) { | ||
logger.warn(`Installing plugin ${pluginName} ...`); | ||
|
||
await installPlugin(options.config, pluginName); | ||
|
||
logger.warn(`Installed plugin ${pluginName}"!`); | ||
logger.warn('Please try running the command again'); | ||
this.exit(0); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const sinon = require('sinon'); | ||
const inquirer = require('inquirer'); | ||
const proxyquire = require('proxyquire'); | ||
const { expect, test } = require('@twilio/cli-test'); | ||
|
||
function PluginsMock() { | ||
this.install = sinon.stub(); | ||
} | ||
|
||
const hookFunc = proxyquire('../../src/hooks/command-not-found', { | ||
'@oclif/plugin-plugins': { default: PluginsMock } | ||
}); | ||
|
||
const config = { | ||
plugins: [{ name: '@twilio-labs/plugin-watch' }], | ||
runHook: sinon.stub() | ||
}; | ||
|
||
describe('hooks', () => { | ||
describe('command-not-found', () => { | ||
before(() => { | ||
inquirer._prompt = inquirer.prompt; | ||
inquirer.prompt = sinon.stub().resolves({ install: true }); | ||
}); | ||
|
||
after(() => { | ||
inquirer.prompt = inquirer._prompt; | ||
delete inquirer._prompt; | ||
}); | ||
|
||
test.stderr().it('warns and installs non-installed plugin topics', async ctx => { | ||
ctx.exit = sinon.stub().resolves(1); | ||
await hookFunc.call(ctx, { id: 'serverless', config }); | ||
expect(ctx.stderr).to.contain('not installed'); | ||
}); | ||
|
||
test.stderr().it('warns and install non-installed plugins commands', async ctx => { | ||
ctx.exit = sinon.stub().resolves(1); | ||
await hookFunc.call(ctx, { id: 'serverless:init', config }); | ||
expect(ctx.stderr).to.contain('not installed'); | ||
}); | ||
|
||
/* eslint-disable no-unused-expressions */ | ||
test.stderr().it('does nothing for installed plugins or unknown commands', async ctx => { | ||
await hookFunc.call(ctx, { id: 'watch', config }); | ||
expect(ctx.stderr).to.be.empty; | ||
await hookFunc.call(ctx, { id: 'serverless-schmerverless', config }); | ||
expect(ctx.stderr).to.be.empty; | ||
}); | ||
}); | ||
}); |