Skip to content

Commit

Permalink
#25: Move command removal to LandoOclifPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Sep 24, 2021
1 parent ff99de0 commit f17ccf3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
1 change: 1 addition & 0 deletions bin/hyperdrive
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
1 change: 1 addition & 0 deletions cli/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
50 changes: 22 additions & 28 deletions cli/hooks/init.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
};
46 changes: 33 additions & 13 deletions utils/plugin.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit f17ccf3

Please sign in to comment.