mirror of https://github.com/Seich/Beau.git
Added beau-std as an auto-loaded plugin.
This means that beau-std will automatically be loaded if available in the same space as Beau. beau-std is a set of commonly used plugins, these map directly to nodejs features that are commonly needed within Beau.
This commit is contained in:
parent
59f85fac8c
commit
e923668ecd
|
|
@ -0,0 +1,7 @@
|
||||||
|
class BeauStd {
|
||||||
|
constructor(registry, settings) {
|
||||||
|
registry.defineDynamicValue('createReadStream', () => {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = BeauStd;
|
||||||
|
|
@ -1,3 +1,15 @@
|
||||||
module.exports = function(name) {
|
function requireg(name) {
|
||||||
return require(name);
|
return require(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
requireg.std_resolving = false;
|
||||||
|
|
||||||
|
requireg.resolve = function(name) {
|
||||||
|
if (requireg.std_resolving) {
|
||||||
|
return '';
|
||||||
|
} else {
|
||||||
|
throw new Error(`Failed to resolve.`);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = requireg;
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,16 @@ 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');
|
||||||
|
|
||||||
describe(`Beau's plugin system`, () => {
|
describe(`Beau's plugin system`, () => {
|
||||||
let config;
|
let config;
|
||||||
let request;
|
let request;
|
||||||
let plugins;
|
let plugins;
|
||||||
|
let doc;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const doc = yaml.safeLoad(`
|
doc = yaml.safeLoad(`
|
||||||
version: 1
|
version: 1
|
||||||
endpoint: 'http://example.com'
|
endpoint: 'http://example.com'
|
||||||
|
|
||||||
|
|
@ -32,6 +34,13 @@ 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();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ const { toKebabCase, dynamicValueRegex, replaceInObject } = require('./shared');
|
||||||
const isPlainObject = require('is-plain-object');
|
const isPlainObject = require('is-plain-object');
|
||||||
|
|
||||||
class Plugins {
|
class Plugins {
|
||||||
constructor(plugins = []) {
|
constructor(plugins = [], autoload = ['std']) {
|
||||||
this.registry = {
|
this.registry = {
|
||||||
preRequestModifiers: [],
|
preRequestModifiers: [],
|
||||||
postRequestModifiers: [],
|
postRequestModifiers: [],
|
||||||
|
|
@ -15,6 +15,12 @@ class Plugins {
|
||||||
this.context = {};
|
this.context = {};
|
||||||
|
|
||||||
plugins.forEach(plugin => this.loadPlugin(plugin));
|
plugins.forEach(plugin => this.loadPlugin(plugin));
|
||||||
|
autoload.forEach(plugin => {
|
||||||
|
try {
|
||||||
|
requireg.resolve(plugin);
|
||||||
|
this.loadPlugin(plugin);
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loadPlugin(plugin) {
|
loadPlugin(plugin) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue