From 25987b6078d25a648ac9d0939a8761c1fe1a21ae Mon Sep 17 00:00:00 2001 From: Mike Pirog Date: Tue, 21 Sep 2021 11:57:36 -0400 Subject: [PATCH] #25: Some useful but bad code --- package.json | 16 +++++++- src/commands/bye.js | 22 ++++++++++ src/commands/hello.js | 1 + src/hooks/cnt.js | 3 ++ src/hooks/init.js | 95 +++++++++++++++++++++++++++++++++++++++++++ src/hooks/postrun.js | 3 ++ src/hooks/prerun.js | 11 +++++ src/more/bye.js | 23 +++++++++++ 8 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 src/commands/bye.js create mode 100644 src/hooks/cnt.js create mode 100644 src/hooks/init.js create mode 100644 src/hooks/postrun.js create mode 100644 src/hooks/prerun.js create mode 100644 src/more/bye.js diff --git a/package.json b/package.json index 13c5166..20bd84c 100644 --- a/package.json +++ b/package.json @@ -48,8 +48,22 @@ "license": "GPL-3.0", "main": "src/index.js", "oclif": { - "commands": "./src/commands", "bin": "hyperdrive", + "commands": "./src/commands", + "hooks": { + "init": [ + "./src/hooks/init.js" + ], + "prerun": [ + "./src/hooks/prerun.js" + ], + "postrun": [ + "./src/hooks/postrun.js" + ], + "command_not_found": [ + "./src/hooks/cnt.js" + ] + }, "plugins": [ "@oclif/plugin-help" ] diff --git a/src/commands/bye.js b/src/commands/bye.js new file mode 100644 index 0000000..eebc217 --- /dev/null +++ b/src/commands/bye.js @@ -0,0 +1,22 @@ +const {Command, flags} = require('@oclif/command'); + +class GoodbyeCommand extends Command { + async run() { + const {flags} = this.parse(GoodbyeCommand); + const name = flags.name || 'world'; + console.log(flags); + this.log(`goodbye ${name} from ./src/commands/hello.js`); + } +} +GoodbyeCommand.description = `awsgwag the command here +... +Extra documentation goes here +`; + +GoodbyeCommand.flags = { + name: flags.string({char: 'q', description: 'name to print'}), +}; + +GoodbyeCommand.hidden = true; + +module.exports = GoodbyeCommand; diff --git a/src/commands/hello.js b/src/commands/hello.js index 40caddc..1417057 100644 --- a/src/commands/hello.js +++ b/src/commands/hello.js @@ -4,6 +4,7 @@ class HelloCommand extends Command { async run() { const {flags} = this.parse(HelloCommand); const name = flags.name || 'world'; + console.log(flags); this.log(`hello ${name} from ./src/commands/hello.js`); } } diff --git a/src/hooks/cnt.js b/src/hooks/cnt.js new file mode 100644 index 0000000..3bdf14b --- /dev/null +++ b/src/hooks/cnt.js @@ -0,0 +1,3 @@ +module.exports = async options => { + console.log(`example cnt hook running before ${options.id}`) +} diff --git a/src/hooks/init.js b/src/hooks/init.js new file mode 100644 index 0000000..130c934 --- /dev/null +++ b/src/hooks/init.js @@ -0,0 +1,95 @@ +const { Command } = require('@oclif/config'); +const {flags} = require('@oclif/command'); +const Config = require('@oclif/config'); + +class DynamicPlugin extends Config.Plugin { + get hooks() { return {} } + get topics() { + return [] + } + get commandIDs() { + return ['mydynamiccommand'] + } + + get commands() { + const cmd = require('./../more/bye'); + cmd.id = 'bye'; + cmd.load = () => cmd; + return [cmd]; + } +} + +/* + * New plugin types: + * HyperdrivePlugin extends Config.Plugin + * 1. accepts a list of commands and an optional selector function for a "parent" + * + * +*/ + +module.exports = async (options) => { + // commands = [require('./../more/bye')]; + // config.plugins.push(new DynamicPlugin(config)) + // console.log(config.plugins); + // config.plugins[0].commands[0].flags.stuff = flags.string({char: 'z', description: 'name to print'}); + console.log(options); // {id, argv, conf} + + // Set DEBUG=* when -vvv is set? + + // Load in bootstrap config from configDir + /* + bootstrap: + bootstrapper: ./lib/bootstrap.js + envPrefix: + - HYPERDRIVE_ + configSources: + - config.yml + mode: 'cli', + packaged: _.has(process, 'pkg'), + + channel: stable? + leia: _.has(process, 'env.LEIA_PARSER_RUNNING') + pluginDirs: _.compact(pluginDirs.concat(process.landoAppPluginDirs)) + plugins: , + product: 'lando', + userAgent: `Lando/${version}`, + + // + channel: 'stable', + landoFile: '.lando.yml', + logLevelConsole: (this.argv().verbose) ? this.argv().verbose + 1 : this.logLevel, + logDir: path.join(this.userConfRoot, 'logs'), + mode: 'cli', + packaged: _.has(process, 'pkg'), + pluginDirs: _.compact(pluginDirs.concat(process.landoAppPluginDirs)), + preLandoFiles: ['.lando.base.yml', '.lando.dist.yml', '.lando.upstream.yml'], + postLandoFiles: ['.lando.local.yml'], + userConfRoot: this.userConfRoot, + version, + + */ + + // run bootstrap + // 1. merge in more config + // 2. go through plugins and build manifest of components/config/whatever + // 3. traverse plugins to find commands + // 4. what do commandIDs do? + // 5. install defaults eg desktop -> lando-desktop + /* + hyperdrive: + // list of installers + installers: + + // Just OCLIF command objects, this is just a list of metadata + commands: + - {id: 'install', variant: 'lando-docker-engine', path: } + + plugins: + - pathtofunction -> gets config and returns plugin + + // Final mods to commands, useful to add more options/args etc + mods: (?) + - {id: 'install', path: } + + */ +} diff --git a/src/hooks/postrun.js b/src/hooks/postrun.js new file mode 100644 index 0000000..ea9aeca --- /dev/null +++ b/src/hooks/postrun.js @@ -0,0 +1,3 @@ +module.exports = async options => { + console.log(`example postrun hook running before ${options.id}`) +} diff --git a/src/hooks/prerun.js b/src/hooks/prerun.js new file mode 100644 index 0000000..ad8581c --- /dev/null +++ b/src/hooks/prerun.js @@ -0,0 +1,11 @@ +const _ = require('lodash'); +const {flags} = require('@oclif/command'); + +module.exports = async options => { + // options.Command = require('./../commands/hello2'); + // console.log(options); + // options.Command.flags.name2 = flags.string({char: 'z', description: 'name to print'}), + + + console.log(`example prerun hook running before ${options.id}`) +} diff --git a/src/more/bye.js b/src/more/bye.js new file mode 100644 index 0000000..1d7c837 --- /dev/null +++ b/src/more/bye.js @@ -0,0 +1,23 @@ +const {Command, flags} = require('@oclif/command'); + +class GoodbyeCommand extends Command { + async run() { + const {flags} = this.parse(GoodbyeCommand); + const name = flags.name || 'world'; + console.log(flags); + this.log(`FU ${name} from ./src/commands/hello.js`); + } +} +GoodbyeCommand.name = 'bye'; +GoodbyeCommand.description = `awsgwag the command here +... +Extra documentation goes here +`; + +GoodbyeCommand.flags = { + name: flags.string({char: 'q', description: 'name to print'}), +}; + +GoodbyeCommand.hidden = false; + +module.exports = GoodbyeCommand;