mirror of https://github.com/Seich/Beau.git
Making improvements to the plugins system.
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
29e241c704
|
|
@ -1,5 +1,6 @@
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
const dotenv = require('dotenv');
|
const dotenv = require('dotenv');
|
||||||
const { Command, flags } = require('@oclif/command');
|
const { Command, flags } = require('@oclif/command');
|
||||||
|
|
||||||
|
|
@ -22,6 +23,12 @@ class Base extends Command {
|
||||||
|
|
||||||
const envParams = { params: Object.assign(env, params) };
|
const envParams = { params: Object.assign(env, params) };
|
||||||
|
|
||||||
|
const configFileDir = path.dirname(
|
||||||
|
path.resolve(process.cwd(), configFile)
|
||||||
|
);
|
||||||
|
|
||||||
|
process.chdir(configFileDir);
|
||||||
|
|
||||||
return new Beau(config, envParams);
|
return new Beau(config, envParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ function requireg(name) {
|
||||||
return require(name);
|
return require(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
requireg.std_resolving = false;
|
requireg.resolving = true;
|
||||||
|
|
||||||
requireg.resolve = function(name) {
|
requireg.resolve = function(name) {
|
||||||
if (requireg.std_resolving) {
|
if (requireg.resolving) {
|
||||||
return '';
|
return '';
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Failed to resolve.`);
|
return undefined;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const Beau = require('../beau');
|
const Beau = require('../beau');
|
||||||
|
|
||||||
|
const requireg = require('requireg');
|
||||||
|
requireg.resolving = false;
|
||||||
|
|
||||||
describe(`Beau's config Loader.`, () => {
|
describe(`Beau's config Loader.`, () => {
|
||||||
it('should create a request list', () => {
|
it('should create a request list', () => {
|
||||||
const doc = yaml.safeLoad(`
|
const doc = yaml.safeLoad(`
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const Config = require('../config');
|
const Config = require('../config');
|
||||||
|
|
||||||
|
const requireg = require('requireg');
|
||||||
|
requireg.resolving = false;
|
||||||
|
|
||||||
describe('Config', () => {
|
describe('Config', () => {
|
||||||
it('should load valid config keys', () => {
|
it('should load valid config keys', () => {
|
||||||
const doc = yaml.safeLoad(`
|
const doc = yaml.safeLoad(`
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,15 @@
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const Config = require('../config');
|
|
||||||
const Plugins = require('../plugins');
|
const Plugins = require('../plugins');
|
||||||
const Request = require('../request');
|
const Request = require('../request');
|
||||||
const RequestCache = require('../requestCache');
|
const RequestCache = require('../requestCache');
|
||||||
const requireg = require('requireg');
|
const requireg = require('requireg');
|
||||||
|
|
||||||
describe(`Beau's plugin system`, () => {
|
describe(`Beau's plugin system`, () => {
|
||||||
let config;
|
|
||||||
let request;
|
let request;
|
||||||
let plugins;
|
let plugins;
|
||||||
let doc;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
doc = yaml.safeLoad(`
|
plugins = new Plugins([{ Modifiers: [Object] }, 'DynamicValues'], []);
|
||||||
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;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load all plugins', () => {
|
it('should load all plugins', () => {
|
||||||
|
|
@ -34,19 +18,24 @@ describe(`Beau's plugin system`, () => {
|
||||||
expect(plugins.registry.dynamicValues.length).toBe(1);
|
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`, () => {
|
it(`should throw if given an invalid configuration`, () => {
|
||||||
expect(() => new Plugins([{ test1: true, test2: true }])).toThrow();
|
expect(() => new Plugins([{ test1: true, test2: true }])).toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`shouldn't do anything when given an empty array.`, () => {
|
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`, () => {
|
describe(`Request Modifiers`, () => {
|
||||||
|
|
|
||||||
|
|
@ -14,32 +14,51 @@ class Plugins {
|
||||||
|
|
||||||
this.context = {};
|
this.context = {};
|
||||||
|
|
||||||
plugins.forEach(plugin => this.loadPlugin(plugin));
|
this.loadPlugins(autoload.concat(plugins));
|
||||||
autoload.forEach(plugin => {
|
|
||||||
try {
|
|
||||||
requireg.resolve(plugin);
|
|
||||||
this.loadPlugin(plugin);
|
|
||||||
} catch (e) {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadPlugin(plugin) {
|
normalizePlugins(plugins) {
|
||||||
|
let results = {};
|
||||||
|
|
||||||
|
plugins.forEach(plugin => {
|
||||||
let name = plugin;
|
let name = plugin;
|
||||||
let settings = {};
|
let settings = undefined;
|
||||||
|
|
||||||
if (typeof plugin === 'object') {
|
if (typeof plugin === 'object') {
|
||||||
let keys = Object.keys(plugin);
|
let keys = Object.keys(plugin);
|
||||||
|
|
||||||
if (keys.length !== 1) {
|
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];
|
name = keys[0];
|
||||||
settings = plugin[name];
|
settings = plugin[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin = requireg(`beau-${toKebabCase(name)}`);
|
results[name] = settings;
|
||||||
new plugin(this, 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) {
|
executeModifier(modifier, obj, orig) {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ const httpVerbs = [
|
||||||
|
|
||||||
const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i');
|
const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i');
|
||||||
const replacementRegex = /(?:\\?)\$([a-zA-Z\.\d\-\_\/\\\:]+)/g;
|
const replacementRegex = /(?:\\?)\$([a-zA-Z\.\d\-\_\/\\\:]+)/g;
|
||||||
const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])+?\))\]/g;
|
const dynamicValueRegex = /\$\[(\w+\((?:.|[\n\r])*?\))\]/g;
|
||||||
|
|
||||||
const UpperCaseKeys = function(obj) {
|
const UpperCaseKeys = function(obj) {
|
||||||
let result = {};
|
let result = {};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue