Skip to content

Commit db2820e

Browse files
committed
#25: Ministrapper for CLI bootstrap config
1 parent 6edd925 commit db2820e

File tree

16 files changed

+189
-122
lines changed

16 files changed

+189
-122
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "oclif",
33
"rules": {
4+
"no-console": ["warn"],
45
"semi": ["error", "always"],
56
"semi-style": ["error", "last"],
67
"space-before-function-paren": ["error", {

bin/hyperdrive.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@echo off
22

3-
node "%~dp0\run" %*
3+
node "%~dp0\hyperdrive" %*

cli/commands/bye.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class GoodbyeCommand extends Command {
44
async run() {
55
const {flags} = this.parse(GoodbyeCommand);
66
const name = flags.name || 'world';
7-
console.log(flags);
87
this.log(`goodbye ${name} from ./src/commands/hello.js`);
98
}
109
}

cli/commands/hello.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class HelloCommand extends Command {
44
async run() {
55
const {flags} = this.parse(HelloCommand);
66
const name = flags.name || 'world';
7-
console.log(flags);
87
this.log(`hello ${name} from ./src/commands/hello.js`);
98
}
109
}

cli/hooks/cnt.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
module.exports = async options => {
2-
console.log(`example cnt hook running before ${options.id}`)
3-
}
1+
2+
module.exports = async() => {};

cli/hooks/init.js

Lines changed: 62 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,62 @@
1-
const { Command } = require('@oclif/config');
2-
const {flags} = require('@oclif/command');
3-
const Config = require('@oclif/config');
4-
5-
class DynamicPlugin extends Config.Plugin {
6-
get hooks() { return {} }
7-
get topics() {
8-
return []
9-
}
10-
get commandIDs() {
11-
return ['mydynamiccommand']
12-
}
13-
14-
get commands() {
15-
const cmd = require('./../more/bye');
16-
cmd.id = 'bye';
17-
cmd.load = () => cmd;
18-
return [cmd];
19-
}
20-
}
21-
22-
/*
23-
* New plugin types:
24-
* HyperdrivePlugin extends Config.Plugin
25-
* 1. accepts a list of commands and an optional selector function for a "parent"
26-
*
27-
*
28-
*/
29-
30-
module.exports = async (options) => {
31-
// commands = [require('./../more/bye')];
32-
// config.plugins.push(new DynamicPlugin(config))
33-
// console.log(config.plugins);
34-
// config.plugins[0].commands[0].flags.stuff = flags.string({char: 'z', description: 'name to print'});
35-
console.log(options); // {id, argv, conf}
1+
'use strict';
362

37-
// Set DEBUG=* when -vvv is set?
3+
// @NOTE: We use LET so we can rename the debugger as needed
4+
const createDebugger = require('./../../lib/debug');
5+
const path = require('path');
6+
const Ministrapper = require('./../../lib/ministrapper');
387

39-
// Load in bootstrap config from configDir
40-
/*
41-
bootstrap:
42-
bootstrapper: ./lib/bootstrap.js
43-
envPrefix:
44-
- HYPERDRIVE_
45-
configSources:
46-
- config.yml
47-
mode: 'cli',
48-
packaged: _.has(process, 'pkg'),
49-
50-
channel: stable?
51-
leia: _.has(process, 'env.LEIA_PARSER_RUNNING')
52-
pluginDirs: _.compact(pluginDirs.concat(process.landoAppPluginDirs))
53-
plugins: ,
54-
product: 'lando',
55-
userAgent: `Lando/${version}`,
56-
57-
//
58-
channel: 'stable',
59-
landoFile: '.lando.yml',
60-
logLevelConsole: (this.argv().verbose) ? this.argv().verbose + 1 : this.logLevel,
61-
logDir: path.join(this.userConfRoot, 'logs'),
62-
mode: 'cli',
63-
packaged: _.has(process, 'pkg'),
64-
pluginDirs: _.compact(pluginDirs.concat(process.landoAppPluginDirs)),
65-
preLandoFiles: ['.lando.base.yml', '.lando.dist.yml', '.lando.upstream.yml'],
66-
postLandoFiles: ['.lando.local.yml'],
67-
userConfRoot: this.userConfRoot,
68-
version,
8+
module.exports = async({id, argv, config}) => {
9+
let debug = createDebugger(config.dirname, 'hooks', 'init');
10+
const sourceConfig = path.join(__dirname, '..', '..', 'config.yml');
11+
const userConfig = path.join(config.configDir, 'config.yml');
6912

70-
*/
13+
// @TODO: set this based on some options (--debug?). if boolean/null should set * if string should set as if DEBUG
14+
// envvar was set.
15+
// @NOTE: this shows all debug right now for dev purposes. see @TODO above.
16+
require('debug').enable('*'); // eslint-disable-line node/no-extraneous-require
17+
debug('cli init start with id=%s, argv=%O', id, argv);
18+
19+
// Get config vars
20+
const ENV_PREFIX = process.env.HYPERDRIVE_BOOTSTRAP_ENV_PREFIX || 'HYPERDRIVE';
21+
const ENV_SEPARATOR = process.env.HYPERDRIVE_BOOTSTRAP_ENV_SEPARATOR || '_';
22+
23+
// Build up hyperdrive/product config from various sources
24+
const bootstrapConf = new Ministrapper([config.name, 'lib', 'ministrapper']);
25+
26+
// @NOTE: do we want to accept some hidden args for this eg `hyperdrive --config bootstrap.module=something?`
27+
// ENVARS are highest priority
28+
bootstrapConf.env(ENV_PREFIX, ENV_SEPARATOR);
29+
debug('get config from %s%s* envvars done', ENV_PREFIX, ENV_SEPARATOR);
30+
31+
// Then user config if it exists
32+
bootstrapConf.file('user', {file: userConfig, format: require('nconf-yaml')});
33+
debug('get config from file %s done', userConfig);
7134

72-
// run bootstrap
73-
// 1. merge in more config
74-
// 2. go through plugins and build manifest of components/config/whatever
75-
// 3. traverse plugins to find commands
76-
// 4. what do commandIDs do?
77-
// 5. install defaults eg desktop -> lando-desktop
78-
/*
35+
// Then source config
36+
bootstrapConf.file('source', {file: sourceConfig, format: require('nconf-yaml')});
37+
debug('get config from file %s done', sourceConfig);
38+
39+
// Then defaults
40+
bootstrapConf.defaults({
41+
mode: 'cli',
42+
leia: Object.prototype.hasOwnProperty.call(process.env, 'LEIA_PARSER_RUNNING'),
43+
packaged: Object.prototype.hasOwnProperty.call(process, 'pkg'),
44+
product: 'hyperdrive',
45+
});
46+
debug('get config from defaults');
47+
48+
// Reset debugger to indicate product status
49+
debug = createDebugger(bootstrapConf.get('product'), 'hooks', 'init');
50+
debug('bootstrap config set to %O', bootstrapConf.get());
51+
52+
// Set DEBUG=* when -vvv is set?
53+
// run bootstrap
54+
// 1. merge in more config
55+
// 2. go through plugins and build manifest of components/config/whatever
56+
// 3. traverse plugins to find commands
57+
// 4. what do commandIDs do?
58+
// 5. install defaults eg desktop -> lando-desktop
59+
/*
7960
hyperdrive:
8061
// list of installers
8162
installers:
@@ -91,5 +72,11 @@ module.exports = async (options) => {
9172
mods: (?)
9273
- {id: 'install', path: }
9374
75+
// commands = [require('./../more/bye')];
76+
// config.plugins.push(new DynamicPlugin(config))
77+
// console.log(config.plugins);
78+
// config.plugins[0].commands[0].flags.stuff = flags.string({char: 'z', description: 'name to print'});
79+
// console.log(id, argv, config); // {id, argv, conf}
80+
9481
*/
95-
}
82+
};

cli/hooks/postrun.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
module.exports = async options => {
2-
console.log(`example postrun hook running before ${options.id}`)
3-
}
1+
module.exports = async() => {};

cli/hooks/prerun.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
1-
const _ = require('lodash');
2-
const {flags} = require('@oclif/command');
31

4-
module.exports = async options => {
5-
// options.Command = require('./../commands/hello2');
6-
// console.log(options);
7-
// options.Command.flags.name2 = flags.string({char: 'z', description: 'name to print'}),
8-
9-
10-
console.log(`example prerun hook running before ${options.id}`)
11-
}
2+
module.exports = async() => {};

cli/more/bye.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bootstrap:
2+
module: ./lib/bootstrap.js
3+
env:
4+
separator: _
5+
prefix: HYPERDRIVE

0 commit comments

Comments
 (0)