Skip to content

Commit e16cc87

Browse files
committed
#25: Move command removal to LandoOclifPlugin
1 parent 643e45e commit e16cc87

File tree

4 files changed

+57
-41
lines changed

4 files changed

+57
-41
lines changed

bin/hyperdrive

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ if (process.argv.slice(2).find(element => element === '--debug') === '--debug')
1212
require('@oclif/command')
1313
.run()
1414
.then(require('@oclif/command/flush'))
15+
// eslint-disable-next-line node/no-unpublished-require
1516
.catch(require('@oclif/errors/handle'));

cli/commands/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class UninstallCommand extends Command {
3333
const {flags} = this.parse(UninstallCommand);
3434
const name = flags.name || 'world';
3535
this.log(`hello ${name} from ./src/commands/hello.js`);
36+
// Instantiate hyperd
3637
}
3738
}
3839

cli/hooks/init.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const createDebugger = require('./../../utils/debug');
22
const path = require('path');
33

4-
// @TODO: only load this if we need it
5-
const LandoOclifPlugin = require('./../../utils/plugin');
6-
74
module.exports = async({id, argv, config}) => {
85
let debug = createDebugger(config.dirname, 'hooks', 'init');
96
// Below is mostly just to DEBUG confirm we can get this far
@@ -53,42 +50,39 @@ module.exports = async({id, argv, config}) => {
5350

5451
// @TODO: move findPlugin to utils/utils.js? and eventually into @lando/oclifer?
5552
// @TODO: eventually we should make criteria able to match by more than just name/id
56-
const findPlugin = (plugins = [], criteria) => plugins.find(({name}) => name === criteria);
57-
// @TODO: remove command should eventually be a method on LandoOclifPlugin
58-
const removeCommand = (plugin = {}, cmdId) => {
59-
const commandIndex = plugin.commands.findIndex(({id}) => id === cmdId);
60-
if (commandIndex === -1) {
61-
debug('could not find a command called %s in plugin %s, doing nothing', cmdId, plugin.name);
62-
return plugin.commands;
63-
}
64-
plugin.commands.splice(commandIndex, 1);
65-
debug('removed command %s: plugin now has commands %o', cmdId, plugin.commands.map(command => command.id).join(', '));
66-
return plugin.commands;
67-
};
53+
// const findPlugin = (plugins = [], criteria) => plugins.find(({name}) => name === criteria);
54+
const findPluginIndex = (plugins = [], criteria) => plugins.findIndex(({name}) => name === criteria);
6855

69-
// @TODO: restantiate corePlugin as a ocliflandoplugin?
56+
// @TODO: We should only load our plugin class and replace the core plugin if
57+
// we have an id/argv combination that will require command removal and/or
58+
// dynamically loading commands eg hyperdrive install docker-desktop
59+
const LandoOclifPlugin = require('./../../utils/plugin');
60+
// Create a drop in replacement of the corePlugin using our extended plugin class and load it
61+
// @NOTE: root probably will not be universally config.root
62+
const newCorePlugin = new LandoOclifPlugin({type: 'hyperdrive', root: config.root});
63+
await newCorePlugin.load();
7064

7165
// if id-argv matches a signature then remove id and load up queuer
7266
// @NOTE: should this be both add and install?
7367
if (id === 'install' && argv[0] === 'docker-desktop') {
74-
// Lets remove the add command
75-
const corePlugin = findPlugin(config.plugins, '@lando/hyperdrive');
76-
// delete corePlugin.manifest.commands.add;
77-
corePlugin.commands = removeCommand(corePlugin, 'install');
68+
// Remove the install command
69+
newCorePlugin.commands = newCorePlugin.removeCommand('install');
7870
// find the correct install plugin?
79-
const installerPlugin = findPlugin(config.installers, 'docker-desktop');
80-
config.plugins.push(new LandoOclifPlugin(config, {id: 'install', path: installerPlugin.path}));
71+
// const installerPlugin = findPlugin(config.installers, 'docker-desktop');
72+
// config.plugins.push(new LandoOclifPlugin(config, {id: 'install', path: installerPlugin.path}));
8173
}
8274

8375
// if id-argv matches a signature then remove id and load up queuer
8476
// @NOTE: should this be both add and install?
8577
if (id === 'uninstall' && argv[0] === 'docker-desktop') {
86-
// Lets remove the add command
87-
const corePlugin = findPlugin(config.plugins, '@lando/hyperdrive');
88-
// delete corePlugin.manifest.commands.add;
89-
corePlugin.commands = removeCommand(corePlugin, 'uninstall');
78+
// Lets remove the uninstall command
79+
newCorePlugin.commands = newCorePlugin.removeCommand('uninstall');
9080
// find the correct install plugin?
91-
const uninstallerPlugin = findPlugin(config.uninstallers, 'docker-desktop');
92-
config.plugins.push(new LandoOclifPlugin(config, {id: 'uninstall', path: uninstallerPlugin.path}));
81+
// const uninstallerPlugin = findPlugin(config.uninstallers, 'docker-desktop');
82+
// config.plugins.push(new LandoOclifPlugin(config, {id: 'uninstall', path: uninstallerPlugin.path}));
9383
}
84+
85+
// Let's replace it with our extended plugin class
86+
const corePluginIndex = findPluginIndex(config.plugins, '@lando/hyperdrive');
87+
config.plugins.splice(corePluginIndex, 1, newCorePlugin);
9488
};

utils/plugin.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
const OclifPlugin = require('@oclif/config').Plugin;
22

33
class LandoOclifPlugin extends OclifPlugin {
4-
constructor(config, replace) {
5-
super(config);
6-
this.replace = replace;
7-
}
4+
// constructor(config) {
5+
// super(config);
6+
// // this.replace = replace;
7+
// }
88

9-
get hooks() {
10-
return {};
11-
}
9+
// @TODO: this should handle a string id or an array of ids to be removed
10+
removeCommand(cmdId) {
11+
// remove command logic here
12+
// @NOTE: this is not namespaced in a useful way until load is run
1213

13-
get topics() {
14-
return [];
14+
const commandIndex = this.commands.findIndex(({id}) => id === cmdId);
15+
if (commandIndex === -1) {
16+
this._debug('could not find a command called %s in plugin %s, doing nothing', cmdId, this.name);
17+
return this.commands;
18+
}
19+
this.commands.splice(commandIndex, 1);
20+
this._debug('removed command %s: plugin now has commands %o', cmdId, this.commands.map(command => command.id));
21+
return this.commands;
1522
}
1623

17-
// @TODO: do we need this?
18-
get commandIDs() {
19-
return [this.replace.id];
20-
}
24+
// @TODO: we need to make sure that when we reinstantiate @lando/hyperdrive that this
25+
// gets the list of hooks correctly, otherwise is set to {}
26+
27+
// get hooks() {
28+
// return {};
29+
// }
30+
31+
// get topics() {
32+
// return [];
33+
// }
34+
35+
// // @TODO: do we need this?
36+
// get commandIDs() {
37+
// return [this.replace.id];
38+
// }
2139

40+
/*
2241
get commands() {
2342
const cmd = require(this.replace.path);
2443
cmd.id = this.replace.id;
2544
cmd.load = () => cmd;
2645
return [cmd];
2746
}
47+
*/
2848
}
2949

3050
module.exports = LandoOclifPlugin;

0 commit comments

Comments
 (0)