Skip to content

Commit

Permalink
#25: remove lodash dep from init. reorganize in prep for abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Sep 24, 2021
1 parent b24bf81 commit 222f8ce
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 67 deletions.
1 change: 0 additions & 1 deletion bin/hyperdrive
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

require('@oclif/command')
.run()
.then(require('@oclif/command/flush'))
Expand Down
10 changes: 5 additions & 5 deletions cli/commands/add.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const {Command} = require('@oclif/command');

class InstallCommand extends Command {
static description = 'install things';
class AddCommand extends Command {
static description = 'add things';

static usage = 'usage';

static aliases = ['install'];
// static aliases = ['install'];

static examples = [];

async run() {
const {flags} = this.parse(InstallCommand);
const {flags} = this.parse(AddCommand);
const name = flags.name || 'world';
this.log(`goodbye ${name} from ./src/commands/hello.js`);
}
}

module.exports = InstallCommand;
module.exports = AddCommand;
17 changes: 17 additions & 0 deletions cli/commands/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {Command} = require('@oclif/command');

class InstallCommand extends Command {
static description = 'install things';

static usage = 'usage';

static examples = [];

async run() {
const {flags} = this.parse(InstallCommand);
const name = flags.name || 'world';
this.log(`goodbye ${name} from ./src/commands/hello.js`);
}
}

module.exports = InstallCommand;
8 changes: 4 additions & 4 deletions cli/commands/remove.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {Command, flags} = require('@oclif/command');

class UninstallCommand extends Command {
class RemoveCommand extends Command {
// static _base = 'thing';
// static id = 'thing';
// static title = 'title';
Expand All @@ -15,7 +15,7 @@ class UninstallCommand extends Command {

static help = 'stuff';

static aliases = ['uninstall'];
// static aliases = ['uninstall'];

// static strict = false;
// static parse = true;
Expand All @@ -30,10 +30,10 @@ class UninstallCommand extends Command {
// static

async run() {
const {flags} = this.parse(UninstallCommand);
const {flags} = this.parse(RemoveCommand);
const name = flags.name || 'unin';
this.log(`erg ${name} from ./src/commands/hello.js`);
}
}

module.exports = UninstallCommand;
module.exports = RemoveCommand;
19 changes: 19 additions & 0 deletions cli/commands/uninstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {Command} = require('@oclif/command');

class UninstallCommand extends Command {
static description = 'install things';

static usage = 'usage';

// static aliases = ['install'];

static examples = [];

async run() {
const {flags} = this.parse(UninstallCommand);
const name = flags.name || 'world';
this.log(`goodbye ${name} from ./src/commands/hello.js`);
}
}

module.exports = UninstallCommand;
83 changes: 46 additions & 37 deletions cli/hooks/init.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
const _ = require('lodash'); // eslint-disable-line node/no-unpublished-require
const createDebugger = require('../../utils/debug');
const Config = require('@oclif/config');
const createDebugger = require('./../../utils/debug');
const path = require('path');

class DynamicPlugin extends Config.Plugin {
constructor(config, replace) {
super(config);
this.replace = replace;
}

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];
}
}
// @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');
// Check for --debug and internally set DEBUG=* if its set
// @TODO: we should make debug a string flag so it can work like setting DEBUG=something
// @TODO: should we handle --debug on our own or use something lightweight like minimist?
// @TODO: or maybe add this as a util?
// @TODO: this should go at the top of bin/hyperdrive so we get debug output before this point
// @TODO: we should give preference to process.env.DEBUG if its set.
if ([id, ...argv].find(element => element === '--debug') === '--debug') {
require('debug').enable('*'); // eslint-disable-line node/no-extraneous-require
}
// Below is mostly just to DEBUG confirm we get this far
// Below is mostly just to DEBUG confirm we can get this far
debug('cli init start with id=%s, argv=%O', id, argv);

// @TODOS:
Expand All @@ -55,30 +33,61 @@ module.exports = async({id, argv, config}) => {
* 10. move configurator into its own thing, move oclif helpers in their own thing?
*/

// handle argv aliases
// handle argv install aliases
// @TODO: eventually this should be powered by config metadata
// @TODO: where will that metadata go?
if ((id === 'install' || id === 'uninstall') && argv[0] === 'engine') {
argv[0] = 'docker-desktop';
}

// @TODO: Below should be loaded via Configurator when that is ready to go
// @TODO: When a plugins manifest data is loaded it should construct an id for each component which is
// pluginname/component so for below this would give something like hyperdrive-core/docker-desktop
config.installers = [
{name: 'docker-desktop', path: path.join(__dirname, '..', 'installers', 'docker-desktop.js')},
];
config.uninstallers = [
{name: 'docker-desktop', path: path.join(__dirname, '..', 'uninstallers', 'docker-desktop.js')},
];

// @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;
};

// @TODO: restantiate corePlugin as a ocliflandoplugin?

// 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 = _.find(config.plugins, {name: '@lando/hyperdrive'});
const corePlugin = findPlugin(config.plugins, '@lando/hyperdrive');
// delete corePlugin.manifest.commands.add;
_.remove(corePlugin.commands, {id: 'add'});
corePlugin.commands = removeCommand(corePlugin, 'install');
// find the correct install plugin?
config.plugins.push(new DynamicPlugin(config, {id: 'install', path: './../commands/install-docker-desktop.js'}));
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 = _.find(config.plugins, {name: '@lando/hyperdrive'});
const corePlugin = findPlugin(config.plugins, '@lando/hyperdrive');
// delete corePlugin.manifest.commands.add;
_.remove(corePlugin.commands, {id: 'remove'});
corePlugin.commands = removeCommand(corePlugin, 'uninstall');
// find the correct install plugin?
config.plugins.push(new DynamicPlugin(config, {id: 'uninstall', path: './../commands/uninstall-docker-desktop.js'}));
const uninstallerPlugin = findPlugin(config.uninstallers, 'docker-desktop');
config.plugins.push(new LandoOclifPlugin(config, {id: 'uninstall', path: uninstallerPlugin.path}));
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {Command, flags} = require('@oclif/command');

class InstallDockerDesktop extends Command {
// static _base = 'thing';
// static id = 'thing';
static id = 'install';
// static title = 'title';

static description = 'install docker desktop'
Expand All @@ -13,8 +13,6 @@ class InstallDockerDesktop extends Command {

static help = 'stuff';

static aliases = ['add'];

// static strict = false;
// static parse = true;
static flags = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UninstallDockerDesktop extends Command {

static help = 'stuff';

static aliases = ['remove'];
// static aliases = ['remove'];

// static strict = false;
// static parse = true;
Expand Down
35 changes: 19 additions & 16 deletions utils/plugin.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/*
* New plugin types:
* HyperdrivePlugin extends Config.Plugin
* 1. accepts a list of commands and an optional selector function for a "parent"
*
*
*/
/*
const Config = require('@oclif/config');
const OclifPlugin = require('@oclif/config').Plugin;

class LandoOclifPlugin extends OclifPlugin {
constructor(config, replace) {
super(config);
this.replace = replace;
}

get hooks() {
return {};
}

class DynamicPlugin extends Config.Plugin {
get hooks() { return {} }
get topics() {
return []
return [];
}

// @TODO: do we need this?
get commandIDs() {
return ['mydynamiccommand']
return [this.replace.id];
}

get commands() {
const cmd = require('../more/bye');
cmd.id = 'bye';
const cmd = require(this.replace.path);
cmd.id = this.replace.id;
cmd.load = () => cmd;
return [cmd];
}
}
*/

module.exports = LandoOclifPlugin;

0 comments on commit 222f8ce

Please # to comment.