From f17ccf339b622c79859871e41c5d1f5061f449dc Mon Sep 17 00:00:00 2001 From: Mike Pirog Date: Fri, 24 Sep 2021 15:14:42 -0400 Subject: [PATCH] #25: Move command removal to LandoOclifPlugin --- bin/hyperdrive | 1 + cli/commands/config.js | 1 + cli/hooks/init.js | 50 +++++++++++++++++++----------------------- utils/plugin.js | 46 +++++++++++++++++++++++++++----------- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/bin/hyperdrive b/bin/hyperdrive index ba3d6f1..dccc63a 100755 --- a/bin/hyperdrive +++ b/bin/hyperdrive @@ -12,4 +12,5 @@ if (process.argv.slice(2).find(element => element === '--debug') === '--debug') require('@oclif/command') .run() .then(require('@oclif/command/flush')) +// eslint-disable-next-line node/no-unpublished-require .catch(require('@oclif/errors/handle')); diff --git a/cli/commands/config.js b/cli/commands/config.js index 2a223d1..d60d9f2 100644 --- a/cli/commands/config.js +++ b/cli/commands/config.js @@ -33,6 +33,7 @@ class UninstallCommand extends Command { const {flags} = this.parse(UninstallCommand); const name = flags.name || 'world'; this.log(`hello ${name} from ./src/commands/hello.js`); + // Instantiate hyperd } } diff --git a/cli/hooks/init.js b/cli/hooks/init.js index 74cdd67..f3cdab2 100644 --- a/cli/hooks/init.js +++ b/cli/hooks/init.js @@ -1,9 +1,6 @@ const createDebugger = require('./../../utils/debug'); const path = require('path'); -// @TODO: only load this if we need it -const LandoOclifPlugin = require('./../../utils/plugin'); - module.exports = async({id, argv, config}) => { let debug = createDebugger(config.dirname, 'hooks', 'init'); // Below is mostly just to DEBUG confirm we can get this far @@ -53,42 +50,39 @@ module.exports = async({id, argv, config}) => { // @TODO: move findPlugin to utils/utils.js? and eventually into @lando/oclifer? // @TODO: eventually we should make criteria able to match by more than just name/id - const findPlugin = (plugins = [], criteria) => plugins.find(({name}) => name === criteria); - // @TODO: remove command should eventually be a method on LandoOclifPlugin - const removeCommand = (plugin = {}, cmdId) => { - const commandIndex = plugin.commands.findIndex(({id}) => id === cmdId); - if (commandIndex === -1) { - debug('could not find a command called %s in plugin %s, doing nothing', cmdId, plugin.name); - return plugin.commands; - } - plugin.commands.splice(commandIndex, 1); - debug('removed command %s: plugin now has commands %o', cmdId, plugin.commands.map(command => command.id).join(', ')); - return plugin.commands; - }; + // const findPlugin = (plugins = [], criteria) => plugins.find(({name}) => name === criteria); + const findPluginIndex = (plugins = [], criteria) => plugins.findIndex(({name}) => name === criteria); - // @TODO: restantiate corePlugin as a ocliflandoplugin? + // @TODO: We should only load our plugin class and replace the core plugin if + // we have an id/argv combination that will require command removal and/or + // dynamically loading commands eg hyperdrive install docker-desktop + const LandoOclifPlugin = require('./../../utils/plugin'); + // Create a drop in replacement of the corePlugin using our extended plugin class and load it + // @NOTE: root probably will not be universally config.root + const newCorePlugin = new LandoOclifPlugin({type: 'hyperdrive', root: config.root}); + await newCorePlugin.load(); // if id-argv matches a signature then remove id and load up queuer // @NOTE: should this be both add and install? if (id === 'install' && argv[0] === 'docker-desktop') { - // Lets remove the add command - const corePlugin = findPlugin(config.plugins, '@lando/hyperdrive'); - // delete corePlugin.manifest.commands.add; - corePlugin.commands = removeCommand(corePlugin, 'install'); + // Remove the install command + newCorePlugin.commands = newCorePlugin.removeCommand('install'); // find the correct install plugin? - const installerPlugin = findPlugin(config.installers, 'docker-desktop'); - config.plugins.push(new LandoOclifPlugin(config, {id: 'install', path: installerPlugin.path})); + // const installerPlugin = findPlugin(config.installers, 'docker-desktop'); + // config.plugins.push(new LandoOclifPlugin(config, {id: 'install', path: installerPlugin.path})); } // if id-argv matches a signature then remove id and load up queuer // @NOTE: should this be both add and install? if (id === 'uninstall' && argv[0] === 'docker-desktop') { - // Lets remove the add command - const corePlugin = findPlugin(config.plugins, '@lando/hyperdrive'); - // delete corePlugin.manifest.commands.add; - corePlugin.commands = removeCommand(corePlugin, 'uninstall'); + // Lets remove the uninstall command + newCorePlugin.commands = newCorePlugin.removeCommand('uninstall'); // find the correct install plugin? - const uninstallerPlugin = findPlugin(config.uninstallers, 'docker-desktop'); - config.plugins.push(new LandoOclifPlugin(config, {id: 'uninstall', path: uninstallerPlugin.path})); + // const uninstallerPlugin = findPlugin(config.uninstallers, 'docker-desktop'); + // config.plugins.push(new LandoOclifPlugin(config, {id: 'uninstall', path: uninstallerPlugin.path})); } + + // Let's replace it with our extended plugin class + const corePluginIndex = findPluginIndex(config.plugins, '@lando/hyperdrive'); + config.plugins.splice(corePluginIndex, 1, newCorePlugin); }; diff --git a/utils/plugin.js b/utils/plugin.js index c1f2d4b..53739dc 100644 --- a/utils/plugin.js +++ b/utils/plugin.js @@ -1,30 +1,50 @@ const OclifPlugin = require('@oclif/config').Plugin; class LandoOclifPlugin extends OclifPlugin { - constructor(config, replace) { - super(config); - this.replace = replace; - } + // constructor(config) { + // super(config); + // // this.replace = replace; + // } - get hooks() { - return {}; - } + // @TODO: this should handle a string id or an array of ids to be removed + removeCommand(cmdId) { + // remove command logic here + // @NOTE: this is not namespaced in a useful way until load is run - get topics() { - return []; + const commandIndex = this.commands.findIndex(({id}) => id === cmdId); + if (commandIndex === -1) { + this._debug('could not find a command called %s in plugin %s, doing nothing', cmdId, this.name); + return this.commands; + } + this.commands.splice(commandIndex, 1); + this._debug('removed command %s: plugin now has commands %o', cmdId, this.commands.map(command => command.id)); + return this.commands; } - // @TODO: do we need this? - get commandIDs() { - return [this.replace.id]; - } + // @TODO: we need to make sure that when we reinstantiate @lando/hyperdrive that this + // gets the list of hooks correctly, otherwise is set to {} + + // get hooks() { + // return {}; + // } + + // get topics() { + // return []; + // } + + // // @TODO: do we need this? + // get commandIDs() { + // return [this.replace.id]; + // } + /* get commands() { const cmd = require(this.replace.path); cmd.id = this.replace.id; cmd.load = () => cmd; return [cmd]; } + */ } module.exports = LandoOclifPlugin;