mirror of https://github.com/Seich/Beau.git
Making improvements to the plugins system. (#22)
This is mostly some clean up. Now plugins are checked for existence before being required. The regex for dynamic values was improved a little in an attemp to make it more reliable. Plugins are only required once now. The CLI's CWD is set to that of the config file. This makes relative paths mentioned in the file more reliable.
This commit is contained in:
parent
9ba3027017
commit
0677497074
|
|
@ -1,5 +1,6 @@
|
|||
const yaml = require('js-yaml');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const dotenv = require('dotenv');
|
||||
const { Command, flags } = require('@oclif/command');
|
||||
|
||||
|
|
@ -22,6 +23,12 @@ class Base extends Command {
|
|||
|
||||
const envParams = { params: Object.assign(env, params) };
|
||||
|
||||
const configFileDir = path.dirname(
|
||||
path.resolve(process.cwd(), configFile)
|
||||
);
|
||||
|
||||
process.chdir(configFileDir);
|
||||
|
||||
return new Beau(config, envParams);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ function requireg(name) {
|
|||
return require(name);
|
||||
}
|
||||
|
||||
requireg.std_resolving = false;
|
||||
requireg.resolving = true;
|
||||
|
||||
requireg.resolve = function(name) {
|
||||
if (requireg.std_resolving) {
|
||||
if (requireg.resolving) {
|
||||
return '';
|
||||
} else {
|
||||
throw new Error(`Failed to resolve.`);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
const yaml = require('js-yaml');
|
||||
const Beau = require('../beau');
|
||||
|
||||
const requireg = require('requireg');
|
||||
requireg.resolving = false;
|
||||
|
||||
describe(`Beau's config Loader.`, () => {
|
||||
it('should create a request list', () => {
|
||||
const doc = yaml.safeLoad(`
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
const yaml = require('js-yaml');
|
||||
const Config = require('../config');
|
||||
|
||||
const requireg = require('requireg');
|
||||
requireg.resolving = false;
|
||||
|
||||
describe('Config', () => {
|
||||
it('should load valid config keys', () => {
|
||||
const doc = yaml.safeLoad(`
|
||||
|
|
|
|||
|
|
@ -1,31 +1,15 @@
|
|||
const yaml = require('js-yaml');
|
||||
const Config = require('../config');
|
||||
const Plugins = require('../plugins');
|
||||
const Request = require('../request');
|
||||
const RequestCache = require('../requestCache');
|
||||
const requireg = require('requireg');
|
||||
|
||||
describe(`Beau's plugin system`, () => {
|
||||
let config;
|
||||
let request;
|
||||
let plugins;
|
||||
let doc;
|
||||
|
||||
beforeEach(() => {
|
||||
doc = yaml.safeLoad(`
|
||||
version: 1
|
||||
endpoint: 'http://example.com'
|
||||
|
||||
plugins:
|
||||
- Modifiers:
|
||||
data: hi
|
||||
- DynamicValues
|
||||
|
||||
GET /posts/$[add(1, 1)]: get-post
|
||||
`);
|
||||
|
||||
config = new Config(doc);
|
||||
plugins = config.PLUGINS;
|
||||
plugins = new Plugins([{ Modifiers: [Object] }, 'DynamicValues'], []);
|
||||
});
|
||||
|
||||
it('should load all plugins', () => {
|
||||
|
|
@ -34,19 +18,24 @@ describe(`Beau's plugin system`, () => {
|
|||
expect(plugins.registry.dynamicValues.length).toBe(1);
|
||||
});
|
||||
|
||||
it(`should load autoload plugins`, () => {
|
||||
requireg.std_resolving = true;
|
||||
config = new Config(doc);
|
||||
expect(config.PLUGINS.registry.dynamicValues.length).toBe(2);
|
||||
requireg.std_resolving = false;
|
||||
});
|
||||
|
||||
it(`should throw if given an invalid configuration`, () => {
|
||||
expect(() => new Plugins([{ test1: true, test2: true }])).toThrow();
|
||||
});
|
||||
|
||||
it(`shouldn't do anything when given an empty array.`, () => {
|
||||
expect(new Plugins()).toMatchSnapshot();
|
||||
expect(new Plugins([], [])).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it(`should warn if the plugin is not available.`, () => {
|
||||
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
requireg.resolving = false;
|
||||
|
||||
new Plugins(['not-a-Package']);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
|
||||
requireg.resolving = true;
|
||||
spy.mockReset();
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
describe(`Request Modifiers`, () => {
|
||||
|
|
|
|||
|
|
@ -14,32 +14,51 @@ class Plugins {
|
|||
|
||||
this.context = {};
|
||||
|
||||
plugins.forEach(plugin => this.loadPlugin(plugin));
|
||||
autoload.forEach(plugin => {
|
||||
try {
|
||||
requireg.resolve(plugin);
|
||||
this.loadPlugin(plugin);
|
||||
} catch (e) {}
|
||||
});
|
||||
this.loadPlugins(autoload.concat(plugins));
|
||||
}
|
||||
|
||||
loadPlugin(plugin) {
|
||||
normalizePlugins(plugins) {
|
||||
let results = {};
|
||||
|
||||
plugins.forEach(plugin => {
|
||||
let name = plugin;
|
||||
let settings = {};
|
||||
let settings = undefined;
|
||||
|
||||
if (typeof plugin === 'object') {
|
||||
let keys = Object.keys(plugin);
|
||||
|
||||
if (keys.length !== 1) {
|
||||
throw new Error(`Plugin items should contain only one key.`);
|
||||
throw new Error(
|
||||
`Plugin items should contain only one key.`
|
||||
);
|
||||
}
|
||||
|
||||
name = keys[0];
|
||||
settings = plugin[name];
|
||||
}
|
||||
|
||||
plugin = requireg(`beau-${toKebabCase(name)}`);
|
||||
new plugin(this, settings);
|
||||
results[name] = settings;
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
loadPlugins(plugins) {
|
||||
plugins = this.normalizePlugins(plugins);
|
||||
Object.keys(plugins).forEach(name => {
|
||||
const module = `beau-${toKebabCase(name)}`;
|
||||
|
||||
if (typeof requireg.resolve(module) !== 'undefined') {
|
||||
const plugin = requireg(module);
|
||||
new plugin(this, plugins[name]);
|
||||
} else {
|
||||
if (name === 'std') return;
|
||||
|
||||
console.warn(
|
||||
`Plugin ${name} couldn't be found. It is available globally?`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
executeModifier(modifier, obj, orig) {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ const httpVerbs = [
|
|||
|
||||
const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i');
|
||||
const replacementRegex = /(?:\\?)\$([a-zA-Z\.\d\-\_\/\\\:]+)/g;
|
||||
const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])+?\))\]/g;
|
||||
const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g;
|
||||
|
||||
const UpperCaseKeys = function(obj) {
|
||||
let result = {};
|
||||
|
|
|
|||
Loading…
Reference in New Issue